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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
yevhen_87
Frequent Visitor

Merging tables within query

In a table with columns like: period, id and value I need to merge values by id from a previous period with a recent period.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @yevhen_87 

Based on your description you can refetr to the following sample.

Sample data 

vxinruzhumsft_0-1730947776736.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtSz1DMyMDJR0lEyMDAEkoYGSrE6aOJGQNIIJm5ogKrBGJsESIcJTMICVYOJKaY4SL2xmVJsLAA=", 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,{{"period", type date}, {"id", Int64.Type}, {"value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let a=[id],
b=[period],
c=List.Max(Table.SelectRows(#"Changed Type",each [id]=a and [period]<b)[period])
in c),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"period", Order.Ascending}, {"id", Order.Ascending}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Sorted Rows",{{"Custom", type date}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type1", {"id", "Custom"}, #"Changed Type", {"id", "period"}, "Changed Type1", JoinKind.LeftOuter),
    #"Expanded Changed Type1" = Table.ExpandTableColumn(#"Merged Queries", "Changed Type1", {"value"}, {"value.1"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Changed Type1", "New Period", each if [Custom]<>null then Text.From([Custom])&"-"&Text.From([period]) else Text.From([period])),
    #"Inserted Sum" = Table.AddColumn(#"Added Custom1", "Addition", each List.Sum({[value.1], [value]}), Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Sum",{"Custom", "value.1"})
in
    #"Removed Columns"

 Ouptut

vxinruzhumsft_1-1730947835948.png

Best Regards!

Yolo Zhu

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

4 REPLIES 4
Anonymous
Not applicable

Hi @yevhen_87 

Based on your description you can refetr to the following sample.

Sample data 

vxinruzhumsft_0-1730947776736.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtSz1DMyMDJR0lEyMDAEkoYGSrE6aOJGQNIIJm5ogKrBGJsESIcJTMICVYOJKaY4SL2xmVJsLAA=", 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,{{"period", type date}, {"id", Int64.Type}, {"value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let a=[id],
b=[period],
c=List.Max(Table.SelectRows(#"Changed Type",each [id]=a and [period]<b)[period])
in c),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"period", Order.Ascending}, {"id", Order.Ascending}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Sorted Rows",{{"Custom", type date}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type1", {"id", "Custom"}, #"Changed Type", {"id", "period"}, "Changed Type1", JoinKind.LeftOuter),
    #"Expanded Changed Type1" = Table.ExpandTableColumn(#"Merged Queries", "Changed Type1", {"value"}, {"value.1"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Changed Type1", "New Period", each if [Custom]<>null then Text.From([Custom])&"-"&Text.From([period]) else Text.From([period])),
    #"Inserted Sum" = Table.AddColumn(#"Added Custom1", "Addition", each List.Sum({[value.1], [value]}), Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Sum",{"Custom", "value.1"})
in
    #"Removed Columns"

 Ouptut

vxinruzhumsft_1-1730947835948.png

Best Regards!

Yolo Zhu

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

 

yevhen_87
Frequent Visitor

Period values are in a format like 
1.9.2024
1.9.2024
1.10.2024
1.10.2024, etc.

Omid_Motamedise
Super User
Super User

What is the format of values in the period  column?


If my answer helped solve your issue, please consider marking it as the accepted solution.

My replies are not visible so I posted a comment to my post.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors