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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
akowalski
Frequent Visitor

Measure calculating difference to previous period

Hello,

I have a problem calculating a measure that calculates difference to previous period(year). Years are selected in a slicer (generally 2 but would be great if the code worked with more periods). This code works for 2 latest year, but is not working even with 2 following years in the middle. For years with a gap (2015 and 2017) chart also shows year 2016.

 

I cannot find a way to adjust the code to work with the previous period selected from a slicer, not hardcoded.

 

Thank you for your help!

 

Penetration % YoY% = 
	VAR __PREV_YEAR =
		CALCULATE(
			SUM('output_ALL_2018p03'[Penetration %]);
			DATEADD('output_ALL_2018p03'[Date].[Date]; -1; YEAR)
		)
	RETURN
IF(SUM('output_ALL_2018p03'[Penetration %]) - __PREV_YEAR = 
    SUM('output_ALL_2018p03'[Penetration %]);BLANK();SUM('output_ALL_2018p03'[Penetration %]) - __PREV_YEAR )
1 ACCEPTED SOLUTION

Hi akowalski ,

 

In your DAX formula, Min() and Max() are calculated in a same row context which have only one row every iteration, so the filter result is nothing. The aggregation function like Min(), Max() or Sum() should be calculated in every filter context.

 

Regards,

Jimmy Tao

View solution in original post

3 REPLIES 3
v-yuta-msft
Community Support
Community Support

Hi akowalski,

 

Here is a general solution based on your description for your reference.

 

To sum up values based on selected date in slicer, create a calculate column using DAX formula like this:

Date2Year =
YEAR ( 'output_ALL_2018p03'[Date] )

 

Then modify your first measure like below:

VAR __PREV_YEAR =
CALCULATE (
    SUM ( 'output_ALL_2018p03'[Penetration %] ),
    ALLSELECTED ( 'output_ALL_2018p03[Date2Year]' )
)

Regards,

Jimmy Tao

 

I tried to solve the problem by adding a measure calculating difference between max and min year.

Diff = MIN(output_ALL_2018p03[Date2Year]) - MAX(output_ALL_2018p03[Date2Year])

It is calculaing properly, but when i i try to add this variable into DateAdd function or even Switch and compare it to numbers (-1,-2,-3) it is not working at all in Switch and in Dateadd it is showing line of zeros.

CALCULATE(SUM('output_ALL_2018p03'[Penetration %]);DATEADD('output_ALL_2018p03'[Date].[Date]; Diff; YEAR))

Hi akowalski ,

 

In your DAX formula, Min() and Max() are calculated in a same row context which have only one row every iteration, so the filter result is nothing. The aggregation function like Min(), Max() or Sum() should be calculated in every filter context.

 

Regards,

Jimmy Tao

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.