Skip to main content

Command Palette

Search for a command to run...

Power BI DAX: CALCULATE, DISTINCTCOUNT

Updated
2 min read
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

Product Categories Sold = 
CALCULATE(
DISTINCTCOUNT('Product'[ProductCategory]),
'Date'
)

DISTINCTCOUNT is a DAX function that counts the number of unique values in a column. It's particularly useful when you need to count distinct items, such as unique product categories, customers, or any other distinct entities.

Example Scenario

Imagine you have a sales dataset with various products sold over different dates. Each product belongs to a specific category. You want to create a measure that counts the number of unique product categories that had sales during a selected period.

DAX Expression

To achieve this, you can use the DISTINCTCOUNT function within a CALCULATE function to filter the data based on the selected period.

Product Categories Sold = 
CALCULATE(
DISTINCTCOUNT('Product'[ProductCategory]),
'Date'
)

Illustration

Consider the following context:

DateProductProductCategory
2024-09-01Product ACategory 1
2024-09-02Product BCategory 2
2024-09-03Product CCategory 1
2024-09-04Product DCategory 3
2024-09-05Product ECategory 2

If you select the period from 2024-09-01 to 2024-09-05, the measure Product Categories Sold will count the number of unique product categories that had sales during this period. In this case, the result would be 3 (Category 1, Category 2, and Category 3).

This measure dynamically calculates the number of distinct product categories based on the selected date range, providing valuable insights into the diversity of products sold over time.