Forum Discussion

Kostas's avatar
Kostas
Helper IV
4 years ago
Solved

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

  • 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

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

    • Kostas's avatar
      Kostas
      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

  • 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%