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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
OliverMatsik
Frequent Visitor

Using count of selected years to calculate average

Hi,

 

I hope someone can help me out here 🙂

 

I have a graph which shows 2020 Revenue trend per month and another line which is 2017 - 2019 AVG, so you can see how much we differ from the trend and also what we can expect for the rest of the year based on history.

 

Revenue trend comparison.PNG

 

Below that there is another one which shows the same but showing the yearly totals.

 

Revenue per year comparison.PNG

 

I would like to 2017 - 2019 AVG line to dynamicly calculate based on the selection in the table below. So if you only click on 2019 then it basically compares 2019 to 2020, if you select 2018 + 2019 then it only devides thes SUM by 2 and not 3.

 

For this I wrote a measure to count the selected years (and exclude 2020): 

Selected years count = IF(CONTAINS(FinancialData , FinancialData[Date].Year] , 2020)=TRUE,

CALCULATE(DISTINCTCOUNT(FinancialData[Date].[Year]) , FinancialData[Date])-1,

CALCULATE(DISTINCTCOUNT(FinancialData[Date].[Year]) , FinancialData[Date]))

 
I checked and the calculation returns a number between 1 and 3, and it is a number. So I thought so far so good...
 
Then I wanted to devide the sum of 2017 + 2018 + 2019 Revenue by the above. There is no syntax error but the calculation simply doesn't work
2017 - 2019 AVG Revenue = (FinancialData[2017 Revenue]+FinancialData[2018 Revenue]+FinancialData[2019 Revenue]) / [Selected years count]
 
Revenue trend calc issue.PNG
 
If I devide it simply by 3 it works perfectly but of course it is not dynamic and if you only select on or 2 years the calculation is off.
 
Your support is much appreciated.
 
Oliver
2 REPLIES 2
amitchandak
Super User
Super User

@OliverMatsik , Try like this with a date table

Avg last 3 year = if(isbalnk(SUM(FinancialData[2017 Revenue]),(CALCULATE(SUM(FinancialData[2017 Revenue]),DATESMTD(dateadd('Date'[Date],-1,year)))+CALCULATE(SUM(FinancialData[2017 Revenue]),DATESMTD(dateadd('Date'[Date],-2,year))) +CALCULATE(SUM(FinancialData[2017 Revenue]),DATESMTD(dateadd('Date'[Date],-3,year))))/3)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks, I created a date table, but it somehow messed up my current date information, my graphs crashed.

 

Can you advise how can I make my calculation work maybe? If the calculated measure works well, it calculates the number of selected years correctly, why cant I use this number to simply divide the selected years' totals with the number of years?

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.