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
rogerdea
Helper IV
Helper IV

Rolling 3 Year Average

Hello

 

I am trying to figure out how to calculate this from my data and struggling with this.  I need the rolling 3 year average of the sum of units, taking into account that for the first year the data begins (2010) the average should be 1 year avg, and for the first two years (2010, 2011) it will be a 2 year avg then moving to the required 3 year averages thereafter.

 

I'm connecting my data to a date table made by using the CALENDAR function.

 

Some sample data is below, which includes expected results following that.  Any help appreciated!

 

DATA:

IDDateUnit
102/03/20101
205/05/20102
309/06/20101
401/06/20113
502/10/20111
615/12/20114
714/10/20111
830/04/20112
901/01/20121
1006/07/20123
1103/09/20122
1224/12/20128
1318/05/20122
1419/05/20121
1501/03/20131
1602/03/20133
1708/10/20136
1821/08/20132
1902/02/20143
2014/02/20141
2113/05/20141
2216/09/20152
2317/05/20152
2431/07/20151
2501/09/20164
2603/05/20161
2701/02/20162
2830/10/20162
2924/11/20163
3005/04/20176
3106/05/20175
3201/09/20172
3324/12/201710
3431/12/20175
3506/06/20182
3607/08/20184
3709/09/20181
3801/02/20186
3904/10/201915
4020/01/201910
4114/03/20198

 

Expected Results

YearSum of Unit3 Yr Avg
201044
2011117.5
20121710.6
20131213.3
2014511.3
201557.3
2016127.3
20172815
20181317.6
20193324.6
1 ACCEPTED SOLUTION
v-xuding-msft
Community Support
Community Support

Hi @rogerdea ,

Please try like this:

  • Create a calendar table.

 

Date =
VAR _calendar =
    CALENDAR ( MIN ( 'Table'[Date] ), MAX ( 'Table'[Date] ) )
RETURN
    ADDCOLUMNS ( _calendar, "Year", YEAR ( [Date] ) )

 

  • Create a measure.

 

Measure =
DIVIDE (
    CALCULATE (
        SUM ( 'Table'[Unit] ),
        DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, YEAR )
    ),
    CALCULATE (
        DISTINCTCOUNT ( 'Date'[Year] ),
        DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, YEAR )
    )
)

 

4.PNG

For more details, please see the attachment.

 

Best Regards,

Xue Ding

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

12 REPLIES 12
v-xuding-msft
Community Support
Community Support

Hi @rogerdea ,

Please try like this:

  • Create a calendar table.

 

Date =
VAR _calendar =
    CALENDAR ( MIN ( 'Table'[Date] ), MAX ( 'Table'[Date] ) )
RETURN
    ADDCOLUMNS ( _calendar, "Year", YEAR ( [Date] ) )

 

  • Create a measure.

 

Measure =
DIVIDE (
    CALCULATE (
        SUM ( 'Table'[Unit] ),
        DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, YEAR )
    ),
    CALCULATE (
        DISTINCTCOUNT ( 'Date'[Year] ),
        DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, YEAR )
    )
)

 

4.PNG

For more details, please see the attachment.

 

Best Regards,

Xue Ding

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks @v-xuding-msft 

 

I'm getting an error half way through the second part you posted: (blurred my table names for privacy)

Captureerror.JPG

Have i done something wrong?

 

Hi @rogerdea ,

There is an extra parenthesis in your formula. I point it out with red pen. Please remove it to have a try.

6.PNG

 

You could download the pbix file from my answer above. It may help you a little.

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-xuding-msft 

 

amazing this works in my main pbx file now.  However one thing which doesnt look right on your attached pbx is the 2019 average is incorrect. All others are correct.  Using the same DAX this does work in my file though.

Hi @rogerdea ,

Glad it works fine in your sample.😊

 

For the wrong result, it might be caused by date. The format of date is mm/dd/yyyy rather than dd/mm/yyyy in my sample. I don't change it to match your sample date completely. I just import it and the Desktop change it from your format to mm/dd/yyyy automatically.

 

Best Regards,

Xue Ding

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
amitchandak
Super User
Super User

Typically this how we do rolling with a date calendar. example

Rolling 3 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],ENDOFMONTH(Sales[Sales Date]),-3,Year)) 
Or
Rolling 3 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date Filer],MAX(Sales[Sales Date]),-3,Year))  

Rolling 3 = CALCULATE(Average(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],ENDOFMONTH(Sales[Sales Date]),-3,Year)) 
Or
Rolling 3 = CALCULATE(Average(Sales[Sales Amount]),DATESINPERIOD('Date'[Date Filer],MAX(Sales[Sales Date]),-3,Year))  

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

 

Rolling 3 = CALCULATE(Average(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],ENDOFMONTH(Sales[Sales Date]),-3,Year)) 

 

 

@amitchandak  I tried the above method and i have no errors, but my output is:

 

rolling.JPG

Each row has a column with the amoun of units (which are all 1).  Does there need to be a SUM somewhere in the DAX to get the yearly sum?

Try like

rolling 3 = CALCULATE(Average(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],ENDOFMONTH(Sales[Sales Date]),-3,Year)) 

Avg rolling = Averagex(summarize(table,Date[year],"_1",[rolling 3 ]),[_1])

I've not been able to get that working, the first part just returns a "1" for each year.   But i changed that to the following which works except for the first two years, where there is not enough years data to divide by 3:

 

Changed average to a sum, and then divided by 3. 

Rolling 3 = CALCULATE(SUM('FACT DATA'[data_table]),DATESINPERIOD('Date Table'[Date],ENDOFMONTH('FACT DATA'[Date]),-3,YEAR)) /3

 Problem is the first two years are not the correct average because there is not three years of data.

@rogerdea , Please find the attached solution after signature

Check for Avg Unit

Thanks @amitchandak 

 

That's almost working but not quite.  The avg unit field for years 2010, 2011 & 2012 are correct but 2013 onwards are not.  For 2013 i would expect the 'average unit' result to be 13.3 (and not 10.00).  This should be the average of values 11, 17 and 12.

To be clear its the calendar year average i need, eg 1st Jan to 31 Dec each year, summed and then these averaged.

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.