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.
I have two tables with relations.
Users: UserId, UserName
Answers: UserId, AnswerId
Users:
Id Name
1 User1
2 User2
3 User 3
Answers:
Id UserId
1 1
2 2
3 1
4 1
5 2
etc.
I want to find top 3 users by a number of answers, sum the total number of answers of those top 3 users and display in a card % of all answers for those top 3 users have. I don't have issues to find the sum of all answers, but how to find the sum of answers by top 3 users?
Is that possible to that without additional tables? Using measures and \ or calculated columns only?
Solved! Go to Solution.
Hi @sKaiNet
Try this:
PercentageTop3 = DIVIDE ( CALCULATE ( COUNT ( Answers[AnswerId] ); TOPN ( 3; VALUES ( Answers[UserId] ); CALCULATE ( COUNT ( Answers[AnswerId] ) ); DESC ) ); COUNT ( Answers[AnswerId] ) )
Probably better ways to do this, but something like this should work:
Measure 6 = VAR __topN = 1 //change this to how many you want VAR __table = ADDCOLUMNS(ALL(Users),"__count",COUNTX(RELATEDTABLE(Answers),[Id])) VAR __table1 = ADDCOLUMNS(__table,"__rank",RANKX(__table,[__count])) RETURN CONCATENATEX(SELECTCOLUMNS(FILTER(__table1,[__rank]<=__topN),"__name",[Name]),[__name],",")
Hi @sKaiNet
Try this:
PercentageTop3 = DIVIDE ( CALCULATE ( COUNT ( Answers[AnswerId] ); TOPN ( 3; VALUES ( Answers[UserId] ); CALCULATE ( COUNT ( Answers[AnswerId] ) ); DESC ) ); COUNT ( Answers[AnswerId] ) )
@AlB wrote:Try this:
PercentageTop3 = DIVIDE ( CALCULATE ( COUNT ( Answers[AnswerId] ); TOPN ( 3; VALUES ( Answers[UserId] ); CALCULATE ( COUNT ( Answers[AnswerId] ) ); DESC ) ); COUNT ( Answers[AnswerId] ) )
Thank you, that works!
Could you please explain how TOPN works in your sample? From my POV TOPN gets top N rows by comparing values in a single column. However, you use CALCULATE as the third parameter for TOPN, which confuses me.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
111 | |
100 | |
80 | |
64 | |
58 |
User | Count |
---|---|
146 | |
110 | |
93 | |
84 | |
67 |