Forum Discussion

Fromit87's avatar
Fromit87
Advocate I
3 years ago
Solved

Calculate the difference in values whenever year changes

Hello!

 

For every change in Store_ID, Metric_Name I need the difference of Goal_Value between every year and the respective next year.

 

Example: Store_ID = 1, Metric_Name = A, 2024 (vs. 2023) = 3%

 

I have the following table structure:

Store_IDMetric_NameGoal_ValueYear
1A77%2023
1A80%2024
1A88%2025
1B5%2023
1B8%2024
1B12%2025
2A60%2023
2A67%2023
2A72%2023
2C50%2023
2C40%2024
2C30%2025

 

A measure would be my prefered solution, but if it's a calculated column, that would be fine too.

Thank you in advance!

  • Fromit87 , it better to have a separate year table, join with your table and try

     


    //Only year vs Year, not a level below

    This Year = CALCULATE(sum('Table'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])))
    Last Year = CALCULATE(sum('Table'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])-1))
    diff = [This Year]-[Last Year ]
    diff % = divide([This Year]-[Last Year ],[Last Year ])

     

     

    or try like, Assume Goal_Value is Measure, using offset for previous year

     

    LastYear = CALCULATE([Goal_Value], OFFSET(-1, ALLSELECTED(Table[Store_ID],Table[Metric_Name],Table[Year]), ORDERBY(Table[Year],asc),KEEP,PARTITIONBY(Table[Store_ID],Table[Metric_Name])))

1 Reply

  • Fromit87 , it better to have a separate year table, join with your table and try

     


    //Only year vs Year, not a level below

    This Year = CALCULATE(sum('Table'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])))
    Last Year = CALCULATE(sum('Table'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])-1))
    diff = [This Year]-[Last Year ]
    diff % = divide([This Year]-[Last Year ],[Last Year ])

     

     

    or try like, Assume Goal_Value is Measure, using offset for previous year

     

    LastYear = CALCULATE([Goal_Value], OFFSET(-1, ALLSELECTED(Table[Store_ID],Table[Metric_Name],Table[Year]), ORDERBY(Table[Year],asc),KEEP,PARTITIONBY(Table[Store_ID],Table[Metric_Name])))