COUNTROWS DAX Function (Aggregation)

Counts the number of rows in a table.

Syntax

COUNTROWS ( [<Table>] )
Parameter Attributes Description
Table Optional

The table containing the rows to be counted. If it is not specified, it uses the table containing the measure definition.

Return values

Scalar A single integer value.

Number of rows obtained by the evaluation of the table expression. If the table has no rows, it returns blank.

Remarks

This function can be used to count the rows of a table expression.

Even though the table argument is optional, it is a best practice to always specify the first argument to improve readability and simplify code refactoring when needed.

» 4 related articles
» 2 related functions

Examples

The following are valid syntaxes.

COUNTROWS ( table )
COUNTROWS ( DISTINCT ( table ) )
COUNTROWS ( VALUES ( table ) )

The COUNTROWS function can be used to count the unique values available in a column for the current filter context. However, DISTINCTCOUNT is better in that case. The following expressions are equivalent.

COUNTROWS ( DISTINCT ( table[column] ) )
DISTINCTCOUNT ( table[column] ) )

The COUNTROWS function can be used to check whether a column has only one item filtered/selected in the current filter context. However, HASONEVALUE is better in that case. The following expressions are equivalent.

COUNTROWS ( VALUES ( table[column] ) ) = 1
HASONEVALUE ( table[column] ) )
--  COUNTROWS counts the number of rows in a table
DEFINE
    MEASURE Customer[# Customers] = 
        COUNTROWS ( Customer )
    MEASURE Customer[# Countries 1] = 
        COUNTROWS ( DISTINCT ( Customer[CountryRegion] ) )
    MEASURE Customer[# Countries 2] = 
        DISTINCTCOUNT ( Customer[CountryRegion] )
EVALUATE
SUMMARIZECOLUMNS (
    Customer[Continent],
    "# Customers", [# Customers],
    "# Countries 1", [# Countries 1],
    "# Countries 2", [# Countries 2]
)
Continent # Customers # Countries 1 # Countries 2
Asia 3,658 15 15
North America 9,665 2 2
Europe 5,546 12 12
--  COUNTROWS is often used inside CALCULATE to count
--  the number of rows in a filtered table
DEFINE
    MEASURE Customer[# Individuals] =
        CALCULATE ( 
            COUNTROWS ( Customer ),
            Customer[Customer Type] = "Person"
        )
    MEASURE Customer[# Companies] =
        CALCULATE ( 
            COUNTROWS ( Customer ),
            Customer[Customer Type] = "Company"
        )
EVALUATE
SUMMARIZECOLUMNS (
    Customer[Continent],
    "# Individuals", [# Individuals],
    "# Companies", [# Companies]
)
Continent # Individuals # Companies
Asia 3,591 67
North America 9,389 276
Europe 5,504 42

Related articles

Learn more about COUNTROWS in the following articles:

Related functions

Other related functions are:

Last update: Dec 4, 2024   » Contribute   » Show contributors

Contributors: Alberto Ferrari, Marco Russo

Microsoft documentation: https://docs.microsoft.com/en-us/dax/countrows-function-dax

2018-2024 © SQLBI. All rights are reserved. Information coming from Microsoft documentation is property of Microsoft Corp. » Contact us   » Privacy Policy & Cookies

Context Transition

This function performs a Context Transition if called in a Row Context. Click to read more.

Row Context

This expression is executed in a Row Context. Click to read more.

Iterator

Not recommended

The use of this function is not recommended. See Remarks and Related functions for alternatives.

Not recommended

The use of this parameter is not recommended.

Deprecated

This function is deprecated. Jump to the Alternatives section to see the function to use.

Volatile

A volatile function may return a different result every time you call it, even if you provide the same arguments. Click to read more.

Deprecated

This parameter is deprecated and its use is not recommended.

DirectQuery compatibility

Limitations are placed on DAX expressions allowed in measures and calculated columns.
The state below shows the DirectQuery compatibility of the DAX function.

Contribute

Want to improve the content of COUNTROWS? Did you find any issue?
Please, report it us! All submissions will be evaluated for possible updates of the content.


This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.