Grouping and set functions
Grouping, ranking and membership
A grouping query turns fact rows into a summary at a declared grain. Ranking selects groups according to an ordering rule; it does not guarantee output display order. Set functions combine compatible tables, but duplicate retention and lineage matter. Use the isolated fixture in labs/dax-queries/README.md. Import QuerySales and QueryCategory, whole-number LineID/Amount, text Category, one-to-many active single-direction relationship from QueryCategory to QuerySales. No other relationships, filters or RLS in this disposable local model. Native execution is pending; outputs below are offline predictions.
Choose the exact semantics
SUMMARIZECOLUMNS groups by model columns, computes named scalar expressions and ordinarily suppresses rows where all supplied expressions are blank. ROLLUPADDISSUBTOTAL adds a rollup flag; use it rather than identifying totals from blank labels. TOPN(n,table,expression,DESC,...) selects by its own sort expressions and includes all boundary ties unless an additional key resolves them. ORDER BY then sorts the selected rows. UNION aligns columns by position and preserves duplicates; INTERSECT keeps matching rows from its first input, including duplicates; EXCEPT keeps first-input rows absent from the second. INTERSECT and EXCEPT are directional; use aligned types and grain. Lineage can affect downstream filtering; these literal examples test only membership and multiplicity.
Example 1 — rank categories and audit subtotals
DEFINE MEASURE 'QuerySales'[Trial Revenue] = SUM('QuerySales'[Amount]) VAR Ranked = SUMMARIZECOLUMNS('QueryCategory'[Category], "Revenue", [Trial Revenue])EVALUATE TOPN(2, Ranked, [Revenue], DESC)ORDER BY [Revenue] DESC, 'QueryCategory'[Category] ASCEVALUATE TOPN(2, Ranked, [Revenue], DESC, 'QueryCategory'[Category], ASC)ORDER BY [Revenue] DESC, 'QueryCategory'[Category] ASCEVALUATESUMMARIZECOLUMNS(ROLLUPADDISSUBTOTAL('QueryCategory'[Category], "IsTotal"), "Revenue", [Trial Revenue])ORDER BY [IsTotal] ASC, 'QueryCategory'[Category] ASCResult 1: A 150, B 100, C 100 (three rows because second place is tied). Result 2: A 150, B 100 (unique category breaks tie, C omitted). Result 3: A 150 false, B 100 false, C 100 false, D 0 false, blank category 350 true. E is absent. Never sum detail rows plus the total row; that doubles revenue. Tie-inclusive ranking answers a different business question from a strict two-row display.
Example 2 — check set membership and duplicate handling
DEFINE VAR LeftRows = { 1, 2, 2, 4 } VAR RightRows = { 2, 3 }EVALUATE UNION(LeftRows, RightRows)ORDER BY [Value] ASCEVALUATE INTERSECT(LeftRows, RightRows)ORDER BY [Value] ASCEVALUATE EXCEPT(LeftRows, RightRows)ORDER BY [Value] ASCEVALUATE INTERSECT(RightRows, LeftRows)ORDER BY [Value] ASCOrdered results: UNION → 1,2,2,2,3,4; INTERSECT(left,right) → 2,2; EXCEPT(left,right) → 1,4; INTERSECT(right,left) → 2. UNION does not join tables on a key. Apply DISTINCT explicitly if the requirement is unique values. A column with duplicated values cannot serve as a unique row identifier.
Review the visual and query independently
Build a matrix grouped by Category using a separately approved saved measure or use the provided expected matrix offline. Match filters, relationship directions, data types, blank policy and totals. Copy a visual query only from your disposable model; generated queries may include additional subtotals or filters. Sort explicitly and compare by grouping key plus subtotal flag. Avoid using TOPN on a subtotal table unless total rows are removed. A SQL GROUP BY is useful as an independent arithmetic oracle but does not reproduce DAX lineage, auto-exist, nonempty rules or engine execution.
Practice and references
Change the first TOPN to three and then four; predict row counts and values. Reverse EXCEPT and explain the different result. Reconcile category revenue to 350 before accepting the ranking.
- https://learn.microsoft.com/en-us/dax/topn-function-dax
- https://learn.microsoft.com/en-us/dax/summarizecolumns-function-dax
- https://learn.microsoft.com/en-us/dax/rollupaddissubtotal-function-dax
- https://learn.microsoft.com/en-us/dax/union-function-dax
- https://learn.microsoft.com/en-us/dax/intersect-function-dax
- https://learn.microsoft.com/en-us/dax/except-function-dax