Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

How to find difference between two rows of data?

I have data as shown in a small snippet below. I would like to add a new column that shows the difference from one row to the next. Can anyone show me the DAX code to do this?

 

 

  • johnt75's avatar
    johnt75
    4 years ago

    My mistake, I changed my mind about where to put the check for 0 index whilst I was writing it.

    Diff to previous = 
    var currentIndex = 'Table'[index]
    IF( currentIndex > 0,
    var currentValue = 'Table'[Value]
    var prevValue = LOOKUPVALUE( 'Table'[Value], 'Table'[index], currentIndex - 1)
    return currentValue - prevValue
    )

3 Replies

  • Diff to previous = IF( currentIndex > 0,
    var currentIndex = 'Table'[index]
    var currentValue = 'Table'[Value]
    var prevValue = LOOKUPVALUE( 'Table'[Value], 'Table'[index], currentIndex - 1)
    return currentValue - prevValue
    )
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi johnt75 ,

       

      Tried your code but currentIndex on line 1 is greyed out and gives the error "Failed to resolve name 'currentIndex'. Rest of the lines seem to work fine. Any idea how to fix this?

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

        My mistake, I changed my mind about where to put the check for 0 index whilst I was writing it.

        Diff to previous = 
        var currentIndex = 'Table'[index]
        IF( currentIndex > 0,
        var currentValue = 'Table'[Value]
        var prevValue = LOOKUPVALUE( 'Table'[Value], 'Table'[index], currentIndex - 1)
        return currentValue - prevValue
        )