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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
borlip
Kudo Kingpin
Kudo Kingpin

Table.Group sum up dynamic columns

I have a pivoted table where each column represents revenue data by week starts. Week starts are column headers. There could be various number of columns.

 

I need to sum up each column (total revenue by week). I was thinking to use Table.Group

(table as table, key as any, aggregatedColumns as list, optional groupKind as nullable number, optional comparer as nullable function) as table

 

 

aggregatedColumns should be something like this: {{"1/7/2015", each List.Sum([1/7/2015]), type number}....

But how can I generate it on fly for a dynamic list of columns, as I don't know how many columns do I have?

2 REPLIES 2
Shahid12523
Community Champion
Community Champion

You can handle this in Power Query (M) by dynamically building the list of aggregation operations. Since your week-start columns are dynamic, you can’t hardcode them — you generate the list from the column names.

 

Example in M:

let
Source = YourPivotedTable,
ColNames = List.RemoveItems(Table.ColumnNames(Source), {"KeyColumn"}), // keep out keys
AggList = List.Transform(ColNames, each {_, each List.Sum(_), type number}),
Result = Table.Group(Source, {"KeyColumn"}, AggList)
in
Result


Explanation:

Table.ColumnNames(Source) → gets all column names.

List.Transform → turns each into an aggregation spec {ColumnName, each List.Sum([ColumnName]), type number}.

Table.Group → applies it dynamically.

This way, no matter how many week columns you have, the grouping/summing will work.

Shahed Shaikh
borlip
Kudo Kingpin
Kudo Kingpin

Got away without using Table.Group()... Unpivoted the table and grouped by dates to calculate sum of revenues per each date. Then pivoted the table by dates again...

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Solution Authors