Pivot/unpivot help

tombamber

New Member
Joined
Mar 28, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hi

Using PowerQuery I want to transform the first table into the second so that every 'Number' in a 'Region' shows every 'Location' in that 'Region', with multiple rows per 'Number'. This is just dummy data.

I have tried doing this with pivot/unpivot but struggling to get my head around it.

Transform this...

1703162498119.png

...into this...
1703162522011.png


Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Load the data in to two queries. In the first one remove the Number column then group on the other two. In the second, remove the location column (it doesn't look like you have duplicate numbers, but if you do, group the columns). Then merge the two queries on Region, and expand the resulting table column.
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.AddColumn(Source, "Custom", each Table.SelectRows(Source, (x)=> x[Region] = [Region])[Location]),
    tbl1 = Table.RemoveColumns(tbl,{"Location"}),
    tbl2 = Table.ExpandListColumn(tbl1, "Custom"),
    tbl3 = Table.RenameColumns(tbl2,{{"Custom", "Location"}}),
    tbl4 = Table.Distinct(tbl3),
    Result = Table.ReorderColumns(tbl4,{"Number", "Region", "Location"})
in
    Result

Book1
ABCDEFGH
1LocationRegionNumberNumberRegionLocation
21234Region 1362559362559Region 11234
31234Region 1625223362559Region 14537
44537Region 1538346625223Region 11234
55467Region 2348920625223Region 14537
66544Region 2779436538346Region 11234
75467Region 2281464538346Region 14537
84355Region 3862489348920Region 25467
94355Region 3266184348920Region 26544
106799Region 3396368779436Region 25467
11779436Region 26544
12281464Region 25467
13281464Region 26544
14862489Region 34355
15862489Region 36799
16266184Region 34355
17266184Region 36799
18396368Region 34355
19396368Region 36799
20
Sheet1
 
Upvote 0
a UI based solution (same concept as already mentioned above).
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Locations"]}[Content],
    #"Merged Queries" = Table.NestedJoin(Source, {"Region"}, Source, {"Region"}, "Locations", JoinKind.LeftOuter),
    #"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"Location"}),
    #"Expanded Changed Type" = Table.ExpandTableColumn(#"Removed Columns", "Locations", {"Location"}, {"Location"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded Changed Type"),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Duplicates",{"Number", "Region", "Location"})
in
    #"Reordered Columns"
 
Upvote 0
a UI based solution (same concept as already mentioned above).
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Locations"]}[Content],
    #"Merged Queries" = Table.NestedJoin(Source, {"Region"}, Source, {"Region"}, "Locations", JoinKind.LeftOuter),
    #"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"Location"}),
    #"Expanded Changed Type" = Table.ExpandTableColumn(#"Removed Columns", "Locations", {"Location"}, {"Location"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded Changed Type"),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Duplicates",{"Number", "Region", "Location"})
in
    #"Reordered Columns"
Thanks, a UI version would be great for my level of experience.

Can you elaborate a little on how to do this as I don't fully understand the code.
 
Upvote 0
On the Data Ribbon -> Get data from Table/range
1704391030106.png

In the PQ UI, on the home tab, Merge Queries, and combine the query with itself. Select Region in "both" tables. In the Join type go for left Outer, the default.
1704391149055.png

In the result at this stage, select the column Location and press the delete button on your keyboard.

In the column Locations, click on the icon with two arrows pointing up and to the side (that's the expand button)
1704391330307.png


Select Location and press OK.
1704391311327.png

Again from the home tab, in the Remove Rows drop down, select Remove Duplicates.
1704391499275.png


Reorganizing the columns is much like in excel. You select it with a left mouse click and move it around while holding the left mouse button. Release it when the column in the correct order.

Finally it's time to close and load the data to excel. Either as a pivot table or a table. You probably do not need to use the data model at all.

1704391627449.png
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

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