Transpose with power query

linkn00

New Member
Joined
Aug 15, 2019
Messages
44
Office Version
  1. 365
Platform
  1. Windows
Hello,

Is there a way to do this transformation using power query? Help is extremely appreciated

order numberitem
1​
a
1​
a
1​
a
2​
b
2​
b
2​
c
2​
d
result
Order number
1​
aaa
2​
bbcd
 

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.
Select the range and go to Data -> and click From Table/Range. It will default to changing the range to a table that looks like this:
Book1
ABCDE
2order numberitem
31a
41a
51a
62b
72b
82c
92d
10
11result
12Order number
131aaa
142bbcd
Sheet1

Alternately, if you select the entire range and name it something like Data, the source will not be changed.
Either way, in Power Query go to Transform tab and click the Transpose button. The resulting table in both cases will look like this:
Book1
HIJKLMNOPQRST
1Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11Column12Column13
2order number1112222resultOrder number12
3itemaaabbcdab
4ab
5ac
6d
Sheet2
 
Upvote 0
The sample suggests the order number is sorted.
If that is the case, one can use a group by, with the options groupkind.local. That's a fast grouping.
As aggregation using all rows, named Sub or something Aline. Add a custom column with the formula table.selectcolumns([ Sub ],"Item"). Wrap it in table.transpose. Expand the table.
 
Last edited by a moderator:
Upvote 0
Maybe I didn't explain it correctly. I need to change A1:B8 to something that looks like A13:E14
 
Upvote 0
The sample suggests the order number is sorted.
If that is the case, one can use a group by, with the options groupkind.local. That's a fast grouping.
As aggregation using all rows, named Sub or something Aline. Add a custom column with the formula table.selectcolumns(,"Item"). Wrap it in table.transpose. Expand the table.
could you please explain to me in detail? i am new to this power query
 
Upvote 0
could you please explain to me in detail? i am new to this power query
 
Upvote 0
My bad. Sometimes I miss the details.
Going from this table:
Book1
AB
1order numberitem
21a
31a
41a
52b
62b
72c
82d
Sheet1

Load it into Power Query, using this M Code:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"order number"}, {{"All Rows", each _, type table [order number=text, item=text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.RemoveColumns([All Rows],"order number")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Table.Transpose( [Custom] )),
    #"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom1", "Custom.1", {"Column1", "Column2", "Column3", "Column4"}, {"Column1", "Column2", "Column3", "Column4"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom.1",{"All Rows", "Custom"})
in
    #"Removed Columns"
Resulting in this table:
Book1
DEFGH
1order numberColumn1Column2Column3Column4
21aaa
32bbcd
Sheet1

The two custom columns had to be hand coded, and no doubt there may be a better way to do it, but I think (HOPE!) this answers the question.

Depending on how large the actual data is, this MIGHT be as easy to do with 365 Array formulas, but if the data is large or reoccurring, Power Query is the way to go.
 
Upvote 0
Thank you so much this is amazing. One last question if you could please help me?
How can I compare the row to see if all of the items are the same?
in this case Order number 2, all items are not the same

Basically, I am trying to find the order number with different items.
 
Last edited:
Upvote 0
If the goal is looking for different items, you do not need to transpose.
After the grouping, add a custom column with a formula like List.Distinct([All Rows][Item]).
Depending of your need, wrap this inside List.Count. you can even add a condition the count needs to be one to have true or false as the result.
Without excel for the moment, but maybe others can provide the code. Team work here...
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"order number", Int64.Type}, {"item", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"order number"}, {{"Items", each List.Count(List.Distinct([item]))=1, type nullable logical}}, GroupKind.Local)
in
    #"Grouped Rows"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,970
Messages
6,175,703
Members
452,667
Latest member
vanessavalentino83

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