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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
d_sa
New Member

insert rows from other table for each unique entry in a column of first table

Hello all,

First of all, I'm pretty new to power query/pbi, so this may be a bit of a dumb question, but I searched and couldn't find an answer, so here it goes:

 

I have loaded 3 queries. 1st one merges several monthly files. Simplified, it has columns A for month, another one B with material type, another one with C materials and one D with production quantity1

 ABCD
1MonthM TypeMaterialProd Qtty
21T1M15
31T1M210
41T2M315
51T2M46
62T1M17
72T3M612
82T3M714
92T4M82

 

Then I have a second table with column A containing all the material types (B in table 1)

 A
1M Type
2T1
3T2
4T3
5T4
6T5

 

Finally a third query (again monthly files) with table with A month, B material type (B in table 1), and C scrap quantity (i cant relate scrap to materials, only to material types, at least at this point)

 ABC
1MonthM TypeScrap Qtty
21T11
31T22
41T31
52T12
62T21
72T41



I'd like to merge query 2 to query one, adding all the material types as rows in the first query for each month (all T1 to T5 even if they're zero), repeating material type as material in column C and quantity produced as zero. Then i'd get scrap quantity from query 3, but this i'd figure out easily, my issue is in adding all the lines from table 2 for each unique entry in table 1/column A (Final output expected below)

Thank you in advance!

 ABCDE
1MonthM TypeMaterialProd QttyScrap Qtty
21T1M150
31T1M2100
41T1T101
51T2M3150
61T2M460
71T2T202
81T3T301
91T4T400
101T5T500
112T1M170
122T1T102
132T2T201
142T3M6120
152T3M7140
162T3T300
172T4M820
182T4T401
192T5T500


 

 

2 ACCEPTED SOLUTIONS
wini_R
Resolver IV
Resolver IV

Hi @d_sa,

 

here's a not too sophisticated solution to your challenge but hopefully it works for you. I'm pretty sure it could be done in a more elegant way.

You can try to paste this code in the advanced editor in PQ to see it in action:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XcyxDQAgCETRXagtFEVdgs7OuP8acjaozU94AeakRIEGoojQCrexJUVHzJqB8mGx1GP8fGxuuNOKY/6wAYsjnmm32OLa", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"M Type" = _t, Material = _t, #"Prod Qtty" = _t]),
    tbl2 = #table({"M Type"}, {{"T1"}, {"T2"}, {"T3"}, {"T4"}, {"T5"}}),
    #"Grouped Rows" = Table.Group(Source, {"Month"}, {{"aaa", each _, type table [Month=nullable number, M Type=nullable text, Material=nullable text, Prod Qtty=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.FillDown(Table.Combine({[aaa], Table.AddColumn(tbl2, "Material", each [M Type]) }), {"Month"})),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Month", "M Type", "Material", "Prod Qtty"}, {"Month", "M Type", "Material", "Prod Qtty"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Custom",null,"0",Replacer.ReplaceValue,{"Prod Qtty"})
in
    #"Replaced Value"

That should be an output table:

wini_R_0-1730500532741.png

 

View solution in original post

p45cal
Resolver II
Resolver II

Same results as final table. Don't know if the algorithm is entirely correct:

 

p45cal_0-1730647191883.png

 

Here's the code - some tables stolen from @Resolver III :

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XcyxDQAgCETRXagtFEVdgs7OuP8acjaozU94AeakRIEGoojQCrexJUVHzJqB8mGx1GP8fGxuuNOKY/6wAYsjnmm32OLa", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"M Type" = _t, Material = _t, #"Prod Qtty" = _t]),
    SourceCT = Table.TransformColumnTypes(Source,{{"Month", Int64.Type}, {"Prod Qtty", type number}}),
    tbl2 = #table({"M Type"}, {{"T1"}, {"T2"}, {"T3"}, {"T4"}, {"T5"}}),
    Qry3 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQoBEYZKsTpQrhGQMEJwjeGyRjDFRgiuEaqsCYQbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"M Type" = _t, #"Scrap Qtty" = _t]),
    Qry3CT = Table.TransformColumnTypes(Qry3,{{"Month", Int64.Type}, {"Scrap Qtty", type number}}),
    DuplicatedColumn = Table.DuplicateColumn(tbl2, "M Type", "Material"),
    #"Grouped Rows" = Table.Group(SourceCT, {"Month"}, {{"grp", each _, type table [Month=nullable number, M Type=nullable text, Material=nullable text, Prod Qtty=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [grp] & DuplicatedColumn),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"grp"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"M Type", "Material", "Prod Qtty"}, {"M Type", "Material", "Prod Qtty"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Custom", each if [M Type]=[Material] then [M Type] else null),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom1", {"Month", "Custom"}, Qry3CT, {"Month", "M Type"}, "Qry3", JoinKind.LeftOuter),
    #"Expanded Qry3" = Table.ExpandTableColumn(#"Merged Queries", "Qry3", {"Scrap Qtty"}, {"Scrap Qtty"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded Qry3",{"Custom"}),
    #"Replaced Value" = Table.ReplaceValue(#"Removed Columns1",null,0,Replacer.ReplaceValue,{"Prod Qtty", "Scrap Qtty"}),
    #"Sorted Rows" = Table.Sort(#"Replaced Value",{{"Month", Order.Ascending}, {"M Type", Order.Ascending}})
