Table.Group

Table

Groups rows by key columns and applies aggregation functions to produce summary results.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Table.Group(table as table, key as any, aggregatedColumns as any, optional groupKind as nullable number, optional comparer as nullable function) as table

Parameters

NameTypeRequiredDescription
tabletableYesThe input table to group.
keyanyYesA column name or list of column names to group by.
aggregatedColumnsanyYesA list of {newColumnName, aggregationFunction} pairs defining the aggregations.
groupKindnumberNoGroupKind.Global (default) or GroupKind.Local for contiguous grouping.
comparerfunctionNoAn optional comparer function for key comparison.

Return Value

tableA table with one row per unique key value, with additional columns for aggregated results.

Remarks

Table.Group groups rows that share the same values in the specified key column(s), then applies aggregation functions to produce summary columns. Common aggregation patterns include List.Count for counting rows, List.Sum for summing values, and List.Average for averages.

The aggregatedColumns parameter takes a list of lists, where each inner list contains the new column name and a function that receives the grouped sub-table.

If your data is already sorted and groups are contiguous (all rows for each key value appear together), use GroupKind.Local as the fourth argument for better performance. Otherwise, the default GroupKind.Global scans the entire table.

If you sort a table before grouping, wrap the sorted table in Table.Buffer first. Without buffering, Table.Group does not guarantee that the row order from the preceding sort is preserved, which can lead to unexpected results.

When using the "All Rows" aggregation pattern (each _), always include a type annotation as the third element in the aggregation list (e.g., {"AllRows", each _, type table}) to improve performance and readability.

Examples

Example 1: Count rows by group

Table.Group(Sales, "Region", {{"OrderCount", each Table.RowCount(_), Int64.Type}})
Result
Region
OrderCount
1East3
2West3
3North2

Example 2: Sum values by group

Table.Group(Sales, "Category", {{"TotalQuantity", each List.Sum([Quantity]), type number}})
Result
Category
TotalQuantity
1Widgets28
2Gadgets6
3Misc1

Example 3: Multiple aggregations

Table.Group(
    Sales,
    "Region",
    {
        {"OrderCount", each Table.RowCount(_), Int64.Type},
        {"AvgPrice", each List.Average([UnitPrice]), type number}
    }
)
Result
Region
OrderCount
AvgPrice
1East353.33
2West341.67
3North245

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks