UNION, EXCEPT, INTERSECT and CROSSJOIN in DAX

How the DAX set functions combine tables, UNION to append, EXCEPT for rows only in the first table, INTERSECT for rows in both, CROSSJOIN for every combination, NATURALINNERJOIN and NATURALLEFTOUTERJOIN, with examples for new, lost and returning customers.

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?

FunctionReturnsMeaning
UNION ( LY, TY )A, B, C, D, C, D, Eall, with duplicates
DISTINCT ( UNION ( LY, TY ) )A, B, C, D, Eall customers of both years
EXCEPT ( TY, LY )Enew customers
EXCEPT ( LY, TY )A, Blost customers
INTERSECT ( TY, LY )C, Dreturning customers
Customers last year {A, B, C, D} and this year {C, D, E}

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] ) ).

01,0002,0003,0004,0005,0004,200Last year-900Lost+1,150New4,450This year
Customers from last year to this year

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, EXCEPT and INTERSECT need 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. UNION keeps them; wrap in DISTINCT to count unique values.
  • INTERSECT order. INTERSECT ( t1, t2 ) is not the same as INTERSECT ( t2, t1 ) when duplicates exist.
  • Huge CROSSJOINs. Millions of combinations are slow; filter first.

Where can you check this?

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.

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.