Sub topic 5
Counting rows with COUNTROWS
This chapter assumes you can work in Power BI Desktop's Report view and have a Sales table loaded in which each row is one order line. You should also know how to create a measure from the formula bar and how SUM behaves under filters. The chapter covers COUNTROWS and a few common mistakes. The last section previews how it works with a filtered table.
What COUNTROWS does
COUNTROWS takes a table and returns the number of rows in it. It does not look at any column's values. It counts rows, nothing else.
Order Lines = COUNTROWS(Sales)If each row in Sales is one order line, this measure gives the number of order lines. It is also an explicit measure. You can drop it into any visual, and you can reference it from other measures, as the next chapter does.
COUNTROWS respects filter context
COUNTROWS(Sales) counts the rows of Sales that are visible in the current filter context. A slicer, a visual axis, or a report filter can each narrow the rows before the count happens.
Suppose Sales has 1,000 rows. A card showing Order Lines displays 1,000. If a slicer selects one year and 240 rows belong to that year, the same card shows 240. A table visual with one row per product category shows the count for each category separately. The formula never changes. Only the rows it sees change.
COUNTROWS versus other counting functions
DAX has several counting functions. Pick the one that matches the question you are asking.
| Function | What it counts | Typical use |
|---|---|---|
| `COUNTROWS(Table)` | Rows in a table | Number of order lines, orders, customers in a dimension table |
| `COUNT(Table[Column])` | Non-blank values in a numeric column | Rarely needed when you want rows |
| `COUNTA(Table[Column])` | Non-blank values of any type | Counting filled-in fields |
| `DISTINCTCOUNT(Table[Column])` | Different values in a column | Number of unique customers |
For counting rows, COUNTROWS is the clearest choice. It states the intent directly and does not depend on a column that may contain blanks.
Create the measure
- In the Fields pane, select the
Salestable. - Choose New measure on the Home or Table tools ribbon.
- Type the formula in the formula bar and press Enter.
Order Lines = COUNTROWS(Sales)Then add the measure to a card visual and add a slicer on a date or category column. Change the slicer selection and watch the card update.
Common errors
- Passing a column instead of a table.
COUNTROWS(Sales[SalesAmount])is an error, because the argument must be a table or a table expression. UseCOUNTROWS(Sales). - Counting the wrong table. If
Salesholds order lines, counting it gives lines, not orders. To count orders, you need a column that identifies each order, and you would useDISTINCTCOUNTon it. - Expecting a zero. When filters leave no rows,
COUNTROWSreturns BLANK, not 0. A visual hides that row or shows an empty cell. If you need a zero, add it explicitly, for exampleCOUNTROWS(Sales) + 0. - Using it as a calculated column. A calculated column evaluates once per row, so
COUNTROWS(Sales)there returns the same total in every row. Counting belongs in a measure.
Order Lines (0 if none) = COUNTROWS(Sales) + 0A preview: counting a filtered table
Because the argument can be any table expression, you can count only the rows that meet a condition. FILTER returns the rows of a table for which a test is true.
Large Order Lines = COUNTROWS(FILTER(Sales, Sales[SalesAmount] > 1000))This counts only the Sales rows whose SalesAmount is greater than 1000. It still respects the filters from slicers and visuals. The slicer narrows the rows first, and then FILTER narrows them further.
Check your understanding
Exercise
A card shows Order Lines = COUNTROWS(Sales) as 1,000. You add a slicer and select a single region that has 130 rows in Sales. What does the card show, and why? What would COUNTROWS(Sales[SalesAmount]) do?
Show answer
The card shows 130. The slicer filters Sales to the rows for that region, and COUNTROWS counts the rows left in the filter context. COUNTROWS(Sales[SalesAmount]) produces an error, because COUNTROWS needs a table or table expression, not a column.
Exercise
Write a measure named Small Order Lines that counts the Sales rows where SalesAmount is 100 or less.
Show answer
Small Order Lines = COUNTROWS(FILTER(Sales, Sales[SalesAmount] <= 100))