Forum Discussion

aury_lola's avatar
aury_lola
Frequent Visitor
1 month ago
Solved

Create measure to calculate the difference in values between latest date and previous date

Hi everyone,   I need some help. I've already tried different things, including asking LLM, but without any success, so I'm here to ask smarter people for help.   I'm trying to create a measure t...
  • Zanqueta's avatar
    1 month ago

    Hi  Looking at the screenshot, I suspect the values in the screenshot were simplified for illustration

    If the real expected result for Entity 1 is indeed 7, then there is some additional business logic missing from the example.

     

    Difference Latest vs Previous =
    VAR LatestDate =
    CALCULATE (
    MAX ( Table1[Date] ),
    ALLEXCEPT ( Table1, Table1[Entity name] )
    )

    VAR PreviousDate =
    CALCULATE (
    MAX ( Table1[Date] ),
    ALLEXCEPT ( Table1, Table1[Entity name] ),
    Table1[Date] < LatestDate
    )

    VAR LatestValue =
    CALCULATE (
    SUM ( Table2[Value] ),
    Table1[Date] = LatestDate
    )

    VAR PreviousValue =
    CALCULATE (
    SUM ( Table2[Value] ),
    Table1[Date] = PreviousDate
    )

    RETURN
    LatestValue - PreviousValue

     

     

  • aury_lola's avatar
    aury_lola
    1 month ago

    Hi Zanqueta , thanks for replying.

    Yes I have simplified the data because in reality I have about 300 entities for each date, and 6 dates showing every time. But the logic is the same.

     

    I ended up realising I was using the wrong table in my sources (yes I have the same data in 2 different places...) and I made it work with one of the formulas I was trying previously. Thanks anyway! Your solution worked as well.

     

    This is what I ended up using:

     

    ValueDifference =
    VAR LatestDate =
    CALCULATE(
    MAX('Table1'[Date]),
    ALL('Table1')
    )

    VAR PreviousDate =
    CALCULATE(
    MAX('Table1'[Date]),
    FILTER(
    ALL('Table1'),
    'Table1'[Date] < LatestDate
    )
    )

    VAR LatestValue =
    CALCULATE(
    SUM('Table2'[Value]),
    'Table1'[Date] = LatestDate
    )

    VAR PreviousValue =
    CALCULATE(
    SUM('Table2'[Value]),
    'Table1'[Date] = PreviousDate
    )

    RETURN
    LatestValue - PreviousValue