DAX COUNTX Function
Get last date in the current context in DAX.
COUNTX is an iterator function in DAX that:
- Iterates over a table
- Evaluates an expression for each row
- Counts how many of those evaluated results are non-blank
Syntax
COUNTX(<table>, <expression>)
<table>: A real or virtual table to iterate over<expression>: The expression to evaluate for each row (usually a column, but can be more complex)
Example
Let’s say you have a Sales table:
| Product | Quantity | Discount |
|---|---|---|
| A | 2 | 5 |
| B | 3 | (blank) |
| C | 1 | 10 |
| D | 4 | 0 |
You want to count how many sales have a non-blank discount.
COUNTX(Sales, Sales[Discount])
- Step-by-step:
| Row | Sales[Discount] | Counted? |
|---|---|---|
| A | 5 | ✅ |
| B | BLANK() | ❌ |
| C | 10 | ✅ |
| D | 0 | ✅ |
Result = 3
- It counts 0 as a valid number (because it's not blank).
- It does NOT count blank values.
How is it Different from COUNT?
| Function | Behavior |
|---|---|
COUNT | Counts the number of non-blank values in a column only |
COUNTX | Evaluates an expression row-by-row, counts non-blanks |
When to Use COUNTX
Use COUNTX when:
- You need to evaluate a row-level expression, and count how many results are not blank
- You're using a virtual table (like with
FILTER,SUMMARIZE, etc.) - You want to do conditional counting without writing a full
CALCULATE+FILTERformula
Example
- Count Orders with Discount > 0
COUNTX(
FILTER(Sales, Sales[Discount] > 0),
Sales[Discount]
)
- This counts how many rows have a discount greater than 0
- It ignores 0 and blank values
Summary
| Feature | COUNTX |
|---|---|
| Function Type | Iterator |
| Evaluates | Row-by-row expression |
| Counts | Only non-blank results |
| Use with filters? | ✅ Yes works great with FILTER, VALUES, etc. |
No questions available.