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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
davmor
Frequent Visitor

Rolling average of a sum

I need to calculate the 3 day rolling average of a sum. Couple of items which are tricky:

1) I cannot use the built in rolling average function as I need to use the last 3 available dates which might not be the last three calendar date (weekend, holidays, etc.)

2) I first need to sum the data for a given date and then take the average across dates

 

This is an example of my data set

 

ComponentDateAmount
Component ADate 179.86
Component BDate 193.14
Component CDate 124.89
Component ADate 264.29
Component BDate 299.21
Component CDate 270.75
Component ADate 382.23
Component BDate 325.86
Component CDate 383.60
Component ADate 473.14
Component BDate 480.40
Component CDate 428.74
Component ADate 547.62
Component BDate 531.29
Component CDate 567.56
Component ADate 679.08
Component BDate 697.87
Component CDate 691.66
Component ADate 738.20
Component BDate 737.08
Component CDate 76.24
Component ADate 823.28
Component BDate 814.21
Component CDate 842.81
Component ADate 934.29
Component BDate 955.87
Component CDate 920.05
Component ADate 1025.43
Component BDate 1096.04
Component CDate 1096.95

 

This is what my output should look like (I actually do not need the "Sum of Amount" column, but I thought it might be easier to show the calculation).

 

DateSum of AmountRolling Average
Date 1197.89 
Date 2234.24 
Date 3191.70207.9417
Date 4182.29202.7412
Date 5146.46173.4826
Date 6268.61199.1193
Date 781.52165.5311
Date 880.30143.4749
Date 9110.2190.6753
Date 10218.41136.3054

 

Thanks in advance for any help I get.

1 ACCEPTED SOLUTION

Can't speak to how efficient/smart this is but it seems to work for the test data provided (maybe a better solution would be to use a ranked date list?):

 

Measure = 
AVERAGEX(
    VALUES(Data[Date]),
    VAR AvgBase = 3
    VAR DateIter = [Date]
    VAR CurDateSubset = FILTER(DISTINCT(ALL(Data[Date])),[Date]<=DateIter)
    VAR Last3Dates = TOPN(AvgBase,CurDateSubset,[Date],DESC)
    VAR AvgBaseExists = IF(COUNTROWS(Last3Dates)=AvgBase,AvgBase,BLANK())
    RETURN
    CALCULATE(DIVIDE(SUM(Data[Amount]),AvgBaseExists),Last3Dates)
)

Note that this is set up to easily change the last '3' dates to some other number (change AvgBase = 3 line).

 

The AVERAGEX will provide an average at summary levels, if you want a sum then do SUMX (or MAXX for the max, etc.). Note that summary level is averaging/adding/etc. the calculated values, not the real average/sum of the underlying data.

 

**Edit: same concept except avoiding an iterator X function (so, no summary values / will only work if Date column is in visual):

Measure 2 = 
VAR AvgBase = 3
VAR DateIter = SELECTEDVALUE(Data[Date])
VAR CurDateSubset = FILTER(DISTINCT(ALL(Data[Date])),[Date]<=DateIter)
VAR Last3Dates = TOPN(3,CurDateSubset,[Date],DESC)
VAR AvgBaseExists = IF(COUNTROWS(Last3Dates)=AvgBase,AvgBase,BLANK())
RETURN
CALCULATE(DIVIDE(SUM(Data[Amount]),AvgBaseExists),Last3Dates)

 

View solution in original post

3 REPLIES 3
davmor
Frequent Visitor

Rather than a column I probably should have said a measure.

Can't speak to how efficient/smart this is but it seems to work for the test data provided (maybe a better solution would be to use a ranked date list?):

 

Measure = 
AVERAGEX(
    VALUES(Data[Date]),
    VAR AvgBase = 3
    VAR DateIter = [Date]
    VAR CurDateSubset = FILTER(DISTINCT(ALL(Data[Date])),[Date]<=DateIter)
    VAR Last3Dates = TOPN(AvgBase,CurDateSubset,[Date],DESC)
    VAR AvgBaseExists = IF(COUNTROWS(Last3Dates)=AvgBase,AvgBase,BLANK())
    RETURN
    CALCULATE(DIVIDE(SUM(Data[Amount]),AvgBaseExists),Last3Dates)
)

Note that this is set up to easily change the last '3' dates to some other number (change AvgBase = 3 line).

 

The AVERAGEX will provide an average at summary levels, if you want a sum then do SUMX (or MAXX for the max, etc.). Note that summary level is averaging/adding/etc. the calculated values, not the real average/sum of the underlying data.

 

**Edit: same concept except avoiding an iterator X function (so, no summary values / will only work if Date column is in visual):

Measure 2 = 
VAR AvgBase = 3
VAR DateIter = SELECTEDVALUE(Data[Date])
VAR CurDateSubset = FILTER(DISTINCT(ALL(Data[Date])),[Date]<=DateIter)
VAR Last3Dates = TOPN(3,CurDateSubset,[Date],DESC)
VAR AvgBaseExists = IF(COUNTROWS(Last3Dates)=AvgBase,AvgBase,BLANK())
RETURN
CALCULATE(DIVIDE(SUM(Data[Amount]),AvgBaseExists),Last3Dates)

 

Thanks a lot. I had seen many replies in other theads, but none seemed to work as expected. This one (I tried the second one) works perfectly and seems to be very quick.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.