GROUPBY
A table's rows grouped by columns, with columns computed over each group's rows (CURRENTGROUP), without context transition.
Group: Table manipulation. Compatible with DAX.
Syntax
GROUPBY ( <table> [, <groupBy_columnName>] … [, <name>, <expression>] … )
| Parameter | Type | Description |
|---|---|---|
table | Table | The rows to group: a table of the model or any table expression, such as a variable. |
groupBy_columnName | Column optional, repeatable | A column to group by: one of the table's, or of a table it reaches through relationships. |
name | Text optional, repeatable | The name of a new column, in quotes. |
expression | Expression optional, repeatable | An aggregation over CURRENTGROUP (), worked out for each group. |
Returns
Table. One row per combination of the grouping columns' values, with the new columns.
Remarks
- Each expression reaches the group's rows through
CURRENTGROUP (), inside an X function such asSUMXorMAXX. DAX allows nothing else in these expressions. - There is no context transition, so a measure would not see the group. That sets GROUPBY apart from
SUMMARIZE, and suits it to tables made in the query, whose columns have no tie to the model. - Without a grouping column, all rows make one group and the result has one row; an empty table gives none.
- Grouping columns may come from related tables:
GROUPBY ( Sales, Products[Category], ... ). - A blank is a value of its own when rows are grouped.
Examples
Liters by shop and product
EVALUATE
VAR Sold =
DATATABLE (
"Shop", STRING, "Product", STRING, "Liters", INTEGER,
{ { "A", "Pils", 10 }, { "A", "IPA", 5 }, { "B", "Pils", 7 }, { "B", "Pils", 3 } }
)
RETURN
GROUPBY ( Sold, [Shop], [Product], "Liters", SUMX ( CURRENTGROUP (), [Liters] ) )
ORDER BY [Shop], [Product]
| Shop | Product | Liters |
|---|---|---|
| A | IPA | 5 |
| A | Pils | 10 |
| B | Pils | 10 |
Grouping by a column of a related table
EVALUATE
GROUPBY ( Sales, Products[Category], "Liters", SUMX ( CURRENTGROUP (), Sales[Liters] ) )
ORDER BY Products[Category]
| Products[Category] | Liters |
|---|---|
| Beer | 1505389 |
| Cider | 301074 |
No grouping column
All four rows form one group.
EVALUATE
VAR Sold =
DATATABLE (
"Shop", STRING, "Product", STRING, "Liters", INTEGER,
{ { "A", "Pils", 10 }, { "A", "IPA", 5 }, { "B", "Pils", 7 }, { "B", "Pils", 3 } }
)
RETURN
GROUPBY ( Sold, "Liters", SUMX ( CURRENTGROUP (), [Liters] ) )
| Liters |
|---|
| 25 |
Related functions
CURRENTGROUP, SUMMARIZE, SUMMARIZECOLUMNS, SUMX, ADDCOLUMNS.