Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

How do I combine Group_By and Lag?

Employee Sales Lag LagGroup Billy 1     Billy 2 1 1 Billy 3 2 2 Bobby 5 3   Bobby 10 5 5 Bobby 15 10 10   I know it's fairly easy to generate the Lag co...
  • v-yinliw-msft's avatar
    3 years ago

    Hi Anonymous ,

     

    You can try this method:

    In the Power Query, you can add a index column:

    Then new a column:

    LagGroup =
    VAR _index =
        CALCULATE (
            MAX ( 'Table'[Index] ),
            FILTER (
                'Table',
                [Employee] = EARLIER ( 'Table'[Employee] )
                    && [Index] < EARLIER ( 'Table'[Index] )
            )
        )
    RETURN
        CALCULATE ( MAX ( 'Table'[Sales] ), FILTER ( 'Table', [Index] = _index ) )
    

    The result is:

     

    Hope these help you.

    Here is my PBIX file.

     

    Best Regards,

    Community Support Team _Yinliw

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.