Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
janitor048
Helper I
Helper I

How to apply group by to all columns (an unknown number of columns)?

Hi,

 

I am importing data that looks like this:

CategorySub-CategoryCol1Col2
AA123
AA235
BB156
BB268
C 23

 

I would like to condense this information such that the Sub-Category column gets aggregated, i.e. I want the result to be

CategoryCol1Col2
A58
B1114
C23

 

I believe that this is something like "Group by" with sum applied to column Category.

However, my problem is the following - there could be another column "Col3". Or "Col4" or "Col5", etc. And I want them to be grouped in the same way. I do not know the number of such columns when loading the data - and in fact this number can change. All I know is that those columns all have the same format. And such that I want to sum them in the same way.

 

The reason I am trying to do this inside the Power Query part is that after having my desired result above, I would proceed to trannspose the table and do some more stuff before actually starting to use the table.

 

Any help would be appreciated! Thanks.

 

1 ACCEPTED SOLUTION

Ok, playing around a bit - if I do the following on my result from the unpivot step

1. Group by Category & Attribute

2. Pivot the column Attribute in the result from step 1

 

I get my desired result (see original post). Somehow I have the feeling that this is a bit more complicated than really necessary, but ok 😄

View solution in original post

9 REPLIES 9
artemus
Microsoft Employee
Microsoft Employee

Here is how you can do it without doing an Unpivot.

= Table.Group(PreviousStep, {Category}, List.Transform(List.Select(Table.ColumnNames(PreviousStep),_ <> "Category" and _ <> "Sub-Category"), each {_, (grp) => List.Sum(Table.Column(grp, _)), Int32.Type}))
Greg_Deckler
Community Champion
Community Champion

Having the number of columns change between data loads sounds bad to me. But, @ImkeF may have some tricks up her sleeve.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Yeah, it's actually columns of dates that get eventually added; i.e. new project day / week, new column. I can't change this right now but would still need to do some analysis. 

Would it maybe help transposing the table first? I will probably do this in a later step anyhow.

@janitor048  - I would maybe select your Category and SubCategory columns and then right-click and Unpivot other columns.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Seconding Gregs suggestion!

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hey hey! That might be a first!! 😄


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Ok, I might be a bit slow here. Doing what @Greg_Deckler suggests (unpivot on the columns other than Category and Subcategory) yields

CategorySubcategoryAttributeValue
AA1Col12
AA1Col23
AA2Col13
AA2Col25
BB1Col15
BB1Col26
BB2Col16
BB2Col28
C Col12
C Col23

Originally I wanted to have "Category" and "Col1" etc. still as columns and rows. Also the result is not aggregated by Category (so --> combination of A and Col1 = 5; combination of A and Col2 = 8, etc.). How would I achieve this? 

Ok, playing around a bit - if I do the following on my result from the unpivot step

1. Group by Category & Attribute

2. Pivot the column Attribute in the result from step 1

 

I get my desired result (see original post). Somehow I have the feeling that this is a bit more complicated than really necessary, but ok 😄

Nice @janitor048 ! I don't know, I'm a big fan of whatever works! 🙂  And, dealing with a varying number of columns is a pretty nasty, complicated thing so it may not be overly complex.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.