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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
yevhen_87
Frequent Visitor

Create column with values from a previous period

I have a table structure like this:

1.png
Is it possible to transform it to this:
2.png

 




Maybe even in one step?

1 ACCEPTED SOLUTION

"Everything is doable in Power Query" - but - does it make sense?  How will you guarantee that your raw data always has exactly two periods? Your IDs don't overlap completely - what if the older period has more IDs than the newer period?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XczBCQAgDEPRVSRnD21U0Fmk+68hAUHpIZcHP3uDxl5socI1IuqPFI6ETTgfut2cTKicnlA5R8Kui4WIAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [period = _t, id = _t, value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {{"period", each List.Max([period]), type nullable text}, {"rows", each _, type table [period=nullable text, id=nullable text, value=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "value", (k)=> List.Sum(Table.SelectRows(k[rows],each [period]=k[period])[value]),Int64.Type),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "prev. value", (k)=> List.Sum(Table.SelectRows(k[rows],each [period]<>k[period])[value]),Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"rows"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"period", "id", "value", "prev. value"})
in
    #"Reordered Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the entire Source step with your own source.

 

 

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

Use a matrix visual with the periods in the column area, ids in the rows area, and the value in the values area.

 

Use Visual Calculations if you need to compare the values.

Need to do this particularly in PQ if it's even doable.

"Everything is doable in Power Query" - but - does it make sense?  How will you guarantee that your raw data always has exactly two periods? Your IDs don't overlap completely - what if the older period has more IDs than the newer period?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XczBCQAgDEPRVSRnD21U0Fmk+68hAUHpIZcHP3uDxl5socI1IuqPFI6ETTgfut2cTKicnlA5R8Kui4WIAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [period = _t, id = _t, value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {{"period", each List.Max([period]), type nullable text}, {"rows", each _, type table [period=nullable text, id=nullable text, value=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "value", (k)=> List.Sum(Table.SelectRows(k[rows],each [period]=k[period])[value]),Int64.Type),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "prev. value", (k)=> List.Sum(Table.SelectRows(k[rows],each [period]<>k[period])[value]),Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"rows"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"period", "id", "value", "prev. value"})
in
    #"Reordered Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the entire Source step with your own source.

 

 

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.