Forum Discussion
Hide previous years on x axis but keep average calculation
Hi all-
I have a line graph with a count of items and a 5 year average (method here). I want to show a line graph of only the trends from the previous 5 years (e.g. count and then the average), but do *not* want to show the years prior.
For example, the table and graph for 10 years looks something like this:
I want to show only 2020-2024 on a line graph with two lines, one of the actual count and one of the 5 year average (so each 5 year point is an average from 2015-2019, 2016-2020, etc. etc.). Unfortunately, putting the filter causes the calculations to fail, for obvious reasons - it's unable to look more than n-5 years back.
The graph needs to show what's in the red box, without the 2014-2019 data.
Any assistance would be greatly appreciated. Thanks!
-K
Hi KCfromDC ,
Apologies for the incorrection I have done - 5 years on the formula and should be minus -4 years to get current year + 4 that gives 5 years.
If you do the math:
105 + 136 + 123 + 90 +122 +107 = 683 / 6 = 113.83
Redo the formula to:
Notifications prev 5 years avg = VAR _TempTable = ADDCOLUMNS ( CALCULATETABLE ( VALUES ( 'Dates'[Year] ), REMOVEFILTERS ( 'Dates'[Year] ) ), "Notifications", [Notifications count] ) RETURN AVERAGEX ( FILTER ( _TempTable , 'Calendar'[Year] <= MAX ( 'Dates'[Year] ) && 'Calendar'[Year] >= MAX ( 'Dates'[Year] ) - 4 ), [Notifications] )This should get expected result
7 Replies
- KCfromDCFrequent Visitor
Hi Miguel-
Notifications prev 5 years avg = VAR selectedyear = MAX ( Dates[Year] ) VAR previous5years = selectedyear - 5 VAR newdatetable = CALCULATETABLE ( VALUES ( Dates[Year] ), FILTER ( ALL ( Dates ), Dates[Year] < selectedyear && Dates[Year] >= previous5years ) ) RETURN AVERAGEX ( newdatetable, [Notifications count] )Notifications count = CALCULATE( COUNTROWS(Notifications),Dates[Year] = max(Dates[Year]) )I'm a total noob at this, so appreciate the help. Thank you!
- MFelix
Super User
Hi KCfromDC ,
Try to change your calculation to the following:
Notifications prev 5 years avg = VAR _TempTable = ADDCOLUMNS ( CALCULATETABLE ( VALUES ( 'Dates'[Year] ), REMOVEFILTERS ( 'Dates'[Year] ) ), "Notifications", [Notifications count] ) RETURN AVERAGEX ( FILTER ( _TempTable , 'Calendar'[Year] <= MAX ( 'Dates'[Year] ) && 'Calendar'[Year] >= MAX ( 'Dates'[Year] ) - 5 ), [Notifications] )This should return expected result.
- KCfromDCFrequent Visitor
Thanks! I changed the Return function to be 'Dates'[Year] as that's my date table. However, it's giving me the wrong values.
Here's what I get in Excel (using values back to 2010)
Year Count 5 year average 2010 105 2011 136 2012 123 2013 90 2014 122 2015 107 115.2 2016 130 115.6 2017 130 114.4 2018 150 115.8 2019 134 127.8 2020 152 130.2 2021 185 139.2 2022 134 150.2 2023 180 151 2024 176 157 The below is from PowerBI. The original column is calculating correctly from 2015-2024 (since it's not able to "see" the 2014 and earlier 5 year average, e.g. 2009-2013, 2008-2012, etc.), but the new column using the code you provided is incorrect. 🤔
This is a tough one, I appreciate the assistance!
- MFelix
Super User
Hi KCfromDC ,
Apologies for the incorrection I have done - 5 years on the formula and should be minus -4 years to get current year + 4 that gives 5 years.
If you do the math:
105 + 136 + 123 + 90 +122 +107 = 683 / 6 = 113.83
Redo the formula to:
Notifications prev 5 years avg = VAR _TempTable = ADDCOLUMNS ( CALCULATETABLE ( VALUES ( 'Dates'[Year] ), REMOVEFILTERS ( 'Dates'[Year] ) ), "Notifications", [Notifications count] ) RETURN AVERAGEX ( FILTER ( _TempTable , 'Calendar'[Year] <= MAX ( 'Dates'[Year] ) && 'Calendar'[Year] >= MAX ( 'Dates'[Year] ) - 4 ), [Notifications] )This should get expected result
- AnonymousNot applicable
Hi KCfromDC ,
Thank you for reaching out to the Microsoft Fabric Community Forum.
Could you please let us know if the issue has been resolved? I wanted to check if you had the opportunity to review the information provided by MFelix . If you still require support, please let us know, we are happy to assist you.
Thank you.
- AnonymousNot applicable
Hi KCfromDC ,
We haven’t received an update from you in some time. Could you please let us know if the issue has been resolved?
If you still require support, please let us know, we are happy to assist you.Thank you.