Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
calcstuff
Regular Visitor

How do I combine Group_By and Lag?

EmployeeSalesLagLagGroup
Billy1  
Billy211
Billy322
Bobby53 
Bobby1055
Bobby151010

 

I know it's fairly easy to generate the Lag column shown above, based off of the Sales column. Instead, can I first group by Employee and then add the custom LagGroup column, as shown above? If you notice, Bobby's first LagGroup is blank, as the intent is to group by employee, evaluate lags, and not pull in lag values from other employees. What power query or what DAX do I need to do? This is all done in a dataflow if that matters.

1 ACCEPTED SOLUTION
v-yinliw-msft
Community Support
Community Support

Hi @calcstuff ,

 

You can try this method:

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

vyinliwmsft_0-1669183363829.png

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:

vyinliwmsft_1-1669183474664.png

 

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.

 

 

View solution in original post

1 REPLY 1
v-yinliw-msft
Community Support
Community Support

Hi @calcstuff ,

 

You can try this method:

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

vyinliwmsft_0-1669183363829.png

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:

vyinliwmsft_1-1669183474664.png

 

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.

 

 

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors