Hello Gurus,
I've a requirement to show top N rows in a table, but that N is a dynamic which is derived by ranking on 2 different measures. So eventually, we show some rows by ranking on Total and some rows by ranking on Measure 1. (Total = Measure 1 + Measure 2). For some reason I couldn't attach Excel, so attaching the data tables here.
I am able to implement the logic for Case 1 by using multiple variables, but the problem is that I need to show rows according to Case 1 or Case 2 in the same table.
Case 1: I need to show Top 10 (having Total >= 25,000) and if I have 10 such Names, then I can show upto 3 more Names which have M1 >= 10,000. I can show no more than 13 Names in this case.
Case 1 | |||
Amt in K (thousands) | |||
Name | Total (M1+M2) | M1 | M2 |
A01 | 210,000 | 0 | 210,000 |
A02 | 186,000 | 0 | 186,000 |
A03 | 97,000 | 36,000 | 61,000 |
A04 | 85,000 | 0 | 85,000 |
A05 | 76,400 | 1,400 | 75,000 |
A06 | 62,840 | 840 | 62,000 |
A07 | 59,000 | 32,000 | 27,000 |
A08 | 53,000 | 0 | 53,000 |
A09 | 46,000 | 66,000 | (20,000) |
A10 | 44,000 | 0 | 44,000 |
A11 | 43,000 | 0 | 43,000 |
A12 | 33,000 | 43,000 | (10,000) |
A13 | 25,000 | 15,000 | 10,000 |
A14 | 20,000 | 20,000 | 0 |
A15 | 6,000 | 5,000 | 1,000 |
A16 | 7,000 | 6,000 | 1,000 |
A17 | 6,400 | 2,400 | 4,000 |
A18 | 2,000 | 1,000 | 1,000 |
A19 | 200 | 200 | 0 |
A20 | 0 | 0 | 0 |
A21 | 0 | 0 | 0 |
A22 | 0 | 0 | 0 |
A23 | 0 | 0 | 0 |
A24 | 0 | 0 | 0 |
A25 | 0 | 0 | 0 |
A26 | 0 | 0 | 0 |
A27 | 0 | 0 | 0 |
A28 | 0 | 0 | 0 |
A29 | 0 | 0 | 0 |
A30 | 0 | 0 | 0 |
Logic needed for Case 1: I need to show Top 10 (having Total >= 25,000) and if I have 10 such Names, then I can show upto 3 more Names which have M1 >= 10,000. I can show no more than 13 Names in this case. | |||
Top 10 based on Total | |||
Top 3 based on M1 (excludes top 10 based on Total) | |||
Note: As I mentioned in previous bullet, these are not included in top 3 based on M1, because these rows are already included in top 10 based on Total. |
Result from Case 1 | |||
Name | Total (M1+M2) | M1 | M2 |
A01 | 210,000 | 0 | 210,000 |
A02 | 186,000 | 0 | 186,000 |
A03 | 97,000 | 36,000 | 61,000 |
A04 | 85,000 | 0 | 85,000 |
A05 | 76,400 | 1,400 | 75,000 |
A06 | 62,840 | 840 | 62,000 |
A07 | 59,000 | 32,000 | 27,000 |
A08 | 53,000 | 0 | 53,000 |
A09 | 46,000 | 66,000 | (20,000) |
A10 | 44,000 | 0 | 44,000 |
A12 | 33,000 | 43,000 | (10,000) |
A13 | 25,000 | 15,000 | 10,000 |
A14 | 20,000 | 20,000 | 0 |
Case 2: Using the first part of logic in Case 1, if we see that out of Top 10 for Total, we only have 5 Names (with Total more than 25,000), then apart from them, I want to show upto 5 more Names which have M1 >= 5,000. I can show no more than 10 Names in this case.
That means: If only 7 Names have Total >= 25,000, then I can show 3 Names that have M1 >= 5000 or
If only 6 Names have Total >= 25,000, then I can show 4 Names that have M1 >= 5000... and so on.
Case 2 | ||||
Amt in K (thousands) | ||||
Name | Total (M1+M2) | M1 | M2 | |
A01 | 210,000 | 0 | 210,000 | |
A02 | 186,000 | 0 | 186,000 | |
A03 | 97,000 | 36,000 | 61,000 | |
A04 | 85,000 | 0 | 85,000 | |
A05 | 82,000 | 10,000 | 72,000 | |
A06 | 23,840 | 840 | 23,000 | |
A07 | 23,000 | 0 | 23,000 | |
A08 | 2,000 | 12,000 | (10,000) | |
A09 | 10,000 | 8,000 | 2,000 | |
A10 | 6,000 | 6,000 | 0 | |
A11 | 6,000 | 5,000 | 1,000 | |
A12 | 7,000 | 6,000 | 1,000 | |
A13 | 6,400 | 2,400 | 4,000 | |
A14 | 2,000 | 1,000 | 1,000 | |
A15 | 200 | 200 | 0 | |
A16 | 0 | 0 | 0 | |
A17 | 0 | 0 | 0 | |
A18 | 0 | 0 | 0 | |
A19 | 0 | 0 | 0 | |
A20 | 0 | 0 | 0 | |
A21 | 0 | 0 | 0 | |
A22 | 0 | 0 | 0 | |
A23 | 0 | 0 | 0 | |
A24 | 0 | 0 | 0 | |
A25 | 0 | 0 | 0 | |
A26 | 0 | 0 | 0 | |
A27 | 0 | 0 | 0 | |
A28 | 0 | 0 | 0 | |
A29 | 0 | 0 | 0 | |
A30 | 0 | 0 | 0 | |
Logic for Case 2: Using the first part of logic in Case 1, if we see that out of Top 10 for Total, we only have 5 Names (with Total more than 25,000), then apart from them, I want to show upto 5 more Names which have M1 >= 5,000. I can show no more than 10 Names in this case. | ||||
Top N (5 in this case) based on Total | ||||
Top N (5 in this case) based on M1 (excludes top 10 based on Total) | ||||
Note: As I mentioned in previous bullet, these are not included in top N based on M1, because these rows are already included in top N based on Total. |
Result from Case 2 | |||
Name | Total (M1+M2) | M1 | M2 |
A01 | 210,000 | 0 | 210,000 |
A02 | 186,000 | 0 | 186,000 |
A03 | 97,000 | 36,000 | 61,000 |
A04 | 85,000 | 0 | 85,000 |
A05 | 76,400 | 1,400 | 75,000 |
A08 | 2,000 | 12,000 | (10,000) |
A09 | 10,000 | 8,000 | 2,000 |
A10 | 6,000 | 6,000 | 0 |
A11 | 6,000 | 5,000 | 1,000 |
A12 | 7,000 | 6,000 | 1,000 |
I am able to implement the logic for Case 1 by using multiple variables, but the problem is that I need to show rows according to Case 1 or Case 2 in the same table.
Existing Objects: [Name], [Total], [M1] and [M2]
Variables created:
- v_Total_RankingTop = Rank([Total];Top) (This is to rank based on Total, and why I'm doing Top here? I have to build this whole thing for Ranking from Bottom :-))
- v_Total_Top10 = If([v_Total_RankingTop] <= 10) Then 1 Else 0 (This is a flag to show 1 if the Name is in Top 10 based on Total, 0 otherwise.)
- v_Total_25KPlus = If([Total] >= 25000) Then 1 Else 0 (This is a flag to show 1 if the Total >= 25K, 0 otherwise)
- v_Total_Top10with25KPlus = If([v_Total_Top10]=1 And [v_Total_25KPlus]=1) Then 1 Else 0 (This is a flag to show 1, if the Total >= 25000 and if the row is in Top 10 based on Total)
- v_M1_ToRank = If([v_Total_Top10]=1) Then 0 Else [M1] (I'll be using for ranking and not M1 directly, because I don't want to rank those M1 values which are already in top 10 based on Total)
- v_M1_RankingTop10Kplus = If([v_Total_Top10]=0 And [v_M1_ToRank]>=10000) Then Rank([v_M1_ToRank];Top) Else 0 (This is to rank based on v_M1_RankingTop, and why I'm doing Top here? I have to build this whole thing for Ranking from Bottom :-))
- v_M1_ToRank_Top3with10KPlus = If([v_M1_RankingTop10KPlus] <=3) Then 1 Else 0 (This is a flag to show Top 3 based on v_M1_TopRank)
Now, using the above variables, I can accomplish what I need in Case 1 by filtering on the table as [v_Total_Top10with25KPlus] = 1 or [v_M1_ToRank_Top3with10KPlus] = 1 and I'll get 13 rows (10 from ranking based on Total and 3 or less from ranking based on v_M1_ToRank). But what if not all the Top 10 based on Total has Total >= 25,000? I need to go to Case 2, and as you know in N in this case is X+Y (e.g. 9+1 or 8+2 or 7+3 or 6+4 so on till 1+9) where X is the Top N values based on Total having Total > 25,000 and Y is Top 10-X values based on v_M1_ToRank (>= 5000).
To get started with finding out what that X, Y can be, I created a formula that gives us X and we can derive Y a 10-X. Great, what do I do after that?
- v_M1_RankingTopwith5KPlus = If([v_Total_Top10]=0 And [v_M1_ToRank]>=5000) Then Rank([v_M1_ToRank];Top) Else 0 (This is to rank based on v_M1_RankingTop, and why I'm doing Top here? I have to build this whole thing for Ranking from Bottom :-))
- v_X_TopNwithTotal25KPlus =Max([v_Total_RankingTop] Where ([v_Total_25KPlus]=1)) In Block (This gives us the Max possible value of [v_Total_RankingTop] in a block, so we can at least know how many rows do you we need to show based on Total and v_M1_)
- v_X,Y = If(10-[v_X_TopNwithTotal25KPlus]<=0) Then "10,3" ElseIf(10-[v_X_TopNwithTotal25KPlus]=1) Then "9,1" ElseIf(10-[v_X_TopNwithTotal25KPlus]=2) Then "8,2" ElseIf(10-[v_X_TopNwithTotal25KPlus]=3) Then "7,3" ElseIf(10-[v_X_TopNwithTotal25KPlus]=4) Then "6,4" ElseIf(10-[v_X_TopNwithTotal25KPlus]=5) Then "5,5" ElseIf(10-[v_X_TopNwithTotal25KPlus]=6) Then "4,6" ElseIf(10-[v_X_TopNwithTotal25KPlus]=7) Then "3,7" ElseIf(10-[v_X_TopNwithTotal25KPlus]=8) Then "2,8" ElseIf(10-[v_X_TopNwithTotal25KPlus]=9) Then "1,9"
So, we now at least know, how many values are we showing by ranking based on Total and v_M1_RankingTopwith5KPlus. I need a brake now as my head is spinning and also your help with how to proceed.
Hope you'll got the question, any help is appreciated.
Thanks,
Mahboob Mohammed