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
pelowski
Helper III
Helper III

Power Query subtable header concatenate and pivot

I have a lot of subtables within a grouped dataset I'm trying to flatten out into one row per table but I'm having trouble figuring out how to do it.

 

Essentially I have a bunch of tables that look similar to this...

Order TypeSKU Price 
New LicenseProduct01$30.00
UpgradeProduct01U$20.00
MaintenanceProduct01M$10.00

 

And I would like to flatten them out to look like this...

New License
SKU
New License
Price
Upgrade
SKU
Upgrade
Price
Maintenance
SKU
Maintenance
Price
Product01$30.00Product01U$20.00Product01M$10.00

 

I can't figure out the order of operations to use to make this transformation.  Any help would be appreciated.

1 ACCEPTED SOLUTION
v-danhe-msft
Employee
Employee

Hi @pelowski ,

You could refer to below code in advanced editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8i9KSS1SCKksSFXSUQr2DgWSCgFFmcmpCkqxOtFKfqnlCj5AXl4xSD6gKD+lNLnEwBDIVjE20DMwACsKLUgvSkxBUQAyR8UIrsI3MTOvJDUvMS8ZRZUvSJUhRFUsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Type", type text}, {"SKU", type text}, {" Price ", Currency.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Order Type"}, "Attribute", "Value"),
    #"Transposed Table" = Table.Transpose(#"Unpivoted Other Columns"),
    #"Promoted Headers1" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers1",{{"New License", type text}, {"New License_1", type any}, {"Upgrade", type text}, {"Upgrade_2", type any}, {"Maintenance", type text}, {"Maintenance_3", type any}})
in
    #"Changed Type2"

Result:

1.PNG

You could also download the pbix file to have a view.

 

Regards,

Daniel He

Community Support Team _ Daniel He
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

2 REPLIES 2
v-danhe-msft
Employee
Employee

Hi @pelowski ,

You could refer to below code in advanced editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8i9KSS1SCKksSFXSUQr2DgWSCgFFmcmpCkqxOtFKfqnlCj5AXl4xSD6gKD+lNLnEwBDIVjE20DMwACsKLUgvSkxBUQAyR8UIrsI3MTOvJDUvMS8ZRZUvSJUhRFUsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Type", type text}, {"SKU", type text}, {" Price ", Currency.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Order Type"}, "Attribute", "Value"),
    #"Transposed Table" = Table.Transpose(#"Unpivoted Other Columns"),
    #"Promoted Headers1" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers1",{{"New License", type text}, {"New License_1", type any}, {"Upgrade", type text}, {"Upgrade_2", type any}, {"Maintenance", type text}, {"Maintenance_3", type any}})
in
    #"Changed Type2"

Result:

1.PNG

You could also download the pbix file to have a view.

 

Regards,

Daniel He

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you!  The Table.UnpivotOtherColumns is exactly what I needed.

 

My request was a little ambiguous as far as the "SKU" and "Price" parts being on the next line (I just wanted the column headers separated by a line return) but I just added a Table.CombineColumns with an #(lr) separator and now I have exactly what I want, one header row and one detail row of data.  I'm going to turn this into a function to iterate across all the subtables in the master table.

 

Thanks again for your help!

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.