<!-- https://learnsql.cpetoday.com/functions/dax/countrows -->
# COUNTROWS

Language: DAX
Category: Aggregation
Syntax: COUNTROWS(<table>)
Summary: Counts the rows in a table.
Returns: A whole number (a scalar).
Level: Beginner
Related: COUNT, COUNTA, COUNTX, FILTER, DISTINCTCOUNT, CALCULATE

## What it does
COUNTROWS counts the rows of a table that are visible in the current filter context. It counts rows, not values, so a row with blanks in every column still counts. It is the standard way to count transactions, invoices, orders or journal lines. Pass it a filtered table to count only the rows that meet a condition.

## Syntax and arguments
- table: a table reference such as `Sales`, or a table expression such as `FILTER(Sales, Sales[ReturnAmount] > 0)`.

## Example
The operations manager wants the number of sales transactions in the selected period, to compare with the number of orders shipped.

```dax
Sales Lines = COUNTROWS(Sales)
```

Under a date slicer for one quarter, the measure counts the lines in that quarter. Placed next to Total Sales, it shows the average value per line when divided with DIVIDE.

## Example: a second, different use
Count the customers who bought in the period, using a table of distinct customers:

```dax
Active Customers =
COUNTROWS(
    FILTER(
        VALUES(Sales[CustomerKey]),
        [Total Sales] > 0
    )
)
```

VALUES gives one row per customer, and FILTER keeps the customers with positive sales. COUNTROWS then counts them. This is a count of distinct customers, which a plain COUNTROWS(Sales) is not.

## Things to watch
- COUNTROWS counts every row, including duplicates. Use DISTINCTCOUNT or COUNTROWS(VALUES(...)) for distinct items.
- A table function is required. COUNTROWS(Sales[SalesAmount]) is an error. Use COUNT or COUNTA for a column.
- The result depends on the filter context. Relationships can filter the fact table from a dimension, so a slicer on Product changes the count of Sales rows.
- Counting a large fact table without a filter is fast, but counting a filtered list of distinct values inside a measure can be slow.
- Blank values in a row do not stop it from counting. A row is a row.

## In business terms
COUNTROWS is the first measure most finance teams build: number of invoices, number of journal lines, number of payroll records, number of open tickets. It is also the basis of average transaction size, which is total divided by this count.
