Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Need help in creating calculated column based on the same column previous row

Hi, I have a table as mentioned below without New Column, I want to create a calculated column as New Column on 1st row initial value as ZERO(for L1) by default, but second row that L2 40-0 (40 is t...
  • pankajnamekar25's avatar
    1 year ago

    Hello Anonymous 

     

    Power Query allows you to refer to previous rows, which is ideal here.

    Steps

    Sort your data by Dt and Level (or another stable ordering).

    1. Add an Index column.
    2. Add a custom column to compute the value based on the previous row.
    3. Use logic like

    Custome= if [Index] = 0 then 0

      else [Target] - #"Previous Step"{[Index]-1}[NewColumn]

     

     

    for DAX Measure

    New Measure =

    VAR CurrentIndex = MAX('Table'[Index])

    VAR PrevIndex = CurrentIndex - 1

    VAR PrevValue =

        CALCULATE(

            MAX('Table'[YourTarget]),

            FILTER('Table', 'Table'[Index] = PrevIndex)

        )

    RETURN

    [Target] - PrevValue

     

    Thanks,
     Pankaj Namekar | LinkedIn

    If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.