PowerQuery: avg last three dates values

illusionek

Board Regular
Joined
Jun 21, 2014
Messages
104
Hello

I am looking for a way in PowerQuery to do an average of last three values (based on the week) for each product. The challenge I have is that dates are not always consecutive and there is also scenario where I will have less than three values available.

I provided sample of data in below table and then expected results in the second table. The desire outcome is to get avg of last three dates for each product and if there are less than three values available in total then avg just whats available.



ProductWeekSold Qty
BC000210/10/2021
21000​
BC000603/10/2021
90000​
BC000617/10/2021
1000​
BC000631/10/2021
0​
BC000607/11/2021
82000​
BC002203/10/2021
81000​
BC002210/10/2021
80000​
BC002217/10/2021
100000​
BC002224/10/2021
59000​
BC002231/10/2021
81000​
BC002207/11/2021
33000​
BC008603/10/2021
82090​
BC008607/11/2021
57600​
BC010903/10/2021
111725​
BC010910/10/2021
105000​
BC010917/10/2021
185950​
BC010924/10/2021
158975​
BC010931/10/2021
161325​
BC010907/11/2021
219000​


BC0002
21000​
BC0006
27666.67​
BC0022
57666.67​
BC0086
69845​
BC0109
179766.7​
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Change the first line to reflect the table name you are using, and try:

Power Query:
let
    tbl1 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    tblType = Table.TransformColumnTypes(tbl1,{{"Sold Qty", Int64.Type}, {"Week", type date}}),
    tblSort = Table.Sort(tblType,{{"Week", Order.Ascending}}),
    tbl2 = Table.Group(tblSort, {"Product"}, {{"All", each _}}),
    tbl3 = Table.AddColumn(tbl2, "Avg", each let trc = Table.RowCount(_[All]) in 
            List.Average(List.Skip(_[All][Sold Qty], if trc<4 then 0 else trc-3))),
    tbl4 = Table.RemoveColumns(Table.Sort(tbl3,{{"Product", Order.Ascending}}),{"All"})
in
    tbl4
 
Upvote 0
Another way.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"Product"}, {{"Data", each _, type table [Product=text, Week=datetime, Sold Qty=number]}}),
    Avg = Table.TransformColumns(Group,{{"Data", each List.Average(Table.MaxN(_,"Week", 3)[Sold Qty])}})
in
    Avg
 
Upvote 0
Solution
Power Query:
= Table.Group(Source,"Product",{"Avg",each List.Average(Table.MaxN(_,"Week",3)[Sold Qty])})
1636341520215.png
 
Upvote 0

Forum statistics

Threads
1,223,693
Messages
6,173,874
Members
452,536
Latest member
Chiz511

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top