DAX Variables: VAR & RETURN for Readability & High Performance
Overview & Learning Objectives
In business analytics, counting transactions is only half the story. Executives constantly ask: "How many unique customers purchased this month?" or "How many distinct products were sold?" In DAX, counting unique occurrences is performed using `DISTINCTCOUNT()` and `DISTINCTCOUNTNOBLANK()`. Understanding how each handles blanks is essential for accurate executive reporting.
- Write unique counting measures using `DISTINCTCOUNT()` for Active Customers and Unique Products.
- Contrast `DISTINCTCOUNT()` (includes blank as a distinct item) with `DISTINCTCOUNTNOBLANK()` (ignores blanks).
- Understand the high memory cost of high-cardinality distinct counts on multi-million row datasets.
Industry Standard
Standard: Analytics standards at Amazon and Flipkart mandate using `DISTINCTCOUNTNOBLANK()` for customer identity counts to ensure missing guest IDs do not register as an extra customer.
Real-World Example: Scoop & Swirl Loyalty Analytics
Scoop & Swirl tracks unique club members visiting stores. Guest purchases record CustomerID as BLANK.
| Customer ID Values in Filter | DISTINCTCOUNT() Result | DISTINCTCOUNTNOBLANK() Result | Explanation |
|---|---|---|---|
| 101, 102, 103, 101, 102 | 3 | 3 | 3 unique customers (101, 102, 103) |
| 101, 102, BLANK, 101, BLANK | 3 (Includes BLANK) | 2 (Excludes BLANK) | DISTINCTCOUNT counts BLANK as 1 distinct value |
Syntax & Core Workflow
Unique Customers = DISTINCTCOUNT(Fact_Sales[CustomerID])
Clean Active Customers = DISTINCTCOUNTNOBLANK(Fact_Sales[CustomerID])
Parameters Breakdown
ColumnName: The column containing values to count uniquely (e.g. `Fact_Sales[CustomerID]`).BLANK Handling: `DISTINCTCOUNT` includes BLANK as one distinct value; `DISTINCTCOUNTNOBLANK` discards it.Performance Impact: Distinct count is one of the most CPU-intensive DAX operations on large tables.
Sample Walkthrough (Q&A)
Question: When should an analyst use DISTINCTCOUNTNOBLANK instead of standard DISTINCTCOUNT?
Formula / Action: Whenever the column can contain null/blank entries that do NOT represent a real distinct entity.
Result: Prevents guest/anonymous transactions from inflating unique registered customer counts by +1.
Explanation: In e-commerce, anonymous guest checkouts leave CustomerID as null. `DISTINCTCOUNT` counts that null as a customer. `DISTINCTCOUNTNOBLANK` ignores it.
Common Mistakes & Limitations
- Using `DISTINCTCOUNT` on 100-million row tables across multiple visuals on a single page, causing severe visual lag.
- Assuming `COUNT()` and `DISTINCTCOUNT()` produce the same result (COUNT counts occurrences; DISTINCTCOUNT deduplicates).
Keyboard Shortcuts
| Action | Windows | Mac |
|---|---|---|
| Format Measure as Whole Number | Measure Tools -> Whole Number | Measure Tools |
| Add Thousands Separator | Measure Tools -> Click Comma (,) | Click Comma |
Pro Tips & Modern Alternatives
- If you need a distinct count of a dimension table's primary key, use `COUNTROWS(Dim_Customer)` instead—it is 10x faster.
- Pre-aggregate distinct user counts in your data warehouse if tables exceed 100 million rows.