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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
lhanson1
New Member

Yearly Percent Change with Slicer and gapped years

Hi - 

 

I'm trying to calculate the percent change in a count of items labelled as "major" in a dataset between two different years. However, I don't have all consecutive years, just 2020, 2022 and 2023. This was causing issues for my formula and I've tried a few different approaches but still don't have a final solution. My current formula, pasted below, returns for all my variables except PrevYearCount, and I can't figure out why. It also returns 0 when 2020 is selected but is blank when 2022 or 2023 is selected.

 

% Change in Majors =
VAR CurrYear = SELECTEDVALUE('SIA Interact'[Year]) // Year slicer input
VAR CompAmount = IF(CurrYear = 2020, 0, IF(CurrYear = 2022, 2, 1)) // To account for missing years/gaps
VAR PrevYear = CurrYear - CompAmount

VAR CurrYearCount = CALCULATE
    (DISTINCTCOUNT('SIA Interact'[Finding Title]),
    Filter('SIA Interact', 'SIA Interact'[Year]= CurrYear), 'SIA Interact'[Finding Rating]= "Major"
)
VAR PrevYearCount = CALCULATE // This is where something breaks down - this var doesn't return
    (DISTINCTCOUNT('SIA Interact'[Finding Title]),
    Filter('SIA Interact', 'SIA Interact'[Year]= PrevYear), 'SIA Interact'[Finding Rating]= "Major"
)
RETURN
    DIVIDE(CurrYearCount - PrevYearCount, PrevYearCount)
 
Any help would be greatly, greatly appreciated. I've been trying to get this for a few days now and haven't had any luck, I'm relatively new to PowerBI and DAX and I know I could just manually calculate this for the 2 years it matters but I want to be able to have it automated so when the dataset is updated over the years it will still work. Thank you!
1 ACCEPTED SOLUTION
FreemanZ
Super User
Super User

hi @lhanson1 

 

try like:

 

% Change in Majors =
VAR CurrYear = SELECTEDVALUE('SIA Interact'[Year]) // Year slicer input
VAR CompAmount = IF(CurrYear = 2020, 0, IF(CurrYear = 2022, 2, 1)) // To account for missing years/gaps
VAR PrevYear = CurrYear - CompAmount
 
VAR CurrYearCount = CALCULATE
    (DISTINCTCOUNT('SIA Interact'[Finding Title]),
    Filter('SIA Interact', 'SIA Interact'[Year]= CurrYear), 'SIA Interact'[Finding Rating]= "Major"
)
VAR PrevYearCount = CALCULATE // This is where something breaks down - this var doesn't return
    (DISTINCTCOUNT('SIA Interact'[Finding Title]),
    Filter(ALL('SIA Interact'), 'SIA Interact'[Year]= PrevYear), 'SIA Interact'[Finding Rating]= "Major"
)
RETURN
    DIVIDE(CurrYearCount - PrevYearCount, PrevYearCount)

 

View solution in original post

3 REPLIES 3
FreemanZ
Super User
Super User

hi @lhanson1 

 

try like:

 

% Change in Majors =
VAR CurrYear = SELECTEDVALUE('SIA Interact'[Year]) // Year slicer input
VAR CompAmount = IF(CurrYear = 2020, 0, IF(CurrYear = 2022, 2, 1)) // To account for missing years/gaps
VAR PrevYear = CurrYear - CompAmount
 
VAR CurrYearCount = CALCULATE
    (DISTINCTCOUNT('SIA Interact'[Finding Title]),
    Filter('SIA Interact', 'SIA Interact'[Year]= CurrYear), 'SIA Interact'[Finding Rating]= "Major"
)
VAR PrevYearCount = CALCULATE // This is where something breaks down - this var doesn't return
    (DISTINCTCOUNT('SIA Interact'[Finding Title]),
    Filter(ALL('SIA Interact'), 'SIA Interact'[Year]= PrevYear), 'SIA Interact'[Finding Rating]= "Major"
)
RETURN
    DIVIDE(CurrYearCount - PrevYearCount, PrevYearCount)

 

This seems to work, thank you so so much! Why is the All() necessary, and only in one variable?

hi @lhanson1 

Your year selection itself is a kind of filter context and ALL() is to remove such filter context.
Otherwise, it calculates something when year = 2020 and year = 2022, which returns blank.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.