VALUES vs DISTINCT in DAX, and the blank row

The difference between VALUES and DISTINCT in DAX, the blank row added for keys missing from a dimension, VALUES on a table, FILTERS for direct filters, using VALUES as a scalar, iterating distinct values in measures, and how to find the keys behind (Blank).

Short answer: both return the distinct values of a column visible in the current filter context. The difference is the blank row: when a fact table has keys that do not exist in a dimension, the dimension gets a virtual blank row, and VALUES includes it while DISTINCT does not. VALUES also accepts a table name (all its visible rows plus the blank row), and DISTINCT accepts any table expression. In a model with clean keys they return the same thing.

The difference shows up as a mysterious (Blank) item in slicers and visuals, and as a count that is one higher than expected.

When do they differ?

ExpressionReturnsRows
VALUES ( 'Product'[Product] )Bike, Helmet, Bottle, (Blank)4
DISTINCT ( 'Product'[Product] )Bike, Helmet, Bottle3
COUNTROWS ( VALUES ( 'Product'[Product] ) )4
DISTINCTCOUNT ( 'Product'[Product] )4
DISTINCTCOUNTNOBLANK ( 'Product'[Product] )3
Product has 3 products; Sales has keys 1, 2, 3 and 9 (9 is missing in Product)

The sales with key 9 show up under (Blank) in every visual by product. The blank row only exists when there is a relationship violation (or when a column contains real blanks).

How do you find the keys behind (Blank)?

EVALUATE
EXCEPT ( VALUES ( Sales[Product Key] ), VALUES ( 'Product'[Product Key] ) )

-- or as a measure
Sales without Product =
CALCULATE ( [Revenue], ISBLANK ( 'Product'[Product Key] ) )

Fix the source: add the missing products, or map unknown keys to an "Unknown" product row. Hiding the blank row hides missing revenue.

How are VALUES and DISTINCT used in measures?

-- iterate the visible customers
Avg Revenue per Customer = AVERAGEX ( VALUES ( Customer[Customer] ), [Revenue] )

-- the single selected value (or an error with several)
Selected Year Old = VALUES ( 'Date'[Year] )

-- better
Selected Year = SELECTEDVALUE ( 'Date'[Year] )

-- check membership
Is Bikes = "Bikes" IN VALUES ( 'Product'[Category] )

A one-column, one-row table can be used as a scalar, which is why VALUES ( 'Date'[Year] ) works when exactly one year is selected and errors when several are ("A table of multiple values was supplied where a single value was expected"). Microsoft's guidance is to use SELECTEDVALUE instead.

What is FILTERS?

FILTERS ( column ) returns the values directly filtered on a column, while VALUES returns the values visible, also through filters on other columns or tables. With a slicer on Category = Bikes, VALUES ( 'Product'[Product] ) returns the bike products, but FILTERS ( 'Product'[Product] ) returns all products, because Product itself has no direct filter.

Which to use?

NeedUse
iterate or count visible values, including unknown keysVALUES
distinct values without the blank rowDISTINCT
distinct rows of a table expressionDISTINCT ( table expression )
the one selected valueSELECTEDVALUE
only directly filtered valuesFILTERS

What goes wrong?

  • A count that is one too high. The blank row; check referential integrity.
  • VALUES as a scalar. Errors with several values; use SELECTEDVALUE.
  • VALUES ( table ) in iterators. Iterates whole rows plus a possible blank row; iterate a column unless you need the row.

Where can you check this?

Where do you start?

Run the EXCEPT query above for each fact-to-dimension relationship in DAX query view; every key it returns is a (Blank) in your reports. TREATAS uses VALUES to pass filters between tables.

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.