Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!
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_id | market_id | entity | active |
1 | 10 | nyc | true |
2 | 20 | alaska | true |
2 | 20 | alaska | true |
2 | 30 | miami | false |
3 | 40 | dallas | true |
markets:
location_id | market_id | active |
2 | 20 | true |
2 | 20 | true |
5 | 20 | true |
6 | 20 | false |
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
Example 2: When miami selected, where active is False in markets table, the output does not show anything
Hope this helps!!
If this is what you need, mark it as solution and don't hesitate to appreciate with kudos!!!
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?
@jazzk , 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/
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?
User | Count |
---|---|
41 | |
27 | |
23 | |
19 | |
16 |
User | Count |
---|---|
54 | |
35 | |
19 | |
19 | |
15 |