Forum Discussion
Power Query - Aggregate Columns with Dynamic Column Names
Hi,
After much unsuccessful googling, I'm looking for help dealing with aggregating column names that are dynamic.
I'm sure there's a simple pattern or function to do this but I have Friday brain.
Simple scenario, I have a base table, I merge another table and aggregate certain columns from that table.
Depending on a parameter the user sets, the second merged table ("data") column names can change. I need a way to dynamically aggregate based on a distinct list from the second table.
I've attached a simplified example but the main issue is this line:
= Table.AggregateTableColumn(Source, "data", {{"111", List.Sum, "Sum of 111"}, {"222", List.Sum, "Sum of 222"}})I need the part in bold to be dynamic.
In the example, if you go into the query editor and look at the 'Output' query you can see column 111 and 222 have summed values.
Then change the 'pValue' parameter to 'me' and look at the 'Output' query. You can see that columns 111 and 222 are still showing. What I would like showing after the parameter is changed is columns 333 and 444.
I realise I could unpivot other columns then merge and group in this example but the actual data is much larger and more complex than this so I'd rather solve this using a function or reusable pattern in M code.
Aggregate Dynamic Column Names.xlsx
Thanks,
Kim
Hi KNP
You can use this formula to make the solution dynamic:
#"Aggregated data" = Table.AggregateTableColumn(Source, "data", List.Transform(DistinctValue, each {_, List.Sum, "Sum of " &_}))
7 Replies
- v-frfei-msftCommunity Support
Hi KNP ,
To make the column name dynamicly, please refer to this case.
https://community.powerbi.com/t5/Desktop/Dynamic-column-name-from-its-value/td-p/189442