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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anthony_Nguyen
New Member

Duplicate rows based on the values from a table

Hi all, 

I am looking for Power Query solution for the task below (Duplicate rows based on the values from a table)

Row to be duplicated 

 

NoTypeCost
1E11
2E22

 

Look up table 

 

TypeCode
E1A
E1B
E2C
E2D
E2

E

 

Expected results

 

 

NoTypeCostCode
1E11A
1E11B
2E22C
2E22D
2E22E

Thank you in advance for your help

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

Hello, @Anthony_Nguyen 

let
    data = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIFEYZKsTrRSkYgLogwUoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [No = _t, Type = _t, Cost = _t]),
    lookup_table = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjVU0lFyVIrVgTKdIEwjINMZwXRBMF2VYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Type = _t, Code = _t]),
    lookup_gr = Table.Group(lookup_table, {"Type"}, {{"Value", each _}}),
    lookup_record = Record.FromTable(Table.RenameColumns(lookup_gr, {"Type", "Name"})),
    join = Table.AddColumn( data, "lookup", each Record.FieldOrDefault( lookup_record, [Type], #table({"Code"}, {{"not found"}}))),
    expand = Table.ExpandTableColumn(join, "lookup", {"Code"}, {"Code"})
in
    expand

View solution in original post

2 REPLIES 2
AlienSx
Super User
Super User

Hello, @Anthony_Nguyen 

let
    data = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIFEYZKsTrRSkYgLogwUoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [No = _t, Type = _t, Cost = _t]),
    lookup_table = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjVU0lFyVIrVgTKdIEwjINMZwXRBMF2VYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Type = _t, Code = _t]),
    lookup_gr = Table.Group(lookup_table, {"Type"}, {{"Value", each _}}),
    lookup_record = Record.FromTable(Table.RenameColumns(lookup_gr, {"Type", "Name"})),
    join = Table.AddColumn( data, "lookup", each Record.FieldOrDefault( lookup_record, [Type], #table({"Code"}, {{"not found"}}))),
    expand = Table.ExpandTableColumn(join, "lookup", {"Code"}, {"Code"})
in
    expand

It works like Charm. Thank you so much.

I am trying now to test with more complex data table

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors