Skip to content

Instantly share code, notes, and snippets.

@bjulius
Last active March 4, 2024 02:34
Show Gist options
  • Save bjulius/fffa738cd5e914a2bac88b56f28efc68 to your computer and use it in GitHub Desktop.
Save bjulius/fffa738cd5e914a2bac88b56f28efc68 to your computer and use it in GitHub Desktop.
Brian Julius Solution to CH-15 Transnformation
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
AddAttrib = Table.AddColumn(Source, "Attrib", each [
a = {"A".."Z"},
b = List.PositionOf( a, [Product Code]) + 1
][b]),
Group = Table.Group(AddAttrib, {"Product Code"}, {{"All", each _, type table [Product Code=text, Ship Date=datetime, Po number=number, Po Quantity=number, Attrib=number]}}),
AddWGIndex = Table.RemoveColumns( Table.AddColumn(Group, "WithinGroupIndex", each Table.AddIndexColumn( [All], "WGIndex", 1, 1)), "All"),
Expand = Table.ExpandTableColumn(AddWGIndex, "WithinGroupIndex", {"Ship Date", "Po number", "Po Quantity", "Attrib", "WGIndex"}, {"Ship Date", "Po number", "Po Quantity", "Attrib", "WGIndex"}),
UnpivotOther = Table.UnpivotOtherColumns(Expand, {"Attrib", "WGIndex"}, "Attribute.1", "Value"),
AddColumn1 = Table.AddColumn(UnpivotOther, "Pivoter", each if [Attribute.1] = "Product Code" then "Products" else [Attribute.1] & " " & Text.From( [WGIndex] )),
AddAttribute = Table.AddColumn(AddColumn1, "Attribute", each "Column" & Text.From( [Attrib] )),
RemoveCols = Table.Distinct( Table.RemoveColumns(AddAttribute,{"Attrib", "WGIndex", "Attribute.1"})),
Pivot = Table.Pivot(RemoveCols, List.Distinct(RemoveCols[Attribute]), "Attribute", "Value"),
Transpose = Table.Transpose(Pivot),
Promote = Table.PromoteHeaders(Transpose, [PromoteAllScalars=true]),
Headers = Table.ColumnNames(Promote),
Headers1 = List.Transform( List.Transform( List.Transform( List.Transform(Headers, each Text.Split(_, " ")), each List.Last(_)), each if _ = "Products" then "0" else _), each Number.From( _ )),
Headers2 = Table.ToColumns( Table.Sort( Table.AddColumn( Table.FromColumns( {Headers, Headers1}), "Column3", each
if Text.StartsWith( [Column1], "Pr") then 1 else
if Text.StartsWith( [Column1], "Sh") then 2 else
if Text.StartsWith( [Column1], "Po n") then 3 else
if Text.StartsWith( [Column1], "Po Q") then 4 else
99), {{"Column2", Order.Ascending}, {"Column3", Order.Ascending}})){0},
FinalTable = Table.ReorderColumns( Promote, Headers2 )
in
FinalTable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment