Short answer: UNION ( t1, t2 ) appends rows (duplicates kept); EXCEPT ( t1, t2 ) returns the rows of t1 that are not in t2; INTERSECT ( t1, t2 ) returns the rows of t1 that are also in t2; CROSSJOIN ( t1, t2 ) returns every combination. The first three need the same number of columns, match by position and take the column names (and lineage) of the first table. With customer lists of two periods, they answer the classic questions: new, lost and returning customers.
"How many customers did we lose this year?" is a set question: customers last year minus customers this year. In DAX it is one EXCEPT.
What does each return?
| Function | Returns | Meaning |
|---|---|---|
| UNION ( LY, TY ) | A, B, C, D, C, D, E | all, with duplicates |
| DISTINCT ( UNION ( LY, TY ) ) | A, B, C, D, E | all customers of both years |
| EXCEPT ( TY, LY ) | E | new customers |
| EXCEPT ( LY, TY ) | A, B | lost customers |
| INTERSECT ( TY, LY ) | C, D | returning customers |
How do you write new, lost and returning customers?
Customers TY = VALUES ( Sales[Customer Key] )
New Customers =
VAR TY = VALUES ( Sales[Customer Key] )
VAR LY = CALCULATETABLE ( VALUES ( Sales[Customer Key] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN COUNTROWS ( EXCEPT ( TY, LY ) )
Lost Customers =
VAR TY = VALUES ( Sales[Customer Key] )
VAR LY = CALCULATETABLE ( VALUES ( Sales[Customer Key] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN COUNTROWS ( EXCEPT ( LY, TY ) )
Returning Customers =
VAR TY = VALUES ( Sales[Customer Key] )
VAR LY = CALCULATETABLE ( VALUES ( Sales[Customer Key] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN COUNTROWS ( INTERSECT ( TY, LY ) )
Revenue from New Customers =
VAR LY = CALCULATETABLE ( VALUES ( Sales[Customer Key] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN CALCULATE ( [Revenue], EXCEPT ( VALUES ( Sales[Customer Key] ), LY ) )"New" here means "not a customer in the same period last year"; for "never bought before", compare with all earlier dates: CALCULATETABLE ( VALUES ( Sales[Customer Key] ), 'Date'[Date] < MIN ( 'Date'[Date] ) ).
When do you use CROSSJOIN?
Every combination of two lists, for example to find product and region pairs with no sales:
EVALUATE
FILTER (
CROSSJOIN ( VALUES ( 'Product'[Category] ), VALUES ( Region[Region] ) ),
ISBLANK ( [Revenue] )
)Column names must differ across the tables. The row count is the product of both tables, so keep inputs small.
What about joins?
NATURALINNERJOIN ( t1, t2 ) and NATURALLEFTOUTERJOIN ( t1, t2 ) join on columns with the same name and the same lineage (from the same source column). They are mostly used in queries on table variables; in a model, relationships do the joining.
What goes wrong?
- Different column counts.
UNION,EXCEPTandINTERSECTneed the same number of columns, matched by position, not by name. - Lineage. The result keeps the first table's lineage;
EXCEPT ( LY, TY )filters as Sales[Customer Key] because LY came from that column. - Duplicates.
UNIONkeeps them; wrap inDISTINCTto count unique values. - INTERSECT order.
INTERSECT ( t1, t2 )is not the same asINTERSECT ( t2, t1 )when duplicates exist. - Huge CROSSJOINs. Millions of combinations are slow; filter first.
Where can you check this?
- Microsoft Learn: UNION function (DAX)
- Microsoft Learn: EXCEPT function (DAX)
- Microsoft Learn: INTERSECT function (DAX)
Where do you start?
Write New Customers, Lost Customers and Returning Customers and put them in a waterfall from last year's count to this year's. Customer retention and cohorts builds a KPI on the same sets.