Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
JSiebrecht
Resolver I
Resolver I

Rolling average for X entries

Rolling average NOT on dates

Dear community,

I am trying to calculate a rolling/moving average across the last e.g. 100 entries.
But all tutorials I found so far were always on a number of days and the entries in the period of these x days.
I need to calculate the rolling average simply across the latest X values though. Not day or month or years.


I have a table (simplified) with an ID in strictly accending integers (with gaps though) and a duration (integer seconds).
I want to calculate in a measure the moving average of the last X (in the example 10) enties, i.e. the last X entries with the highest ID including the current.

As always
THANK YOU VERY MUCH
for your help!

Best,
Jan

PS
Somehow the HTML table gets reformated very narrowly.
I enclose also a picture of the table.

JSiebrecht_0-1647010240410.png



IDDurationSMA
4463137841448771,50
4463137598386976,20
446313759259381,40
4463137569444993,50
4463137555563999,70
446313752431166107,20
4463137521994100,50
4463137519683110,40
446313751042154119,80
446313749537141113,70
446313748727134111,10
446313747222121111,10
446313746875124110,90
446313746065111118,90
446313745718114117,20
44631374467699114,30
446313744444103111,70
44631374375097107,60
44631374294093105,10
446313741667115102,90
446313739815134100,30
44631373807911996,70
44631373657420493,40
4463137351859480,60
4463137341448577,70
4463137327557375,00
4463137314816272,10
4463136811347278,70
4463136380797184,10
4463136335658989,00
4463136325239890,80
4463136311348691,00
4463136299777690,10
4463136287046591,50
4463136278945894,44
4463136262734499,00
446313624653128106,86
446313623380126103,33
44631362256912098,80
44631362106510793,50
44631361979210089,00
4463136187507783,50
4463136184039090,00
6 REPLIES 6
v-jayw-msft
Community Support
Community Support

Hi @JSiebrecht ,

 

You could add index column in Power Query Editor.

Then create a measure like below.

Measure = CALCULATE(AVERAGE('Table'[Duration]),FILTER(ALLSELECTED('Table'),'Table'[Index]<SELECTEDVALUE('Table'[Index])+10&&'Table'[Index]>=SELECTEDVALUE('Table'[Index])))
1.jpg
 
Best Regards,
Jay
Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

Dear @v-jayw-msft 

many thanks for your input.

While this as a formula seems plausible, in my case it will just not evaluate.
The amount of records simply seems to be too large to calculate this in a meaure.
Trying to put it into a custom column gives me calculated SMA values that make no sense at all.

Oh, well. I guess my business user will just have to make due without a moving average and look at the timeseries as is.

Thanks anyway y'all!

johnt75
Super User
Super User

Rolling average =
var currentID = SELECTEDVALUE( 'Table'[ID] )
return AVERAGEX( TOPN(10, FILTER( ALL('Table'), 'Table'[ID] <= currentID), 'Table'[ID] ), 'Table'[SMA] )

Dear @johnt75 ,

thanks for the prompt response!
I tried your suggestion, but my machine is not able to finish the evaluation and after much CPU cooking stops for lack of memory. And I have only 24'000 records.

I tried without the ALL() and then the memory is sufficient.
But I do not get a moving average. Each item shows the value of Duration also for the moving average.

DAX used:

Import Duration Moving Average = 
 var currentID = SELECTEDVALUE( Contract[ID] )
return 
AVERAGEX(
    TOPN(10, FILTER(ALL('Contract'), Contract[ID] <= currentID), Contract[ID]),
    Contract[Import Duration] )

 

This is the result:

JSiebrecht_0-1647018329754.png

Any idea?

Thanks,
Jan

Unfortunately the ALL is required to get the correct result, that removes the row context introduced by the AVERAGEX. Without it it will only consider the current row, which is why you are seeing the same value in the moving average as in the import duration.

Do you need the averages for every row you have, or only the most recent? I'm thinking that you could use the same basic code to produce a calculated table, which would only be calculated once during data refresh, that had only the most recent X values of id and included the moving average as a column rather than a measure. You could even try adding the column to your full table and see if it can manage that, 24,000 rows isn't so many.

Hi @johnt75 

I understand.
Didn't think 24000 recods would be a problem. I saw guys do rolling averages with stock datasets of several years without problems. In the examples on the net, as said earlier, they usually use dates and the DATESBETWEEN function or DAYSINPERIOD or so.
Apparently this is easier on memory than the TOPN.

I tried with the ALL() and write the Moving Average to a custom column rather than a measure.
But thenn I don't get any entries at all and the column stays empty.

JSiebrecht_0-1647020970142.png

 

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.