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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

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
Super User
Super User

Hi @Anonymous ,

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

@Anonymous 

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
Anonymous
Not applicable

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?

amitchandak
Super User
Super User

@Anonymous , First you use a bridge table

https://www.seerinteractive.com/blog/join-many-many-power-bi/

 

Try treatas

https://docs.microsoft.com/en-us/dax/treatas-function

https://www.sqlbi.com/articles/propagate-filters-using-treatas-in-dax/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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