DAX aggregation functions: every one with an example

A reference of all DAX aggregation functions, SUM, SUMX, AVERAGE, AVERAGEA, AVERAGEX, MIN, MINA, MINX, MAX, MAXA, MAXX, COUNT, COUNTA, COUNTAX, COUNTBLANK, COUNTROWS, COUNTX, DISTINCTCOUNT, DISTINCTCOUNTNOBLANK, APPROXIMATEDISTINCTCOUNT, PRODUCT and PRODUCTX, with examples.

Short answer: DAX aggregation functions come in three forms. The plain form takes a column (SUM ( Sales[Amount] )). The X form is an iterator that evaluates an expression per row of a table and then aggregates (SUMX ( Sales, Sales[Qty] * Sales[Price] )). The A form (AVERAGEA, MINA, MAXA, COUNTA) also handles non-numeric values such as Booleans. Most return BLANK when there are no rows, which keeps empty rows out of visuals.

This page lists all of them with one example each. The everyday ones have their own posts: SUM vs SUMX, count functions, AVERAGEX, MINX and MAXX.

Which functions sum and multiply?

FunctionDoesExample
SUMadds the numbers in a columnSUM ( Sales[Amount] )
SUMXadds an expression evaluated per rowSUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )
PRODUCTmultiplies the numbers in a columnPRODUCT ( Growth[Factor] )
PRODUCTXmultiplies an expression evaluated per rowPRODUCTX ( Returns, 1 + Returns[Monthly Return] ) - 1
Sums and products

PRODUCTX is the function for compound growth: a month of +10% followed by a month of -10% compounds to 1.10 * 0.90 - 1, which is -1%, while adding the two rates says 0%.

Compound Return =
PRODUCTX ( VALUES ( 'Date'[Month] ), 1 + [Monthly Return] ) - 1

Which functions average?

FunctionDoesExample
AVERAGEmean of the numbers in a columnAVERAGE ( Sales[Amount] )
AVERAGEAmean, counting TRUE as 1, FALSE and text as 0AVERAGEA ( Survey[Recommends] )
AVERAGEXmean of an expression per rowAVERAGEX ( VALUES ( 'Date'[Month] ), [Revenue] )
Averages

For medians, percentiles and geometric means see the statistical functions.

Which functions find the smallest and largest?

FunctionDoesExample
MINsmallest value in a column, or the smaller of two valuesMIN ( Sales[Order Date] )
MINAas MIN, also for Booleans (FALSE is 0)MINA ( Tasks[Done] )
MINXsmallest result of an expression per rowMINX ( VALUES ( 'Date'[Month] ), [Revenue] )
MAXlargest value in a column, or the larger of two valuesMAX ( [Revenue], [Target] )
MAXAas MAX, also for Booleans (TRUE is 1)MAXA ( Tasks[Done] )
MAXXlargest result of an expression per rowMAXX ( VALUES ( Sales[Order ID] ), [Revenue] )
Minimum and maximum

Which functions count?

FunctionCountsExample
COUNTnon-blank numbers, dates and stringsCOUNT ( Sales[Ship Date] )
COUNTAnon-blank values, also BooleansCOUNTA ( Sales[Is Return] )
COUNTAXrows where an expression returns any non-blank valueCOUNTAX ( Sales, Sales[Comment] )
COUNTBLANKblank cells in a columnCOUNTBLANK ( Sales[Ship Date] )
COUNTROWSrows of a tableCOUNTROWS ( Sales )
COUNTXrows where an expression is not blankCOUNTX ( VALUES ( 'Product'[Product] ), [Revenue] )
DISTINCTCOUNTunique values, BLANK as oneDISTINCTCOUNT ( Sales[Customer Key] )
DISTINCTCOUNTNOBLANKunique values without BLANKDISTINCTCOUNTNOBLANK ( Sales[Promo Code] )
APPROXIMATEDISTINCTCOUNTan estimate of unique values (large DirectQuery sources)APPROXIMATEDISTINCTCOUNT ( Events[User ID] )
Counts

How do you choose between plain and X?

IfUse
the value is already a columnplain: SUM, AVERAGE, MAX
the value must be computed per rowX: SUMX ( Sales, Qty * Price )
the average or max is per group (month, customer)X over the group: AVERAGEX ( VALUES ( 'Date'[Month] ), [Revenue] )
the total must add up the rows of the visualX over the row column: SUMX ( VALUES ( Customer[Customer] ), [Bonus] )

What goes wrong?

  • Multiplying or dividing aggregates where the row math is needed. SUM ( Qty ) * SUM ( Price ) is not revenue.
  • Average of a ratio column. Average the parts, then divide.
  • X functions over a fact table with a measure inside. Slow; iterate the dimension.
  • Expecting 0. Most return BLANK with no rows; see BLANK handling.

Where can you check this?

Where do you start?

Keep this page for lookup, and learn SUM, SUMX, DISTINCTCOUNT, COUNTROWS and AVERAGEX well; they cover nearly every measure. The filter functions decide what rows these functions see.

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.