Transforming a Table in Excel Power Query: Grouping and Splitting Rows Based on Unique Order IDs

aamaramaster

New Member
Joined
Oct 3, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi, I have an Excel table with three columns: Order ID, Names, and Event. The Order ID column contains duplicate values corresponding to different names and events. I need assistance in transforming this table using Power Query in Excel to achieve the following structure:

  1. Unique Order ID: Each Order ID should appear only once.
  2. Names Columns: For each Order ID, the names associated with it should be spread across multiple columns (e.g., Name 1, Name 2, Name 3, etc.).
  3. Events Columns: Similarly, for each Order ID, the events associated with it should be spread across multiple columns (e.g., Event 1, Event 2, Event 3, etc.).
Original Table:
Order IDNamesEvent
1JANMAC
1BerryMAC
1RickyFAN
2SaraSAN
2NadaSAN
3JasmineBAN

Desired Output:
Order IDName 1Name 2Name 3Event 1Event 2
1JANBerryRickyMACFAN
2SaraNadaSAN
3JasmineBAN
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi, I have an Excel table with three columns: Order ID, Names, and Event. The Order ID column contains duplicate values corresponding to different names and events. I need assistance in transforming this table using Power Query in Excel to achieve the following structure:

  1. Unique Order ID: Each Order ID should appear only once.
  2. Names Columns: For each Order ID, the names associated with it should be spread across multiple columns (e.g., Name 1, Name 2, Name 3, etc.).
  3. Events Columns: Similarly, for each Order ID, the events associated with it should be spread across multiple columns (e.g., Event 1, Event 2, Event 3, etc.).
Original Table:
Order IDNamesEvent
1JANMAC
1BerryMAC
1RickyFAN
2SaraSAN
2NadaSAN
3JasmineBAN

Desired Output:
Order IDName 1Name 2Name 3Event 1Event 2
1JANBerryRickyMACFAN
2SaraNadaSAN
3JasmineBAN
I do not want to use DAX if possible
 
Upvote 0
a little bit longer than expected...
Power Query:
let
    // calculates max number of items in the list of lists
    max_count = (lst) => List.Max(List.Transform(lst, (x) => List.Count(x))),
    // splits list column and creates custom column names
    exp_column = (tbl, column_name) => Table.SplitColumn(
        tbl,
        column_name,
        (x) => x,
        List.Transform(
            {1..max_count(Table.Column(tbl, column_name))},
            (x) => column_name & " " & Text.From(x)
        )
    ),
    // your source table
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    // just group by id and collect names and events into distinct lists
    group = Table.Buffer(
        Table.Group(
            Source,
            "Order ID",
            {
                {"Name", (x) => List.Distinct(x[Names])},
                {"Event", (x) => List.Distinct(x[Event])}
            }
        )
    ),
    // apply our functions to finish this job
    exp_names = exp_column(group, "Name"),
    exp_events = exp_column(exp_names, "Event")
in
    exp_events
 

Attachments

  • eve.png
    eve.png
    51.9 KB · Views: 3
Upvote 0
Another attempt

Power Query:
let
A = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
B = Table.Group(A, {"Order ID"}, {{"x", each 
    [ 
    a = List.Distinct([Names]), 
    b = List.Distinct([Event]), 
    c = List.Combine(List.Transform(List.Zip({{{1..List.Count(a)},{1..List.Count(b)}}, {"Name", "Event"}}), (x)=> List.Transform(x{0}, each x{1} & " " & Text.From (_)))), 
    d = Table.Combine(List.Transform({List.Zip({c, a & b})}, each Table.PromoteHeaders(Table.FromColumns(_)))) 
    ] [d]}}),
C = Table.ExpandTableColumn(B, "x", List.Distinct(List.Combine(List.Transform(B[x], Table.ColumnNames))))
in
C
 
Upvote 0

Forum statistics

Threads
1,223,659
Messages
6,173,637
Members
452,525
Latest member
DPOLKADOT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top