Forum

Calculate rolling a...
 
Notifications
Clear all

Calculate rolling average in power query

5 Posts
3 Users
0 Reactions
629 Views
(@andyc)
Posts: 20
Eminent Member
Topic starter
 

Could someone please explain how to calculate a rolling average for a column of numbers in power query.
I think I need to add an index column and use this with list.average in a custom column but I can't work out the correct syntax.
I've looked online but all the examples I've found seem to be for Power BI and I'm using Excel.

Thanks

 
Posted : 25/05/2023 9:48 am
Philip Treacy
(@philipt)
Posts: 1630
Member Admin
 

Hi Andy,

There are a few ways to do it but the easiest would be to add an Index column starting from 1, then use List.FirstN to get the items in the column up to the current row, sum them, and divide by the row number (given by the Index column)

List.Sum(List.FirstN(#"Changed Type"[Value],[Index]))/[Index]

rolling average in power query

See attached file for example

Regards

Phil

 
Posted : 25/05/2023 7:48 pm
(@andyc)
Posts: 20
Eminent Member
Topic starter
 

Thanks Phil,

That makes sense.

I realise now though that I haven't asked my question correctly!

Maybe what I'm after is correctly called a moving average of a specified number of values.

Please see my attached file as an example.

Here, the first 4 rows are averaged, then rows 2,3,4,5 are averaged, then rows 3,4,5,6 and so on.

I'd like to know how to do this in power query.

My apologies for not making this clear.

Regards,

Andy

 
Posted : 26/05/2023 3:41 am
Riny van Eekelen
(@riny)
Posts: 1195
Member Moderator
 

See attached!

 
Posted : 26/05/2023 4:21 am
(@andyc)
Posts: 20
Eminent Member
Topic starter
 

Nice one Riny!

Thanks,

Andy

 
Posted : 26/05/2023 4:44 am
Share: