Forum Discussion
Measure with multiple filters on same field
Dear all,
I'm trying to create a measure to show the number of accounts that have the following attributes:
- Status: active
- Relationship Type: Customer, (blank), Non Buying Entity
I used this measure but I get an error.
SUM (Account[Account Number]),
FILTER (
Account[Relationship Type] in { "Customer","","Non Buying Entity"}
))
FILTER can handle multiple conditions as long as they are on the same table, you just need to join them with && for and and || for or conditions.
If you are using them inside a CALCULATE statement then you don't need to explicitly specify FILTER, you can pass in as many filter conditions as you like and they can be across multiple tables.
For the relative date filter, if you have a proper date table set up you could use something like
Within last 3 months = VAR StartDate = EOMONTH ( TODAY (), -4 ) + 1 RETURN CALCULATE ( [My measure], DATESBETWEEN ( 'Date'[Date], StartDate, TODAY () ) )
7 Replies
- johnt75Super User
Try
_Measure Customer Accounts = CALCULATE ( COUNTROWS ( Account ), Account[Status] = "Active", Account[Relationship Type] IN { "Customer", BLANK (), "Non buying entity" } )- KMZ_ESSHelper I
Perfect, that works! Thanks a lot for the fast response! 🙂
Would you have some advice on the other questions I added later, too? ->
Also, I will need some more measures where more than 2 fields have to be filtered. Is that possible? I seem to recall the FILTER function can only handle 2 arguments.Lastly, I would like to have the relative date filter (see screenshot) in a measure. How can I do that?Thanks in any case! 🙂- johnt75Super User
FILTER can handle multiple conditions as long as they are on the same table, you just need to join them with && for and and || for or conditions.
If you are using them inside a CALCULATE statement then you don't need to explicitly specify FILTER, you can pass in as many filter conditions as you like and they can be across multiple tables.
For the relative date filter, if you have a proper date table set up you could use something like
Within last 3 months = VAR StartDate = EOMONTH ( TODAY (), -4 ) + 1 RETURN CALCULATE ( [My measure], DATESBETWEEN ( 'Date'[Date], StartDate, TODAY () ) )
- shavishHelper I
What's the difference between following code and one above, if you can please provide technical answer:
_Measure Customer Accounts = CALCULATE ( COUNTROWS ( Account ), FILTER(Account[Status] = "Active" && Account[Relationship Type]="Customer"&& Account[Relationship Type]="Non buying entity"))I did face similar problem as described but was able to fix with your code. But i ran into another problem, the measure value in card is not changing when selecting Non buying entity in slicer settings with your code. What could be problem?
- johnt75Super User
The code you posted won't work for a couple of reasons. The first argument to FILTER needs to be a table, not a filter condition. Secondly you are trying to place two filters on the [Relationship Type] column using an AND condition - the column cannot have both values at the same time.
If you want to use my original solution but have it react to slicers as well you can wrap either or both of the filter condition lines in KEEPFILTERS. That way it will intersect the filters specified in the measure with filters from slicers, rather than overwriting them.