Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
jazzk
Helper I
Helper I

DAX query for distinct count with mutiple filters

I have two tables 'locations' and 'markets', where, a many to many relationship exists between these two tables on the column 'market_id'. A report level filter has been applied on the column 'entity' from 'locations' table. Now, I'm supposed to distinctly count the 'location_id' from 'markets' table where 'active=TRUE'. How can I write a DAX query such that the distinct count of location_id dynamically changes with respect to the selection made in the report level filter?

 

Below is an example of the tables:

 

locations:

location_idmarket_identityactive
110nyctrue
220alaskatrue
220alaskatrue
230miamifalse
340dallastrue

 

markets:

location_idmarket_idactive
220true
220true
520true
620false

 

5 REPLIES 5
Thejeswar
Resident Rockstar
Resident Rockstar

Hi @jazzk ,

After performing many-to-many join using bridge table, you can use the below DAX measure to achieve what you want

 

Measure = 
CALCULATE(DISTINCTCOUNT(markets[location_id]), markets[active] = "True")

For explaining, I added some more data to those tables.

Below screenshots shows the usages

 

Example 1:

Note although there are 3 location under marketid = 20, since only two of them have active status as True, the table shows 2 as the output. It is also filtered by entity

Thejeswar_1-1595433952007.png

 

Example 2: When miami selected, where active is False in markets table, the output does not show anything

Thejeswar_2-1595433998920.png

 

 Hope this helps!!

If this is what you need, mark it as solution and don't hesitate to appreciate with kudos!!!

az38
Community Champion
Community Champion

@jazzk 

Not sure I understand you correct but try a measure like

Measure = 
CALCULATE(DISTINCTCOUNT(markets[location_id]), markets[active]=true(), treatas(VALUES(locations[location_id]), markets[location_id]))

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Hi @az38! If I understood Treatas properly, what it does is that whatever filter is applied to 'locations[location_id]', the same filter gets applied to markets[location_id]. However, I think I'm looking for a different solution. Basically a report level filter is applied on the column 'locations[entity]' and whichever value is selected in the entity, I should get the distinct count of location_id after connecting the two tables 'locations' and 'markets' on many to many relationship. Could you please help me with this?

Hi @amitchandak! If I understood Treatas properly, what it does is that whatever filter is applied to 'locations[location_id]', the same filter gets applied to markets[location_id]. However, I think I'm looking for a different solution. Basically a report level filter is applied on the column 'locations[entity]' and whichever value is selected in the entity, I should get the distinct count of location_id after connecting the two tables 'locations' and 'markets' on many to many relationship.. Could you please help me with this?

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors