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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Employee
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
Super User
Super User

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


@ 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!:
The Definitive Guide to Power Query (M)

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.


@ 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!:
The Definitive Guide to Power Query (M)

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!! 😄

@ 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!:
The Definitive Guide to Power Query (M)

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.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors