COUNTIF in DAX: counting with a condition in Power BI
In Excel, COUNTIF counts the cells that meet a condition. DAX has no such function. You build it from COUNTROWS and a filter, and the result then adapts to every row of the matrix.
In short: instead of =COUNTIF(B:B,"North"), in Power BI you write the measure CALCULATE(COUNTROWS(Sales), Region[Region] = "North"). For conditions with greater than or less than, use COUNTROWS(FILTER(Sales, Sales[Amount] > 500)).
The patterns at a glance
| Excel | DAX in Power BI | Result in the sample model |
|---|---|---|
| COUNTIF with text | Orders North = CALCULATE(COUNTROWS(Sales), Region[Region] = "North") | 32 |
| COUNTIF with > | Large Orders = COUNTROWS(FILTER(Sales, Sales[Amount] > 500)) | 7 to 15 per region |
| COUNTIFS (several conditions) | Hardware 3 Plus = CALCULATE(COUNTROWS(Sales), Product[Category] = "Hardware", Sales[Quantity] >= 3) | 21 |
| Count distinct values | Products Sold = DISTINCTCOUNT(Sales[ProductID]) | 6 per region |
| COUNTIF per row (column) | Orders = COUNTROWS(RELATEDTABLE(Sales)) as a column in Product | Laptop 17, Mouse 29 |
The examples run on the sales model from the course with the tables Sales, Product, Region and Date. We compared every result with Power BI Desktop. You can try them in the DAX playground.
Why you do not need COUNTIF
In Excel, COUNTIF says which range to count and under which condition. In Power BI part of the condition already comes from the matrix: if the region is in the row, COUNTROWS(Sales) automatically counts only that region's orders. You only add the conditions that do not come from the row. That is exactly what CALCULATE does. The lesson on CALCULATE explains how it works.
CALCULATE with Region[Region] = "North" replaces the filter on the region. In a matrix by region every row then shows 32. If the condition should apply in addition, write KEEPFILTERS(Region[Region] = "North").
Conditions with greater than, less than or a measure
A condition on a single column, such as Sales[Amount] > 500, can go straight into CALCULATE. If it depends on several columns or a measure, go through the table row by row with FILTER: COUNTROWS(FILTER(Sales, Sales[Quantity] * RELATED(Product[Price]) > 1000)). More in the lesson on SUMX and iterators.
Common mistakes
COUNT with a condition
COUNT(Sales[Amount] > 500) does not exist. Power BI says: "The COUNT function only accepts a column reference as an argument." COUNT counts the non-blank values of a column; the condition belongs in CALCULATE or FILTER.
Typing COUNTIF as a function
Power BI does not know COUNTIF and reports an unknown name.
Blank instead of 0
If there is nothing to count in a row, the result is blank and the matrix hides the row. If you want 0 there, write COALESCE(CALCULATE(COUNTROWS(Sales), …), 0).
Keep learning
- Measures: practice COUNTROWS, DISTINCTCOUNT and SUM in the course.
- DAX functions: all important functions with an example and a cheat sheet.
- Power BI for Excel users: what stays the same from Excel and what is new.