What COUNTX does
Count rows where an expression returns a non-blank value. In Power BI, the key is not only the formula itself but how it behaves with slicers, relationships, visuals and totals.
Syntax or pattern
COUNTX(table, expression)5 practical business examples
COUNTX in a sales report
Calculate revenue row by row.
Revenue = COUNTX(Sales, Sales[Quantity] * Sales[Unit Price])Use an iterator when the calculation must happen at transaction level.
Profit by row
Calculate profit from each row before aggregating.
Profit = COUNTX(Sales, Sales[Sales Amount] - Sales[Cost Amount])This is safer than subtracting totals when row logic matters.
Weighted score
Calculate a weighted score from rows.
Weighted Score = COUNTX(Scores, Scores[Score] * Scores[Weight])Good for performance scorecards.
Discount impact
Calculate discount value by line item.
Discount Impact = COUNTX(Sales, Sales[List Price] * Sales[Discount %])Use this to analyze discount cost.
Inventory value
Calculate stock value from quantity and cost.
Inventory Value = COUNTX(Inventory, Inventory[Units On Hand] * Inventory[Unit Cost])Useful in inventory valuation reports.
Common mistakes to avoid
- Using COUNTX before checking whether the data model has the right relationships and filter direction.
- Writing one complex measure instead of creating simple base measures first.
- Testing only at the total level and not checking row, category and date contexts.
- Forgetting that slicers, visuals and relationships can all change the filter context.
FAQ
When should I use COUNTX in DAX?
Use COUNTX when the calculation pattern matches the business question and the result behaves correctly in the current filter context.
Why is my COUNTX measure returning the wrong total?
Most total issues come from row context, filter context, relationships, or using a column aggregation where an iterator or CALCULATE pattern is needed.
Can I use this COUNTX pattern in a calculated column?
Some patterns work in calculated columns, but most reporting calculations should be measures so they respond to slicers and report filters.