Forum Discussion
DAX for SAMEPERIODLASTYEAR
Hi ,
I am trying to calculate Previous years value using SAMEPERIODLASTYEAR function in DAX. But i eed the data to be filtered based on current year's record. We have a CompFlag column in the table. So basically, I want a measure that when I enter date as 2019-01-15 and the CompFlag=1(meaning there is data for 2018-01-15) returns data for 2018-01-15. But when i apply the below measure, it returns me data from previous year but it looks at the CompFlag from last year as well. So lets say that for 2019-01-15 has some data and CompFlag=1 and thereis data for 2018-01-15 and CompFlag for that date(2018) is 0, then I would expect some data to be returend but instead ot filters out that record coz it looking at the wrong CompFlag.
Here is the measure:
CALCULATE(SUMX(FILTER('1 - Table1', '1 - Table1[ComparableFlag] = TRUE),'1 - Table1'[SomeValue]),SAMEPERIODLASTYEAR(Date[Date])).
So my question is how can I accomplish that? Below is the expected outcome:
Date Value CompFlag
2019-01-15 $23 1
2018-01-15 11 0
Expected Result: $11
Current Result: BLANK
Basically what I would like to do is first filter the data for allCompFlag=1 and then apply the SAMEPERIODLASTYEAR to that dataset but I dont know a way to do that like some kind of temp tables like we have in SQL. Thank you in advance for your help and suggestions.
Hi Anonymous ,
Could you have set a calender table for your data model? Based on my test, you could create a calender table and related your two table:
Then create the measure:
a = CALCULATE(SUM('1 - Table1'[Value]),SAMEPERIODLASTYEAR('Calender'[Date]))Result:
You may refer to below link about the function:
https://docs.microsoft.com/en-us/dax/sameperiodlastyear-function-dax
And download the pbix to have a view.
Regards,
Daniel He
1 Reply
- v-danhe-msft
Microsoft Employee
Hi Anonymous ,
Could you have set a calender table for your data model? Based on my test, you could create a calender table and related your two table:
Then create the measure:
a = CALCULATE(SUM('1 - Table1'[Value]),SAMEPERIODLASTYEAR('Calender'[Date]))Result:
You may refer to below link about the function:
https://docs.microsoft.com/en-us/dax/sameperiodlastyear-function-dax
And download the pbix to have a view.
Regards,
Daniel He