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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Count records in a related table by ID

I have 2 related tables joined on ID. Table1 is a summary table with 1 record per ID and Table2 is a detail table with multiple records per ID. I need a new column in Table1 with a count of records in Table2 that have the same ID. In my example below, I need the formula for the "Record Count" column. Thoughts?

 

Table1

IDRecord Count
1234
4562

 

Table2

IDValue
123asd
123sdf
123dfg
123fgh
456asd
456dfg
1 ACCEPTED SOLUTION
Anonymous
Not applicable

This might work:

 

CALCULATE(COUNTAX(Table2,Table2[Value]),Table2[ID])

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

 

In Table1, write this calculated column formula

 

=COUNTROWS(RELATEDTABLE(Table2))

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

This might work:

 

CALCULATE(COUNTAX(Table2,Table2[Value]),Table2[ID])
Anonymous
Not applicable

Perfect, thanks!

Hi @Anonymous,

 

I realize that this is solved, however why do you need a new column when you can use a measure like,

count = countrows('table2') 

this approach keeps you model leaner and does not require additional work while refreshing.

 

Richard



I hope this helps,
Richard

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

Proud to be a Super User!


Anonymous
Not applicable

Thanks for the reply. While this works well for grid and chart values, a measure can't be used in slicers and filters. Something that I didn't specify in my post is that I need to be able to filter records where count>X. Otherwise, I like this solution for other applications. Thanks!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.