Unpivot Specific Columns

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I would like to know how I can transform this table:

IndexRegionCategoryRedBluePinkGreen
1NorthFruits56310
2SouthClothes1012620
3EastGames1518930

To this, which basically appends red/pink underneath one another and blue/green underneath one another. I know I can create separate queries then do a merge, but was wondering if it could be done with one query:

IndexRegionCategoryRed-PinkValueBlue-GreenValue1
1NorthFruitsRed5Blue6
2SouthClothesRed10Blue12
3EastGamesRed15Blue18
1NorthFruitsPink3Green10
2SouthClothesPink6Green20
3EastGamesPink9Green30
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Please try

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ToCol = Table.ToColumns(Source),
    FromCol = Table.FromColumns({ List.Repeat(ToCol{0},2),List.Repeat(ToCol{1},2) ,List.Repeat(ToCol{2},2),ToCol{3} & ToCol{5}, ToCol{4} & ToCol{6}}, {"Index","Region","Category","Red-Pink","Blue-Green"})
in
    FromCol

Book1
ABCDEFG
1IndexRegionCategoryRedBluePinkGreen
21NorthFruits56310
32SouthClothes1012620
43EastGames1518930
5
6IndexRegionCategoryRed-PinkBlue-Green
71NorthFruits56
82SouthClothes1012
93EastGames1518
101NorthFruits310
112SouthClothes620
123EastGames930
Sheet1
 
Upvote 0
1630162616758.png

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    t1={"Red", "Pink"},
    t2={"Blue", "Green"},
    t3={"Index","Region","Category","Red-Pink","Value","Blue-Green","Value1"},
    fx=(x,y)=>
               Table.ToColumns(
                                Table.RemoveColumns(
                                                     Table.Unpivot(Source, x, "a", "b"),
                                                     y
                                                   )
                              ),
    res=Table.Sort(
                    Table.FromColumns(
                                        fx(t1,t2)&List.LastN(fx(t2,t1),2),
                                        t3
                                     ),
                    {{"Red-Pink",1},{"Index",0}}
                  )
in
    res
Book1.xlsx
ABCDEFG
1IndexRegionCategoryRedBluePinkGreen
21NorthFruits56310
32SouthClothes1012620
43EastGames1518930
5
6IndexRegionCategoryRed-PinkValueBlue-GreenValue1
71NorthFruitsRed5Blue6
82SouthClothesRed10Blue12
93EastGamesRed15Blue18
101NorthFruitsPink3Green10
112SouthClothesPink6Green20
123EastGamesPink9Green30
Sheet1
 
Upvote 0
I forgot Color columns

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Unpivoted = Table.UnpivotOtherColumns(Source, {"Index", "Region", "Category"}, "Attribute", "Value"),
    Combine = Table.FromColumns(Table.ToColumns(Table.AlternateRows(Unpivoted,1,1,1))&List.LastN(Table.ToColumns(Table.AlternateRows(Unpivoted,0,1,1)),2),{"Index","Region","Category","Red-Pink","Value","Blue-Green","Value "}),
    Sort = Table.Combine(Table.Group(Combine, {"Red-Pink"}, {{"T", each (_)}})[T])
in
    Sort

Book1
ABCDEFG
1IndexRegionCategoryRedBluePinkGreen
21NorthFruits56310
32SouthClothes1012620
43EastGames1518930
5
6IndexRegionCategoryRed-PinkValueBlue-GreenValue
71NorthFruitsRed5Blue6
82SouthClothesRed10Blue12
93EastGamesRed15Blue18
101NorthFruitsPink3Green10
112SouthClothesPink6Green20
123EastGamesPink9Green30
Sheet1
 
Upvote 0
Thank you both. Looks like you changed your original code and used different functions. Just curious, was that necessary in order to add the two missing color columns?
 
Upvote 0
I forgot Color columns

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Unpivoted = Table.UnpivotOtherColumns(Source, {"Index", "Region", "Category"}, "Attribute", "Value"),
    Combine = Table.FromColumns(Table.ToColumns(Table.AlternateRows(Unpivoted,1,1,1))&List.LastN(Table.ToColumns(Table.AlternateRows(Unpivoted,0,1,1)),2),{"Index","Region","Category","Red-Pink","Value","Blue-Green","Value "}),
    Sort = Table.Combine(Table.Group(Combine, {"Red-Pink"}, {{"T", each (_)}})[T])
in
    Sort

Book1
ABCDEFG
1IndexRegionCategoryRedBluePinkGreen
21NorthFruits56310
32SouthClothes1012620
43EastGames1518930
5
6IndexRegionCategoryRed-PinkValueBlue-GreenValue
71NorthFruitsRed5Blue6
82SouthClothesRed10Blue12
93EastGamesRed15Blue18
101NorthFruitsPink3Green10
112SouthClothesPink6Green20
123EastGamesPink9Green30
Sheet1
What is the "T" and [T] mean in the last part of the code? Please explain so I understand.
 
Upvote 0
I forgot Color columns
I need to combine one more column of colors and values. Since Power Query starts with base zero, then the new column should combine rows 1 and 4 (after Unpivoted step). How would I modify the formula below to include the new column? I changed the other starting & rows to remove arguements, so no need to worry about those. Thanks!

Power Query:
Combine = Table.FromColumns(Table.ToColumns(Table.AlternateRows(Unpivoted,1,1,1))&List.LastN(Table.ToColumns(Table.AlternateRows(Unpivoted,0,1,1)),2),{"Index","Region","Category","Red-Pink","Value","Blue-Green","Value "}),
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,701
Messages
6,173,920
Members
452,539
Latest member
deeme

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