How to Use Table.AggregateTableColumn with Dynamic Column Names in Power Query
Question details
The user needs to aggregate a dynamically generated list of columns in Power Query by grouping data and applying mathematical functions to selected numeric fields.

- Product
- Excel / Power Query
- Device & OS
- not provided
- Scenario
- Aggregating large datasets (e.g., 90,000 rows by 300 columns) where column names are dynamically generated instead of manually hard-coded into the query.
- Observed behavior
- The goal is to programmatically apply an aggregation function, such as Median, to a dynamic list of columns without manually specifying each column name.
Ensure your dataset is loaded into Power Query and that the dynamic columns you intend to aggregate are correctly formatted as numeric data types to prevent calculation errors.
Use List.Transform to Generate Dynamic Aggregation Definitions
Use this method to programmatically create aggregation definitions for Table.AggregateTableColumn instead of hardcoding each column name.
When dealing with a massive number of columns (e.g., hundreds of fields), manually writing aggregation rules is inefficient. By combining Table.AggregateTableColumn with List.Transform, you can dynamically apply an aggregation function like List.Median to a custom list of column names.
Generate or define a list containing the names of the columns you want to aggregate. In your M code, assign this to a variable named CustomList.
Group your source data by the designated key column. Ensure the grouped data is stored in a table column (for example, named "AccTable") within your grouped step (e.g., GroupedAcc).
Generate the aggregation definitions dynamically using List.Transform. The syntax translates your column list into the required format: List.Transform(CustomList, each {_, List.Median, _, type number}).
Combine these elements by writing the function: Table.AggregateTableColumn(GroupedAcc, "AccTable", List.Transform(CustomList, each {_, List.Median, _, type number})). This applies the calculation to all dynamically identified columns.

Looking for a Lightweight Tool for Data Analysis?
While complex Power Query M scripting is specific to Microsoft Excel, WPS Office provides a lightweight, highly compatible, and free alternative for comprehensive data processing and spreadsheet management.
- 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and follow the simple installation instructions.
- 2. Open Your Excel Data: Launch WPS Spreadsheet and directly open your existing .xlsx or .csv files without losing formatting or data.
- 3. Analyze Data Quickly: Use built-in PivotTables and functions to aggregate, filter, and summarize massive datasets effortlessly.

Frequently Asked Questions
Can I use other aggregation functions besides List.Median?
Yes, you can substitute List.Median with other Power Query M functions such as List.Sum, List.Average, List.Max, or List.Min within your List.Transform formula, depending on your specific analytical needs.
What happens if a column in my dynamic list is missing from the source table?
If a column specified in your dynamic list does not exist in the source table being aggregated, Power Query will return an error. Ensure your column list generation logic dynamically reads from the available headers in the current dataset.
How do I ensure my dynamic list only picks up numeric columns?
You can use the Table.ColumnsOfType function or filter the results of Table.Schema to programmatically extract a list of column names that are strictly typed as numeric before passing that list to List.Transform.




