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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
techsfaction
Regular Visitor

How to filter data based on other table field in power bi

I have 3 Tables Company, Employee, Card  as shown in the screenshot below; the tables have relations between them.

We can notice from the card table that not every company has a card or multiple cards related to it,

 

Requirement

  • I want to create a donut-chart to show the number of companies based on if the company has at least one card or "NO" (no card).

 

Example here; 50% Hold a card and 40% NO.

companies: 1,5,6,7,10 (have cards)

companies: 2,3,4,8,9 (Don't)

 

My solution

I merged the card_ID column from the Card table to the Company table (left join with Card table as second),

then created a new column "Card_Holder" that indicates "YES" if the card_ID Column is not empty and "NO" if its empty.

but when I applied the changes, power bi found a duplicate ID in the Company Table which is true, because the above steps created more than one record for each company based on the card table.

 

My solution is not good and its not working, and probably I am thinking with excel.

 

What is the appropriate method to do that in power BI? 

 

POWERBI.PNG

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @techsfaction ,

 

In this case you have 50% companies with cards and 50% without cards so you can do two measures like this:

 

Cards = calculate(DISTINCTCOUNT(Companies[Company_ID]), Companies[Company_ID] in VALUES(Cards[Company_id]))

No Cards = calculate(DISTINCTCOUNT(Companies[Company_ID]),NOT( Companies[Company_ID] in VALUES(Cards[Company_id])))

 

Result below:

MFelix_0-1653553811689.png

The model is the same has you have.

 

Another option is to create a calculated column on the Companies on your model using dax or Power Query:

DAX:

 

Cards = if(CALCULATE(COUNT(Cards[Card_id])) > 0, "Card", "No Card")

MFelix_1-1653553921581.png

 

Query Editor:

  • Merge tables Companies with cards
  • Expand but select the count of non blank:

MFelix_2-1653554110145.png

  • Add a custom column or a replace values to get Card No Card
  • In my case I used the replaced with the followiung syntax:
= Table.ReplaceValue(#"Renamed Columns",each [CardNCard] , each if [CardNCard] = 0 then "No Card" else "Card" ,Replacer.ReplaceValue,{"CardNCard"})

MFelix_3-1653554382397.png

 

Now you can use the ID company on the visualization and the card no card column has legend:

MFelix_4-1653554433204.png

Check PBIX attach.

 

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

2 REPLIES 2
v-henryk-mstf
Community Support
Community Support

Hi @techsfaction ,

 

Very much approve the solution given by @MFelix  If the problem has been solved you can mark the reply for the standard answer to help the other members find it more quickly. If not, please point it out.


Looking forward to your feedback.


Best Regards,
Henry

MFelix
Super User
Super User

Hi @techsfaction ,

 

In this case you have 50% companies with cards and 50% without cards so you can do two measures like this:

 

Cards = calculate(DISTINCTCOUNT(Companies[Company_ID]), Companies[Company_ID] in VALUES(Cards[Company_id]))

No Cards = calculate(DISTINCTCOUNT(Companies[Company_ID]),NOT( Companies[Company_ID] in VALUES(Cards[Company_id])))

 

Result below:

MFelix_0-1653553811689.png

The model is the same has you have.

 

Another option is to create a calculated column on the Companies on your model using dax or Power Query:

DAX:

 

Cards = if(CALCULATE(COUNT(Cards[Card_id])) > 0, "Card", "No Card")

MFelix_1-1653553921581.png

 

Query Editor:

  • Merge tables Companies with cards
  • Expand but select the count of non blank:

MFelix_2-1653554110145.png

  • Add a custom column or a replace values to get Card No Card
  • In my case I used the replaced with the followiung syntax:
= Table.ReplaceValue(#"Renamed Columns",each [CardNCard] , each if [CardNCard] = 0 then "No Card" else "Card" ,Replacer.ReplaceValue,{"CardNCard"})

MFelix_3-1653554382397.png

 

Now you can use the ID company on the visualization and the card no card column has legend:

MFelix_4-1653554433204.png

Check PBIX attach.

 

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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