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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Count of rows based on the id column between two tables

I have 2 tables


table1

ID 
1
2
3
4
5
6
7

Table 2


ID       code
1       56265001
1       38341003
1      56265001
2      709044004
2     13645005
3     73211009

 

Table1 - id column is primary key
table2- id column is foreign key
Requirment:-

if table2 count of code >= 2  based on the id column then "categroy1"
if table2 count of code = 1 based on the id column then "category2"
if no any entry present in the table 2  based on the id then "category3"


output:-

 

Type             count of id

Category1          2
category2            1
category3            4

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , Try a new column in table 1

new column =
var _cnt =countx(filter(Table2,table1[ID] = table2[ID] ),Table2[Code])+0

return
switch(true(),
_cnt=0, "category3"
,_cnt=1, "category1"
,"category2")

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

View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@Anonymous , Try a new column in table 1

new column =
var _cnt =countx(filter(Table2,table1[ID] = table2[ID] ),Table2[Code])+0

return
switch(true(),
_cnt=0, "category3"
,_cnt=1, "category1"
,"category2")

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

@amitchandak  Thank you .Its working..

Anonymous
Not applicable

Hi @Anonymous 

Could you tell me if your problem has been solved? If it is, kindly Accept the helpful reply as the solution. More people will benefit from it. Or you are still confused about it, please provide me with more details about your table and your problem or share me with your pbix file from your Onedrive for Business.

 

Best Regards,

Rico Zhou

dedelman_clng
Community Champion
Community Champion

Hi @Anonymous , try this

 

Category = 
var __cnt = CALCULATE(COUNTA(Tab2[Code]), FILTER(Tab2, Tab2[ID] = Tab1[ID]))
return
SWITCH(TRUE(),
__cnt >=2, "Category 1", __cnt=1, "Category 2", "Category 3")

 

Hope this helps

David

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.