Forum Discussion
Help with DAX - YOY
Hi
I tried to do count of current year and previous year for my next calculations. Below are the steps i did
Step 1. create the calendar table:
Hi,
In the previous year measure, try not to use REMOVEFILTERS DAX function, because it will remove all filters even users select year from year-slicer on the report.
Instead, try to use ALLSELECTED() DAX function and please check if it works. And then, we can start from there to optimize the DAX.REMOVEFILTERS function (DAX) - DAX | Microsoft Learn
ALLSELECTED function (DAX) - DAX | Microsoft Learn
total prev yr = VAR _currentyear = YEAR ( CALCULATE ( MAX ( data[RejectDate] ), ALLSELECTED () ) ) VAR _previousyr = MAXX ( FILTER ( ALL ( 'Calendar' ), 'Calendar'[Year] = _currentyear ), 'Calendar'[Year] ) VAR _count2 = CALCULATE ( [Count_NotApprov], 'Calendar'[Year] = _previousyr - 1 ) RETURN IF ( [Current Yr NotAppr], _count2 )
5 Replies
- Jihwan_KimSuper User
Hi,
In the previous year measure, try not to use REMOVEFILTERS DAX function, because it will remove all filters even users select year from year-slicer on the report.
Instead, try to use ALLSELECTED() DAX function and please check if it works. And then, we can start from there to optimize the DAX.REMOVEFILTERS function (DAX) - DAX | Microsoft Learn
ALLSELECTED function (DAX) - DAX | Microsoft Learn
total prev yr = VAR _currentyear = YEAR ( CALCULATE ( MAX ( data[RejectDate] ), ALLSELECTED () ) ) VAR _previousyr = MAXX ( FILTER ( ALL ( 'Calendar' ), 'Calendar'[Year] = _currentyear ), 'Calendar'[Year] ) VAR _count2 = CALCULATE ( [Count_NotApprov], 'Calendar'[Year] = _previousyr - 1 ) RETURN IF ( [Current Yr NotAppr], _count2 ) - ashi7uptHelper I
This issue might be due to the calculation of _currentyear. Present your logic, as this calculation year is not from the calendar table but from the data table.
For the calculation of the previous year count, we can also use the selectedvalue()total prev yr = VAR _currentyear = SELECTEDVALUE('Calendar'[Year]) VAR _previousyr = _currentyear - 1 VAR _count2 = CALCULATE ( [Count_NotApprov], 'Calendar'[Year] = _previousyr - 1 ) RETURN IF ( [Current Yr NotAppr], _count2 ) - pankajnamekar25Super User
Hello Marshmallow
Try these 2 measure
Current Year Not Approved
Current Yr NotAppr =
VAR _currentyear =
MAX ( 'Calendar'[Year] ) // get the selected year from calendar
RETURN
CALCULATE (
[Count_NotApprov],
'Calendar'[Year] = _currentyear
)Previous Year Not Approved
Prev Yr NotAppr =
VAR _currentyear =
MAX ( 'Calendar'[Year] )
RETURN
CALCULATE (
[Count_NotApprov],
'Calendar'[Year] = _currentyear - 1
)
If my response helped you, please consider clicking
Accept as Solution ✅ and giving it a Like 👍 – it helps others in the community too.
Thanks,
Connect with me on:
LinkedIn - danextianSuper User
Hi Marshmallow
It appears to be a problem with your _currentyear calculation. You're using the one from the fact table instead of from the calendar table. Change your variable to this:
CALCUALTE ( MAX ( 'Calendar'[Year] ), ALLSELECTED ( 'Calendar' ) ) - V-yubandi-msftCommunity Support
Hi Marshmallow ,
The response Jihwan_Kim , provided earlier matches your requirement. Using ALLSELECTED() keeps the slicer context, so the measure updates dynamically based on user actions. This avoids the fixed behavior from REMOVEFILTERS(), which removes all filters, including slicer choices.
Please review the updated logic and let me know if you need more details or help.
Thanks,
Yugandhar