Power Query Problems

How to Use Table.AggregateTableColumn with Dynamic Column Names in Power Query

Adam DavisAdam Davis Oct 29, 2026 871 views

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.

How to Use Table.AggregateTableColumn with Dynamic Column Names in Power Query
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.
Before you start

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.

Solution 1Recommended

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.

1
Create a Custom List of Columns

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.

2
Group the Source Table

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).

3
Apply List.Transform for Aggregation

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}).

4
Execute Table.AggregateTableColumn

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.

Use List.Transform to Generate Dynamic Aggregation Definitions
Data Types Matter: Ensure that all columns referenced in CustomList contain numeric data types. Mathematical aggregations like List.Median will result in errors if applied to text or date formats.
Free Microsoft Office alternative

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. 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and follow the simple installation instructions.
  2. 2. Open Your Excel Data: Launch WPS Spreadsheet and directly open your existing .xlsx or .csv files without losing formatting or data.
  3. 3. Analyze Data Quickly: Use built-in PivotTables and functions to aggregate, filter, and summarize massive datasets effortlessly.
Fully compatible with Microsoft Excel formats (.xlsx, .xls, .csv).Powerful built-in data analysis tools including PivotTables and advanced statistical formulas.Lightweight installation and blazing-fast performance for large datasets.Free to use with a familiar, tabbed user interface for seamless migration.
microsoft office alternative - wps office

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.