Forum Discussion
How to create a calculation group for YoY calculation( 2 measures without using sameperiodlastyear )
Hi Community,
I have 2 measures called TY placed sales and LY placed sales created using base columns TY sales and LY sales.
I created a measure like below and it is working as expected.
Var a = [TY Placed Sales]
Var b = [LY Placed Sales]
return
IF(ISBLANK(a) || a <=0 || b<=0 , BLANK() , DIVIDE(a-b,b))
You need to pass in the measure to consider using selectedmeasure(), so need something like
Var maxDt = max( date[date] ) Var thisYear= Year( maxDt ) Var a = Calculate( Selectedmeasure(), Datesbetween( dates[Date], date( thisYear, 1, 1 ), maxDt ) ) Var b = Calculate( Selectedmeasure(), DATEADD(Datesbetween( dates[Date], date( thisYear, 1, 1 ), maxDt ), -12, month) ) return IF(ISBLANK(a) || a <=0 || b<=0 , BLANK() , DIVIDE(a-b,b))
6 Replies
- Deku
Super User
You need to pass in the measure to consider using selectedmeasure(), so need something like
Var maxDt = max( date[date] ) Var thisYear= Year( maxDt ) Var a = Calculate( Selectedmeasure(), Datesbetween( dates[Date], date( thisYear, 1, 1 ), maxDt ) ) Var b = Calculate( Selectedmeasure(), DATEADD(Datesbetween( dates[Date], date( thisYear, 1, 1 ), maxDt ), -12, month) ) return IF(ISBLANK(a) || a <=0 || b<=0 , BLANK() , DIVIDE(a-b,b)) - Gokul_G16
Resolver I
Hi Anonymous ,
Please try the following code,
YoY = VAR CurrentYear = YEAR(SELECTEDVALUE('Date'[Date])) VAR PreviousYear = CurrentYear - 1 RETURN CALCULATE( SELECTEDMEASURE(), FILTER( ALL('Date'), 'Date'[Year] = PreviousYear ) )
For YoY%, use this DAX expression: YoY% = VAR CurrentYear = YEAR(SELECTEDVALUE('Date'[Date])) VAR PreviousYear = CurrentYear - 1 VAR CurrentValue = SELECTEDMEASURE() VAR PreviousValue = CALCULATE( SELECTEDMEASURE(), FILTER( ALL('Date'), 'Date'[Year] = PreviousYear ) ) RETURN DIVIDE(CurrentValue - PreviousValue, PreviousValue, 0)
Regards,
Gokul,
If you good with the answer do like me and share your thougths if you not find well... - rohit1991
Super User
Hi Anonymous
Yes you can create a calculation group in Tabular Editor to apply YoY logic across multiple measures like Sales and Margin. Could you please try below steps
-
In Tabular Editor, right-click on Tables → Create New Calculation Group
-
Add the following items:
Current
SELECTEDMEASURE()YoY
VAR Current = SELECTEDMEASURE() VAR LastYear = CALCULATE(Current, SAMEPERIODLASTYEAR('Date'[Date])) RETURN Current - LastYearYoY % (optional)
VAR Current = SELECTEDMEASURE() VAR LastYear = CALCULATE(Current, SAMEPERIODLASTYEAR('Date'[Date])) RETURN DIVIDE(Current - LastYear, LastYear)Make sure your 'Date' table is marked as a proper date table and connected to your fact table. Once set up, the calculation group will apply these logic items dynamically to any measure.
-
- v-sgandrathi
Community Support
Hi Anonymous,
Has your issue been resolved? If the solution provided by the community member effectively addressed your concern, kindly confirm.
We encourage you to mark the response as Accepted Answer and provide Kudos if you found it helpful. This helps us ensure that valuable solutions are shared with the entire community.
Should you have any further questions, please don't hesitate to reach out.
Thank you for your understanding.
- v-sgandrathi
Community Support
Hi Anonymous,
May I ask if you have gotten this issue resolved?
If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.
Thank you.- v-sgandrathi
Community Support
Hi Anonymous,
I wanted to check in your situation regarding the issue. Have you resolved it? If you have, please consider marking the reply as Accepted solution and give Kudos that helped you. It would be greatly appreciated by others in the community who may have the same question.
Thank you.