Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Subtracting values from the same column but different rows

Hi! 

 

I have an electricity meter that is measuring cumulatively but I need to calculate the difference, therefore, the electricity use each hour. All the electricity data is in one column and I need to, for example, calculate the difference between Row 2-Row 1 for all of the rows. Is there a way to do this in Power BI?

Many thanks for any help you can give me!

 

 

  • Hi Anonymous,

     

    Take below simple data as an example.

     

    To get the difference between rows via calculated column.

    diff =
    Table3[Value]
        - CALCULATE (
            SUM ( Table3[Value] ),
            FILTER ( Table3, Table3[Index] = EARLIER ( Table3[Index] ) - 1 )
        )

     

    To get difference between rows via measure.

    diff measure = 
    SELECTEDVALUE ( Table3[Value] )
        - CALCULATE (
            SUM ( Table3[Value] ),
            FILTER (
                ALLSELECTED ( Table3 ),
                Table3[Index]
                    = SELECTEDVALUE ( Table3[Index] ) - 1
            )
        )

     

    For more advice, please provide your sample data and desired output. How to Get Your Question Answered Quickly

     

    Best regards,

    Yuliana Gu

4 Replies

  • v-yulgu-msft's avatar
    v-yulgu-msft
    Microsoft Employee

    Hi Anonymous,

     

    Take below simple data as an example.

     

    To get the difference between rows via calculated column.

    diff =
    Table3[Value]
        - CALCULATE (
            SUM ( Table3[Value] ),
            FILTER ( Table3, Table3[Index] = EARLIER ( Table3[Index] ) - 1 )
        )

     

    To get difference between rows via measure.

    diff measure = 
    SELECTEDVALUE ( Table3[Value] )
        - CALCULATE (
            SUM ( Table3[Value] ),
            FILTER (
                ALLSELECTED ( Table3 ),
                Table3[Index]
                    = SELECTEDVALUE ( Table3[Index] ) - 1
            )
        )

     

    For more advice, please provide your sample data and desired output. How to Get Your Question Answered Quickly

     

    Best regards,

    Yuliana Gu

    • awoo_123's avatar
      awoo_123
      New Member

      Hi Yuliana, 

       

      If i want to make Row 1 - Row 2 to get the difference, how should the measure look like?

       

      Thank you in advance

    • tarunsachdeva's avatar
      tarunsachdeva
      New Member

      Hi, I tried this and getting error message...

       

      MdxScript(Model) (12,162) Calculation error in measure