Search code examples
excelexcel-formulapowerbipowerquerypowerbi-desktop

How to split values/text from two column format with reference to a column into multiple columns?


Here is the data:

Old Material Number New Material Number
1 110
1 120
1 130
2 210
2 220
3 310

Desired Output

Old Material Number New Material Number 1 New Material Number 2 New Material Number 3
1 110 120 130
2 210 220
3 310

I tried using Group By function in the Power query but was unable to create additional columns to transform the data. Please guide on this.


Solution

  • Alternate method that is not hard coding number of columns

    let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Old Material Number"}, {{"data", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type),type table}}),
    #"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"New Material Number", "Index"}, {"New Material Number", "Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded data", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded data", {{"Index", type text}}, "en-US")[Index]), "Index", "New Material Number")
    in  #"Pivoted Column"
    

    enter image description here