Forum Discussion
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:
| ID | Date | Unit |
| 1 | 02/03/2010 | 1 |
| 2 | 05/05/2010 | 2 |
| 3 | 09/06/2010 | 1 |
| 4 | 01/06/2011 | 3 |
| 5 | 02/10/2011 | 1 |
| 6 | 15/12/2011 | 4 |
| 7 | 14/10/2011 | 1 |
| 8 | 30/04/2011 | 2 |
| 9 | 01/01/2012 | 1 |
| 10 | 06/07/2012 | 3 |
| 11 | 03/09/2012 | 2 |
| 12 | 24/12/2012 | 8 |
| 13 | 18/05/2012 | 2 |
| 14 | 19/05/2012 | 1 |
| 15 | 01/03/2013 | 1 |
| 16 | 02/03/2013 | 3 |
| 17 | 08/10/2013 | 6 |
| 18 | 21/08/2013 | 2 |
| 19 | 02/02/2014 | 3 |
| 20 | 14/02/2014 | 1 |
| 21 | 13/05/2014 | 1 |
| 22 | 16/09/2015 | 2 |
| 23 | 17/05/2015 | 2 |
| 24 | 31/07/2015 | 1 |
| 25 | 01/09/2016 | 4 |
| 26 | 03/05/2016 | 1 |
| 27 | 01/02/2016 | 2 |
| 28 | 30/10/2016 | 2 |
| 29 | 24/11/2016 | 3 |
| 30 | 05/04/2017 | 6 |
| 31 | 06/05/2017 | 5 |
| 32 | 01/09/2017 | 2 |
| 33 | 24/12/2017 | 10 |
| 34 | 31/12/2017 | 5 |
| 35 | 06/06/2018 | 2 |
| 36 | 07/08/2018 | 4 |
| 37 | 09/09/2018 | 1 |
| 38 | 01/02/2018 | 6 |
| 39 | 04/10/2019 | 15 |
| 40 | 20/01/2019 | 10 |
| 41 | 14/03/2019 | 8 |
Expected Results
| Year | Sum of Unit | 3 Yr Avg |
| 2010 | 4 | 4 |
| 2011 | 11 | 7.5 |
| 2012 | 17 | 10.6 |
| 2013 | 12 | 13.3 |
| 2014 | 5 | 11.3 |
| 2015 | 5 | 7.3 |
| 2016 | 12 | 7.3 |
| 2017 | 28 | 15 |
| 2018 | 13 | 17.6 |
| 2019 | 33 | 24.6 |
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 ) ) )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.
12 Replies
- amitchandakSuper 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/- rogerdeaHelper IV
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:
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?
- rogerdeaHelper IV
To be clear its the calendar year average i need, eg 1st Jan to 31 Dec each year, summed and then these averaged.
- v-xuding-msftCommunity 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 ) ) )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.
- rogerdeaHelper IV
Thanks v-xuding-msft
I'm getting an error half way through the second part you posted: (blurred my table names for privacy)
Have i done something wrong?
- v-xuding-msftCommunity Support
Hi rogerdea ,
There is an extra parenthesis in your formula. I point it out with red pen. Please remove it to have a try.
You could download the pbix file from my answer above. It may help you a little.