DAX table functions: every one with an example

A reference of all DAX table manipulation functions, grouping (SUMMARIZECOLUMNS, SUMMARIZE, GROUPBY, rollups), shaping (ADDCOLUMNS, SELECTCOLUMNS, TOPN), sets and joins (UNION, EXCEPT, INTERSECT, CROSSJOIN, GENERATE, natural joins), building (DATATABLE, ROW, GENERATESERIES) and more, with examples.

Short answer: DAX table functions return tables. You use them in four places: as filters in CALCULATE (TREATAS, TOPN), as the table an iterator walks (SUMX ( VALUES ( ... ) )), to define calculated tables (DATATABLE, GENERATESERIES), and as the result of DAX queries (SUMMARIZECOLUMNS). This page lists every function in Microsoft's table manipulation group with one example each.

Which functions group?

FunctionDoesExample
SUMMARIZECOLUMNSgroups by columns of any tables and adds measuresSUMMARIZECOLUMNS ( 'Date'[Year], "Revenue", [Revenue] )
SUMMARIZEdistinct combinations of columns of a table and related onesSUMMARIZE ( Sales, 'Product'[Category], 'Date'[Year] )
GROUPBYgroups a table, aggregating with CURRENTGROUPGROUPBY ( T, T[Year], "@Max", MAXX ( CURRENTGROUP ( ), [@Rev] ) )
CURRENTGROUPthe rows of the current group inside GROUPBYMAXX ( CURRENTGROUP ( ), [@Rev] )
ROLLUP, ROLLUPGROUP, ROLLUPADDISSUBTOTAL, ROLLUPISSUBTOTALsubtotal rows in SUMMARIZE and SUMMARIZECOLUMNSSUMMARIZECOLUMNS ( ROLLUPADDISSUBTOTAL ( 'Date'[Year], "IsTotal" ), "Revenue", [Revenue] )
ADDMISSINGITEMSadds rows with empty values to a SUMMARIZECOLUMNS resultused by Power BI's own queries
IGNOREleaves an expression out of the blank-row test in SUMMARIZECOLUMNSIGNORE ( [Target] )
Grouping

In depth: SUMMARIZECOLUMNS, SUMMARIZE and ADDCOLUMNS.

Which functions shape and pick rows?

FunctionDoesExample
ADDCOLUMNSadds expression columns to a tableADDCOLUMNS ( VALUES ( Customer[Customer] ), "@Rev", [Revenue] )
SELECTCOLUMNSkeeps or renames columns, or adds expressionsSELECTCOLUMNS ( Customer, "Name", Customer[Customer] )
TOPNthe first N rows by an expressionTOPN ( 10, VALUES ( Customer[Customer] ), [Revenue] )
VALUESvisible distinct values, with the blank rowVALUES ( 'Product'[Category] )
DISTINCT (column)visible distinct values, without the blank rowDISTINCT ( 'Product'[Category] )
DISTINCT (table)distinct rows of a table expressionDISTINCT ( UNION ( T1, T2 ) )
FILTERSvalues directly filtered on a columnFILTERS ( 'Product'[Color] )
TREATASapplies a table's values as a filter on other columnsTREATAS ( VALUES ( 'Date'[Year Month] ), Budget[Year Month] )
DETAILROWSreturns the detail rows expression defined for a measureDETAILROWS ( [Revenue] )
Shaping

In depth: TOPN, VALUES vs DISTINCT, TREATAS.

Which functions combine tables?

FunctionDoesExample
UNIONappends rows, duplicates keptUNION ( T1, T2 )
EXCEPTrows of the first table not in the secondEXCEPT ( CustomersTY, CustomersLY )
INTERSECTrows of the first table also in the secondINTERSECT ( CustomersTY, CustomersLY )
CROSSJOINevery combination of rowsCROSSJOIN ( VALUES ( 'Product'[Category] ), VALUES ( Region[Region] ) )
GENERATEfor each row of table1, the rows of table2 evaluated in that rowGENERATE ( VALUES ( 'Date'[Year] ), TOPN ( 3, VALUES ( 'Product'[Product] ), [Revenue] ) )
GENERATEALLas GENERATE, keeping rows of table1 where table2 is emptyGENERATEALL ( T1, T2 )
NATURALINNERJOINjoins on common columns with the same lineageNATURALINNERJOIN ( T1, T2 )
NATURALLEFTOUTERJOINas above, keeping all rows of the left tableNATURALLEFTOUTERJOIN ( T1, T2 )
SUBSTITUTEWITHINDEXa semijoin that replaces common columns with an indexSUBSTITUTEWITHINDEX ( T1, "Index", T2, T2[Key], ASC )
Sets and joins

The GENERATE example returns the top 3 products of each year. In depth: UNION, EXCEPT, INTERSECT and CROSSJOIN.

Which functions build tables?

FunctionDoesExample
DATATABLEa typed table of constantsDATATABLE ( "Name", STRING, { { "A" }, { "B" } } )
ROWa one-row table with named columnsROW ( "Revenue", [Revenue] )
GENERATESERIESa series of numbers in a column named ValueGENERATESERIES ( 0, 100, 10 )
Table constructorrows from any expressions{ ( "Low", 0 ), ( "High", 1 ) }
Building

In depth: GENERATESERIES, DATATABLE and ROW. CALENDAR and CALENDARAUTO build date tables (see date and time functions).

What goes wrong?

  • A table where a value is expected. A table of one row and one column converts to a value; anything larger errors.
  • No sort order. Table functions do not sort; sort in the query or the visual.
  • Lineage lost. Columns created with expressions (SELECTCOLUMNS ( T, "X", T[Col] & "" )) lose their link to the model and do not filter it; use TREATAS.
  • Large intermediate tables. CROSSJOIN and GENERATE multiply rows; filter first.

Where can you check this?

Where do you start?

Learn VALUES, ADDCOLUMNS, SUMMARIZECOLUMNS, TOPN and TREATAS first; they cover most measures and queries. Window functions work on tables like these, row by row.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.