Forum Discussion

pdbenbow's avatar
pdbenbow
Icon for Resolver II rankResolver II
6 years ago
Solved

DAX Time Intelligence - last year's data summing incorrectly at the year level

I've spent the better half of today trying a variety of methods and looking through "The Definitive Guide to DAX," and I can't figure out what I'm doing wrong. I have a Power BI report that is suppo...
  • pdbenbow's avatar
    pdbenbow
    6 years ago

    I finally figured it out.  My hunch that it was caused by the leap year proved to be true.  In the screenshot below, you can see the correct numbers, plus two additional columns:

     

    "test2" shows the results of using the original DATE(YEAR(MAX([Date]))-1,MONTH(MAX([Date])), DAY(MAX([Date]))) variable. You can see how the final date of February 2019 is calculating as "3/1/2019" because Feb 2020 has 29 days instead of the usual 28.

    "test3" shows the results of a new modified measure that takes the 29th day of Feb into account.

     

    Thanks to everyone who tried to help.  This just took a while for me to figure out.

    Here's the final DAX for the "Previous Year Actual" and "Previous Year Actual YTD" measures:

    Previous Year Actual:=
    VAR LastDayOfMonth = IF(DAY(MAX('Fiscal Date'[Fiscal Date]))=29,DAY(MAX('Fiscal Date'[Fiscal Date]))-1,DAY(MAX('Fiscal Date'[Fiscal Date])))
    VAR Previous_Year = DATE(YEAR(MAX('Fiscal Date'[Fiscal Date])) - 1,  MONTH(MAX('Fiscal Date'[Fiscal Date])), LastDayOfMonth )
    return
    IF(HASONEVALUE('Fiscal Period'[Month]), CALCULATE( [Actual], FILTER(ALL('Fiscal Date'), 'Fiscal Date'[Fiscal Date] = Previous_Year)), CALCULATE( [Actual YTD], FILTER(ALL('Fiscal Date'), 'Fiscal Date'[Fiscal Date] = Previous_Year)))
    
    Previous Year Actual YTD:=
    VAR LastDayOfMonth = IF(DAY(MAX('Fiscal Date'[Fiscal Date]))=29,DAY(MAX('Fiscal Date'[Fiscal Date]))-1,DAY(MAX('Fiscal Date'[Fiscal Date])))
    VAR Previous_Year = DATE(YEAR(MAX('Fiscal Date'[Fiscal Date])) - 1,  MONTH(MAX('Fiscal Date'[Fiscal Date])), LastDayOfMonth )
    RETURN
    CALCULATE( [Actual YTD], FILTER(ALL('Fiscal Date'), 'Fiscal Date'[Fiscal Date] = Previous_Year))