Sort and display the "Top X"
Posted by Gerhard Seljehammer on December 29, 2000 6:31 AM
I have a table with two columns. Name and value. Very simple: (but not for me), I just want to sort it first by name and then by value (that is no problem) and then only display the X greatest values for each name.
Anyone has an idea?
Posted by Aladin Akyurek on December 29, 2000 7:57 AM
I see some mismatch of meaning between your "Top X" and "only display X greatest values for each name."
If each name is associated with a number (e.g., a score on something) and you want to retrieve the names associated with, say 3, highest numbers, there are 2 issues to consider:
A) Are the numbers unique? If so, the solution is relatively easy to produce.
B) A is false. That is, a given number (score) is associated with 2 or more names. Your question is then identical to questions like "Give the names of the employees who earn the smallest salary." This type of questions should be answered preferably with database functions.
Posted by Dave on January 01, 2001 3:11 AM
You could apply Autofilters to both columns then filter by a particular name and then by "Top 10" in the Value column.
OzGrid Business Applications
Posted by Aladin Akyurek on January 01, 2001 10:41 PM
In case B is true, a formula-based solution is also possible.
Posted by Gerhard on January 03, 2001 1:22 AM
Case B is true. And to rephrase my question; I want the X greatest values for each name listed at the same time. Autofilter (dave) is not good enough cause it gives me only one name at the time.
(Thank you for trying to help me, boys)
Posted by Aladin Akyurek on January 05, 2001 12:08 AM
Here is a solution to your problem, which has become possible after an additional clarification thru e-mail.
But, first things first.
Here is your example with the expected results (as you provided thru e-mail).
The above data (including the labels) occupy the range A1 thru B29.
You want highest N (or X) values by name. For the example you've set N to 3.
Here is a machinery to compute this outcome.
Step 1. Put the value of N in some cell and name it MaxTop.
Step 2. Sort the names first, say in column D. [ This sorting is formula-based (which I gloss over here), but it can also be done via Data,Sort. ]
Step 3. Create list of names from D in E, where duplicates are removed. [ The removal of duplicates is formula-based, but I will not eloborate here on the machinary that is needed. ]
After this step we have:
Step 4. Array-enter (that is, hit control+shift+enter to enter) the following formula [ the jewel, I guess ]
Copy this down and across as far as needed.
Here is what we get, populating the range E2 thru H5:
karl 3 3 3
nils 8 7 6
Ole 9 7 6
petter 9 8 8
PS. The workbook containing all of the machinery including the steps that I didn't include here is underway to you via e-mail.