'How to count rows in a table, where a specific column value is in a list of specified values?
How do I create a DAX measure to count the number of customers in a customer table dCustomers
, where the customerType
is either FR
, DE
or GG
?
In SQL, this would be something like:
SELECT COUNT(customerId)
FROM dCustomers
WHERE customerType IN ('FR', 'DE', 'GG')
Solution 1:[1]
I would suggest using the IN
operator, to write it in a more concise manner:
Count (Calculate) =
CALCULATE (
COUNTROWS ( dCustomers ) ,
dCustomers[customerType] IN {"FR", "DE", "GG"}
)
You can also use COUNTROWS
directly on the filtered table, this looks to be marginally faster from a bit of testing on a random dataset, but please do benchmarking on a case by case basis:
Count (Filter) =
COUNTROWS (
FILTER (
dCustomers ,
dCustomers[customerType] IN {"FR", "DE", "GG"}
)
)
Solution 2:[2]
You can create a measure and use CALCULATE with appropriate filter, like this:
Count In List = CALCULATE(COUNT(dCustomers[customerId]),
dCustomers[customerType] = "FR" ||
dCustomers[customerType] = "DE" ||
dCustomers[customerType] = "GG")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Andrey Nikolov |