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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
danisharoon
Helper I
Helper I

PowerBI PowerQuery M - Lag for each group

I have a query consisting of 3 columns, category, month, and volume. I would like to determine one month lag of volume for each category. How can this be achieved using PowerBI PowerQuery M?

2 ACCEPTED SOLUTIONS
Vijay_A_Verma
Super User
Super User

Please post your sample data and result expected. Refer to this - How to provide sample data in the Power BI Forum - https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...

View solution in original post

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcknMLKpUKCjKTylNLilW0lHySswDkuZmSrE6WGTdUpOApLEpdlnfxCKQrAl2WccCkKwRRNYptSy1KDE9NUUhpSgzLxths4kpLnmo3Tj1Q2w3w6kfYr+xpVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, Month = _t, Volume = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Month", type text}, {"Volume", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Volume (lag 1)", each try if #"Added Index"[Category]{[Index]-1}=[Category] then #"Added Index"[Volume]{[Index]-1} else null otherwise null),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([#"Volume (lag 1)"] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Volume", "Index"})
in
    #"Removed Columns"

 

View solution in original post

3 REPLIES 3
danisharoon
Helper I
Helper I

Input will be as follows:

 

CategoryMonthVolume
Dairy productsJan76
Dairy productsFeb35
Dairy productsMar34
Dairy productsApr24
Beveraged drinksJan45
Beveraged drinksFeb34
Beveraged drinksMar65
Beveraged drinksApr39

 

Output to be as follows:

 

CategoryMonthVolume (lag 1)
Dairy productsFeb76
Dairy productsMar35
Dairy productsApr34
Beveraged drinksFeb45
Beveraged drinksMar34
Beveraged drinksApr65

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcknMLKpUKCjKTylNLilW0lHySswDkuZmSrE6WGTdUpOApLEpdlnfxCKQrAl2WccCkKwRRNYptSy1KDE9NUUhpSgzLxths4kpLnmo3Tj1Q2w3w6kfYr+xpVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, Month = _t, Volume = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Month", type text}, {"Volume", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Volume (lag 1)", each try if #"Added Index"[Category]{[Index]-1}=[Category] then #"Added Index"[Volume]{[Index]-1} else null otherwise null),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([#"Volume (lag 1)"] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Volume", "Index"})
in
    #"Removed Columns"

 

Vijay_A_Verma
Super User
Super User

Please post your sample data and result expected. Refer to this - How to provide sample data in the Power BI Forum - https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors