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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Kostas
Helper IV
Helper IV

TopN First Value based on Count in another table

Hello, 

 

I am kind of stuck in something that I feel is easy to solve and I need your help (once again). 

I got two tables connected with a relationship 1 to many like the below:

Table 1

IDReason ID
1##1
2##2
3##2
4##3

 

Table 2

Reason IDReason Name
##1Name 1
##2Name 2
##3Name 3
##4Name 4

[Table 1]  *--1 [Table 2] (both way direction)

 

I want to create cards (or card by placing all the calculations inside the measure and concatenate the results) in which I will show with a calculation the following:

Top Reason by distinct count of ID --> Based on tables above the result will be: "Name 2"
Distinct Count of ID which correspond to the Top Reason --> Based on tables above the result will be: 2
Proportion of the Distinct Count of ID which correspond to the Top Reason in comparison with the total distinct count of ID's in the first table --> Based on tables above the result will be: 2/4 (total Population) = 50%

I hope that makes sense, I tried to use the TOPN function in combination with the MAX but didn't make it. 
Any suggestions? 

Thanks

1 ACCEPTED SOLUTION
Kostas
Helper IV
Helper IV

Managed at least to find how to syntax the formula based on the answer @amitchandak gave me. 

 

Top Reason Card = 
Calculate all the available ID's in table1
Var NumberofID = 
DISTINCTCOUNT(table1[ID])
// return the reason with the biggest count of ID's (first ask)
Var TopReason = 
CALCULATE(
    MAX(table2[Reason Name]), topn(1, ALLSELECTED(table2[Reason Name]), calculate(COUNT(table1[ID])), DESC), VALUES(table2[Reason Name])
)
// calculate the count of ID's under the reason with the biggest count (ask 2)
Var CalcReference = CALCULATE(DISTINCTCOUNT(table1[ID]), table2[Reason Name] = TopReason)
// calculate the percentage in comparison to the total population (ask 3) 
Var PercReference = FORMAT(CalcReference/NumberofID,"##%")

Return 
Var TopReason & " - " & CalcReference  & " / " & CalcReference 
// that will return 
Name 2 - 2 / 50%

 

View solution in original post

3 REPLIES 3
Kostas
Helper IV
Helper IV

Managed at least to find how to syntax the formula based on the answer @amitchandak gave me. 

 

Top Reason Card = 
Calculate all the available ID's in table1
Var NumberofID = 
DISTINCTCOUNT(table1[ID])
// return the reason with the biggest count of ID's (first ask)
Var TopReason = 
CALCULATE(
    MAX(table2[Reason Name]), topn(1, ALLSELECTED(table2[Reason Name]), calculate(COUNT(table1[ID])), DESC), VALUES(table2[Reason Name])
)
// calculate the count of ID's under the reason with the biggest count (ask 2)
Var CalcReference = CALCULATE(DISTINCTCOUNT(table1[ID]), table2[Reason Name] = TopReason)
// calculate the percentage in comparison to the total population (ask 3) 
Var PercReference = FORMAT(CalcReference/NumberofID,"##%")

Return 
Var TopReason & " - " & CalcReference  & " / " & CalcReference 
// that will return 
Name 2 - 2 / 50%

 

amitchandak
Super User
Super User

@Kostas , Try a measure like

 

CALCULATE(concatenatex(Table, Table[ID]) ,TOPN(1,allselected(Table2[Reason Name]),calculate(Count(Table1[ID])),DESC),VALUES(Table2[Reason Name]))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Hello @amitchandak 
It seems that the measure returns a concatenation of all the ID's from the first table that refer to the reason with most ID's assigned to it. 
I need the card to return the "Reason" that has the biggest population in terms of count of ID from the table 1 (as shown above the Reason Name 2 has 2 ID's assigned to it compared with the other reasons that have only 1. 
Also, another measure that will return to me the count of ID's that are assigned to the reason that has the most (as mentioned above the number will be 2).

I hope that helps

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.