Forum Discussion

Newbie992's avatar
Newbie992
Regular Visitor
6 years ago
Solved

How to get the difference between 2 rows in a column for category

I want to get the difference between the successive rows in a column(Confirmed_cases). I have indexed the columns by category but can't go further. Please help.

  • parry2k's avatar
    parry2k
    6 years ago

    Newbie992 try this

     

    Diff = 
    VAR __index = SELECTEDVALUE ( 'Table'[Index] ) 
    VAR __prevIndex = __index - 1
    VAR __prevValue = 
    CALCULATE ( 
        SUM ( 'Table'[Confirmed_cases] ), 
        ALLEXCEPT ( 'Table', 'Table'[Location_Key] ), 
        'Table'[Index] = __prevIndex 
    ) 
    RETURN
    SUM ( 'Table'[Confirmed_cases] ) - __prevValue

     

    I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

6 Replies

  • Newbie992 try this measure

     

    Diff =
    VAR __index = SELECTEDVALUE ( Table[Index ) 
    VAR __prevIndex = __index - 1
    VAR __country = SELECTEDVALUE Table[Country] )
    VAR __prevValue = CALULATE ( SUM ( Table[Cases] ), Table[Index] = __prevIndex, Table[Country] = __country )
    RETURN
    SUM ( Table[Cases] ) - __prevValue
    

     

    I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

      

    • Newbie992's avatar
      Newbie992
      Regular Visitor

      Hi,

      Thanks for your reply!

      It's not working for me. In the result, I get the same values that are in Confirmed_cases column. I checked and in the variable _prevValue I am not getting any values.

       

      Thanks!

      • parry2k's avatar
        parry2k
        Icon for Super User rankSuper User

        Newbie992 do you getting value in the _index variable. Can you paste data in a table format so that I can copy it? I probably missed something, I pasted the expression from my phone and was a bit hard to type.