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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register 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]))

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
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.