Sub topic 11
Counting Rows of a Filtered Table with COUNTROWS and FILTER
This chapter assumes you can create a measure in Power BI Desktop. It also assumes you have a Sales table with a numeric SalesAmount column, and you have already written Total Sales and Order Lines. You will extend COUNTROWS so it counts only the rows that meet a condition.
Why COUNTROWS takes a table expression
COUNTROWS counts the rows of a table. The argument does not have to be a table name. It can be any expression that returns a table. That lets you shrink a table first and then count what is left.
FILTER is the function that does the shrinking. It takes a table and a true/false condition, and it returns only the rows where the condition is true.
Large Order Lines = COUNTROWS(FILTER(Sales, Sales[SalesAmount] > 1000))This measure counts only the Sales rows whose SalesAmount is greater than 1000. FILTER evaluates the condition once for each row of Sales. It keeps the rows that pass, and COUNTROWS counts them.
Following the evaluation
The measure runs from the inside out.
The first step matters. FILTER(Sales, ...) does not start from every row in the table. It starts from the rows left visible by the filter context, which is the set of filters coming from slicers, visuals, and other report filters. If a slicer limits the report to 2025, Large Order Lines counts large order lines in 2025 only.
A worked example
Suppose the Sales table holds five order lines.
| OrderLine | Region | SalesAmount |
|---|---|---|
| 1 | East | 400 |
| 2 | East | 1200 |
| 3 | West | 2500 |
| 4 | West | 900 |
| 5 | West | 1500 |
Place a table visual with Region and these measures on a report page.
| Region | Order Lines | Large Order Lines |
|---|---|---|
| East | 2 | 1 |
| West | 3 | 2 |
| Total | 5 | 3 |
For the East row, the filter context shows only lines 1 and 2. FILTER keeps line 2 because 1200 is above 1000. For West, it keeps lines 3 and 5. The Total row has no region filter, so all five lines are tested, and three pass.
Combining it with other measures
A filtered count is an ordinary measure, so other measures can reference it. A common use is a share of large lines:
Large Order Line Share = DIVIDE([Large Order Lines], [Order Lines])DIVIDE returns BLANK if there are no order lines in the current context. The pattern is the same as in the earlier ratio chapter: build small measures, then divide one by the other.
Common errors
- Using a column where a table is expected.
COUNTROWS(Sales[SalesAmount])fails because a column reference is not a table. PassSalesor aFILTERexpression. - Comparing to a measure inside FILTER without care. The condition in
FILTERruns row by row. WritingSales[SalesAmount] > [Total Sales]compares each row to the total of the current context, which is rarely what you want. Compare a column to a constant or to another column of the same row. - Forgetting that BLANK is possible. If no row passes the filter,
COUNTROWSreturns BLANK, not 0. In a visual, that group may disappear or show an empty cell. Add+ 0if you want a zero:
Large Order Lines (0 if none) = COUNTROWS(FILTER(Sales, Sales[SalesAmount] > 1000)) + 0- Hard-coding the threshold in many places. If 1000 appears in several measures, changing the definition of "large" means editing each one. Keep the threshold in one measure, or in a parameter table once you learn that technique.
Check your understanding
Exercise
Write a measure named Small Order Lines that counts the Sales rows whose SalesAmount is 1000 or less. Using the sample table in this chapter, what would it return for the West region?
Show answer
Small Order Lines = COUNTROWS(FILTER(Sales, Sales[SalesAmount] <= 1000)). For West, the filter context contains lines 3, 4, and 5 with amounts 2500, 900, and 1500. Only line 4 (900) is 1000 or less, so the measure returns 1.
Exercise
A slicer limits the report to the East region. Does Large Order Lines count large lines from West as well? Explain why or why not.
Show answer
No. FILTER starts from the Sales rows left visible by the filter context, and the slicer removes the West rows before FILTER runs. Only East lines are tested, so the result is 1.