Distinctcount and Countrows in DAX
When to use Distinctcount and Countrows Function in DAX
Distinctcount:
Counts the number of distinct values in a column.
Expression:
Count_Of_EmpID =CALCULATE(
DISTINCTCOUNT('Table'[EmpID]),
ALLEXCEPT('Table', 'Table'[EmpID])
)
Explanation:
This DAX expression calculates the count of distinct employee IDs in the 'Table' table, excluding any filters on other columns. The ALLEXCEPT function removes all filters from the 'Table' table except for the EmpID column, ensuring that the count is performed on all rows in the table.
If we don't want to count EmpID whose having as 0 then follow below Expression
Count_Of_EmpID=CALCULATE(
DISTINCTCOUNT('Table'[EmpID]),
ALLEXCEPT('Table', 'Table'[EmpID]),
'Table'[EmpID] <> 0
)
This expression adds an additional filter to the ALLEXCEPT function, excluding rows where the EmpID column is equal to 0. As a result, the count of distinct employee IDs will only include non-zero values.
Here's a breakdown of the expression:
* `CALCULATE`: This function allows you to evaluate an expression within a modified filter context.
* `DISTINCTCOUNT('Table'[EmpID])`: This expression counts the number of distinct values in the EmpID column of the 'Table' table.
* `ALLEXCEPT('Table', 'Table'[EmpID])`: This function removes all filters from the 'Table' table except for the EmpID column.
* `'Table'[EmpID] <> 0`: This filter excludes rows where the EmpID column is equal to 0.
By combining these elements, the expression calculates the count of distinct employee IDs, excluding any filters on other columns and excluding rows where the EmpID column is equal to 0.
Countrows:
The COUNTROWS function counts the number of rows in the specified table, or in a table defined by an expression.
Count _rows = COUNTROWS(Sales)
Explanation:
* The COUNTROWS function counts the number of rows in a table or a specified range of cells.
* Sales: This refers to the 'Sales' table in your Power BI model.
* The result of this expression will be a scalar value, which is the total number of rows in the 'Sales' table.
Example 2:
To count the number of rows in the 'Sales' table where the 'Product' column is equal to "Product A", you can use the following expression:
Count_rows_ex2 = COUNTROWS(FILTER(Sales, Sales[Product] = "Product A"))
Explanation:
* The FILTER function filters the 'Sales' table based on a specified condition. In this case, it filters the table to include only rows where the 'Product' column is equal to "Product A".
* The COUNTROWS function then counts the number of rows in the filtered table.
* The result of this expression will be a scalar value, which is the number of rows in the 'Sales' table where the 'Product' column is equal to "Product A".
Comments
Post a Comment