in
    #"Sorted Rows"

 

 

View solution in original post

2 REPLIES 2
p45cal
Resolver II
Resolver II

Same results as final table. Don't know if the algorithm is entirely correct:

 

p45cal_0-1730647191883.png

 

Here's the code - some tables stolen from @Resolver III :

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XcyxDQAgCETRXagtFEVdgs7OuP8acjaozU94AeakRIEGoojQCrexJUVHzJqB8mGx1GP8fGxuuNOKY/6wAYsjnmm32OLa", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"M Type" = _t, Material = _t, #"Prod Qtty" = _t]),
    SourceCT = Table.TransformColumnTypes(Source,{{"Month", Int64.Type}, {"Prod Qtty", type number}}),
    tbl2 = #table({"M Type"}, {{"T1"}, {"T2"}, {"T3"}, {"T4"}, {"T5"}}),
    Qry3 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQoBEYZKsTpQrhGQMEJwjeGyRjDFRgiuEaqsCYQbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"M Type" = _t, #"Scrap Qtty" = _t]),
    Qry3CT = Table.TransformColumnTypes(Qry3,{{"Month", Int64.Type}, {"Scrap Qtty", type number}}),
    DuplicatedColumn = Table.DuplicateColumn(tbl2, "M Type", "Material"),
    #"Grouped Rows" = Table.Group(SourceCT, {"Month"}, {{"grp", each _, type table [Month=nullable number, M Type=nullable text, Material=nullable text, Prod Qtty=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [grp] & DuplicatedColumn),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"grp"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"M Type", "Material", "Prod Qtty"}, {"M Type", "Material", "Prod Qtty"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Custom", each if [M Type]=[Material] then [M Type] else null),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom1", {"Month", "Custom"}, Qry3CT, {"Month", "M Type"}, "Qry3", JoinKind.LeftOuter),
    #"Expanded Qry3" = Table.ExpandTableColumn(#"Merged Queries", "Qry3", {"Scrap Qtty"}, {"Scrap Qtty"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded Qry3",{"Custom"}),
    #"Replaced Value" = Table.ReplaceValue(#"Removed Columns1",null,0,Replacer.ReplaceValue,{"Prod Qtty", "Scrap Qtty"}),
    #"Sorted Rows" = Table.Sort(#"Replaced Value",{{"Month", Order.Ascending}, {"M Type", Order.Ascending}})
in
    #"Sorted Rows"

 

 

wini_R
Resolver IV
Resolver IV

Hi @d_sa,

 

here's a not too sophisticated solution to your challenge but hopefully it works for you. I'm pretty sure it could be done in a more elegant way.

You can try to paste this code in the advanced editor in PQ to see it in action:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XcyxDQAgCETRXagtFEVdgs7OuP8acjaozU94AeakRIEGoojQCrexJUVHzJqB8mGx1GP8fGxuuNOKY/6wAYsjnmm32OLa", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"M Type" = _t, Material = _t, #"Prod Qtty" = _t]),
    tbl2 = #table({"M Type"}, {{"T1"}, {"T2"}, {"T3"}, {"T4"}, {"T5"}}),
    #"Grouped Rows" = Table.Group(Source, {"Month"}, {{"aaa", each _, type table [Month=nullable number, M Type=nullable text, Material=nullable text, Prod Qtty=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.FillDown(Table.Combine({[aaa], Table.AddColumn(tbl2, "Material", each [M Type]) }), {"Month"})),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Month", "M Type", "Material", "Prod Qtty"}, {"Month", "M Type", "Material", "Prod Qtty"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Custom",null,"0",Replacer.ReplaceValue,{"Prod Qtty"})
in
    #"Replaced Value"

That should be an output table:

wini_R_0-1730500532741.png

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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