Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Calculate Year over Year comparison dynamically based on the [year] slicer

Hello All,   I have a question regarding dynamically calculate the year over year comparison.   Suppose I have a table that look like this:   Sales:           2019 2020 2021 2022 ...
  • tom480's avatar
    4 years ago

    Hi Anonymous ,

     

    There's a lot of ways to do this, here's one way:

     

    1) To use time intelligence measures in DAX you need a date table.  I created a basic one and marked as the date table using this code:

     

    TableDT =
    VAR MinYear = YEAR ( MIN ( Table1[Date] ) )
    VAR MaxYear = YEAR ( MAX ( Table1[Date] ) )
    RETURN
    ADDCOLUMNS (
    FILTER (
    CALENDARAUTO( ),
    AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
    ),
    "Month Name", FORMAT ( [Date], "mmmm" ),
    "Month Number", MONTH ( [Date] )
    )
     
    2)  Next I created three measures:
     
    2a) 
    Current Year = SUM(Table1[Value])
     
    2b) 
    Last Year = CALCULATE(
    [Current Year],
    SAMEPERIODLASTYEAR(Table1[Date])
    )
     
    2c)
     
    Chg Year = [Current Year] - [Last Year]
     
    That gives me these results:
     
    Matrix visual with year slicer
     
    Simple Date Table for example
     
    Here's the PBIX file if you want to review in your desktop application.  
     
    If you found this helpful, please mark as a solution so others can find it please!  Always glad to help!  Tom ðŸ˜€