cancel
Showing results for
Did you mean:

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

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

 ID Reason ID 1 ##1 2 ##2 3 ##2 4 ##3

Table 2

 Reason ID Reason Name ##1 Name 1 ##2 Name 2 ##3 Name 3 ##4 Name 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
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%``````

3 REPLIES 3
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%``````

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]))

Helper IV

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

Announcements

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors