PQ pivot or grouping by category

PippaThePointer

New Member
Joined
Sep 21, 2023
Messages
34
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I am having a great time learing PQ and think its awesome, but im stuck on something that seems so simple but i can work it out.
I have some data that i have updated to be tabulated so there is one row for every item. The rows a column for 'Store' and then a whole lot of other columns such as 'Size', category, ArtworkID and qty for example. I want to group the data so that i have only one row per 'Store' and then a column that contains the qty with headers of that column containing the 'Size', Category etc. My full data has loads of items but a i have test file here that should be enough to grasp what i have to do. I tried pivoting and grouping and a combination but just cant quite get it.
Here is a basic example:
Store Category QTY ArtworkCode
A Women 1 Woman_1_1200x3000.pdf
B Women 2 Woman_1_1200x2000.pdf
C Women 3 Woman_1_2000x3000.pdf
C Men 4 Man_1_2000x3000.pdf
C Women 5 Woman_1_1200x2000.pdf
C Men 6 Man_1_1200x2000.pdf
C Women 7 Woman_1_2300x3300.pdf
C Men 8 Man_1_2300x3300.pdf

And this is what i want it to be:
Woman_1_1200x3000.pdfWoman_1_1200x2000.pdfWoman_1_2000x3000.pdfMan_1_2000x3000.pdfWoman_1_1200x2000.pdfMan_1_1200x2000.pdfWoman_1_2300x3300.pdfMan_1_2300x3300.pdf
WomenWomenWomenMenWomenMenWomenMen
A1
B2
C345678
 
Power Query:
let
    Source = Table.Buffer(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]), 
    stores = Table.Group(Source[[Store], [QTY]], "Store", {"Qty", (x) => x[QTY]}),
    result = Table.FromList(
        {Source[ArtworkCode]} & {Source[Category]} & Table.ToList(stores, (x) => {x{0}} & x{1}), 
        (x) => x, 
        Table.RowCount(Source), null, null
    )
in
    result
 
Upvote 0
Solution
Power Query:
let
    Source = Table.Buffer(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]),
    stores = Table.Group(Source[[Store], [QTY]], "Store", {"Qty", (x) => x[QTY]}),
    result = Table.FromList(
        {Source[ArtworkCode]} & {Source[Category]} & Table.ToList(stores, (x) => {x{0}} & x{1}),
        (x) => x,
        Table.RowCount(Source), null, null
    )
in
    result
Thanks for this. Its helpful.
The 2 header rows are currently starting at column 1 right above the 'Store'. I need these to start in this example at column2. So A2 and A3 would be empty cells or be filled with 'ArtworkCode' and 'Category' for example.
I would like the Columns to show only the unique amount required. Currently its giving me a column for every source row.

The end user wants to view this very long table, i have already created a macro in another test that will use a PQ dynamic drop down list so they can select a store and it will only show the row and the columns with an entry.
 

Attachments

  • Screenshot 2025-03-07 105056.png
    Screenshot 2025-03-07 105056.png
    31.3 KB · Views: 0
Upvote 0
I think i have answered my own question with a small test using Pivot and transpose.
let
Source = Table.Buffer(Excel.CurrentWorkbook(){[Name="Test"]}[Content]),
PivotQTY = Table.Pivot(Source, List.Distinct(Source[Store]), "Store", "QTY", List.Sum),
DemotedHeaders = Table.DemoteHeaders(PivotQTY),
Reordered = Table.ReorderColumns(DemotedHeaders,{"Column2", "Column1", "Column3", "Column4", "Column5"}),
Transpose = Table.Transpose(Reordered),
PromoteHeaders = Table.PromoteHeaders(Transpose, [PromoteAllScalars=true])
in
PromoteHeaders
 
Upvote 0

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