Returning top 3 list with duplicates using INDEX, MATCH, IF, SMALL & TEXTJOIN

Dlloyd15

New Member
Joined
Feb 20, 2020
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Firstly I'd just like to say I'm fairly new at using extensive functions in Excel, so if I've missed anything or am doing this the completely wrong way then apologies, still trying to get my head round everything.

So my problem:
I have a table with first name, last name, office, and (car) CO2 emissions and am trying to get the name of the employees with the 3 best (lowest) CO2 emissions per office, in this case = GS. But am trying to keep everything tidy so have one big formula in a cell doing all the functions simultaneously. So far I have been able to do this but have hit a block because the 3rd best (lowest) employee is joint 3rd with 108 g/km and as a result the cell is only showing one name rather than the two.

Does anyone know what i need to do to fix this, or if it is possible? Formula and tables below. Hopefully this all makes sense but happy to answer any questions if not/it helps.

Thanks in advance!

(P.S. all names have been changed)

(Array) Formula:

{=(TEXTJOIN(",",TRUE,INDEX($B$11:$E$77,MATCH(1,($E$11:$E$77=SMALL(IF($D$11:$D$77="GS",$E$11:$E$77),3))*($D$11:$D$77="GS"),0),2)))}

This returns 'Ellis' but I need it to return 'Ellis,Parker' as both are the 3rd best CO2 emissions in the 'GS' office.

Table:
First NameLast NameOfficeCo2 Emission
OliverCaseyIN157
AidaCrawfordGS214
WilliamEllisGS108
NatalieArmstrongMD119
RosieBakerMD89
StellaBrooksIN105
OscarParkerGS108
NatalieMorrisIN115
AlenGibsonGS105
BriannaEvansMD111
AmberJonesNT133
MirandaArmstrongNT104
BrookeMasonGS133
MyraHillGS165
RyanHillMD117
RafaelGrantMD117
LuciaHillIN256
JuliaMasonGS128
EdwardBaileyWW169
AidenMurrayWW119
LilyReedLE112
KellanGibsonIN0
DarylSullivanIN138
DexterScottGS99
MartinRobertsMD127
MichelleEllisIN209
JustinWrightIN91
BradAllenGS120
RichardEllisIN134
ConnieHarperIN106
ValeriaBrownIN106
BrionyPerryGS124
CarlMorrisIN120
CarlosAdamsIN153
AprilClarkGS113
BrookeAlexanderIN129
BriannaColeIN146
PenelopeChapmanIN294
KevinDavisWW103
PaigeMitchellGS112
 
.. with fewer helpers, see column H (with helpers in col G)

If you really wanted to do it without helpers (wouldn't be my choice), see column I

A_MrExcel.xlsm
BCDEFGHI
8GSGS
910Crawford,Parker,Mason,HillCrawford,Parker,Mason,Hill
10First NameLast NameOfficeCo2 Emission299ScottScott
11OliverCaseyIN7573105Gibson,MasonGibson,Mason
12AidaCrawfordGS0
13WilliamEllisGS108
14NatalieArmstrongMD119
15RosieBakerMD89
16StellaBrooksIN105
17OscarParkerGS0
18NatalieMorrisIN115
19AlenGibsonGS105
20BriannaEvansMD111
21AmberJonesNT133
22MirandaArmstrongNT104
23BrookeMasonGS0
24MyraHillGS0
25RyanHillMD117
26RafaelGrantMD117
27LuciaHillIN256
28JuliaMasonGS105
29EdwardBaileyWW169
30AidenMurrayWW119
31LilyReedLE112
32KellanGibsonIN0
33DarylSullivanIN138
34DexterScottGS99
35MartinRobertsMD127
3 lowest
Cell Formulas
RangeFormula
G9:G11G9=AGGREGATE(15,6,E$11:E$35/(D$11:D$35=H$8),COUNTIFS(D$11:D$35,H$8,E$11:E$35,"<="&G8)+1)
H9:H11H9=TEXTJOIN(",",1,IF((D$11:D$35=$H$8)*(E$11:E$35=G9),C$11:C$35,""))
I9:I11I9=TEXTJOIN(",",1,IF((D$11:D$35=I$8)*(E$11:E$35=IFERROR(AGGREGATE(15,6,E$11:E$35/((D$11:D$35=I$8)*(E$11:E$35>INDEX(E$11:E$35,MATCH(LEFT(I8&",",FIND(",",I8&","))&I$8,INDEX(C$11:C$35&","&D$11:D$35,0),0)))),1),MINIFS(E$11:E$35,D$11:D$35,I$8))),C$11:C$35,""))
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Thanks both for your help! Really have been a lot of help!

Just out of curiosity Peter, why wouldn't you do it without helpers?

I now understand it would be (a lot) easier to use helpers but the reason I wanted to keep the formula to one cell is so that visually it's as simple as possible.
The aim of this is to distribute it to all the offices so they can see the top and bottom performers in vehicle CO2 emissions as well as business mileage, commute mileage and total mileage. To ensure it was simple I just wanted people to be able to open the document up and see the full table (A10:E35 in this example) and then next to it the top and bottom 3 for each office for each criteria without seeing lots of formula and helper cells that could confuse people, especially those that aren't used to working with Excel.
 
Upvote 0
why wouldn't you do it without helpers?
It's just the complexity of the formula required - difficult to modify if required for any reason.

the reason I wanted to keep the formula to one cell is so that visually it's as simple as possible.

.. just wanted people to be able to open the document up and see the full table (A10:E35 in this example) and then next to it the top and bottom 3 for each office ..
Just hide the helper column(s)?
 
Upvote 0
To be honest I didn't think it was going to be as complicated as this when I started out so wanted to keep it all in one formula. But having seen the complexity of it, its probably best to use helpers. Thank you Peter.

One last question (hopefully!). In order to get the bottom 3 employees would I just need to change the formula as below? (15 to 16 and < to >)

Original:
=AGGREGATE(15,6,E$11:E$35/(D$11:D$35=H$8),COUNTIFS(D$11:D$35,H$8,E$11:E$35,"<="&G8)+1)

Changed:
=AGGREGATE(16,6,E$11:E$35/(D$11:D$35=H$8),COUNTIFS(D$11:D$35,H$8,E$11:E$35,">="&G8)+1)
 
Upvote 0
You need to change the 15 to 14, not 16 & swap the < to >
 
Upvote 0
Peter,
The big formula in I9
=TEXTJOIN(",",1,IF((D$11:D$35=I$8)*(E$11:E$35=IFERROR(AGGREGATE(15,6,E$11:E$35/((D$11:D$35=I$8)*(E$11:E$35>INDEX(E$11:E$35,MATCH(LEFT(I8&",",FIND(",",I8&","))&I$8,INDEX(C$11:C$35&","&D$11:D$35,0),0)))),1),MINIFS(E$11:E$35,D$11:D$35,I$8))),C$11:C$35,""))

If you're able to use the filter function, what would the formula be? Would the formula be a lot shorter?
 
Upvote 0
If you're able to use the filter function, what would the formula be?
If you have the FILTER function then you would also have the UNIQUE function, so the formula would be considerably shorter ...

Dlloyd15 2020-02-22 1.xlsm
BCDEFG
8GS
91Crawford,Parker,Mason,Hill
10First NameLast NameOfficeCo2 Emission2Scott
11OliverCaseyIN7573Gibson,Mason
12AidaCrawfordGS0
13WilliamEllisGS108
14NatalieArmstrongMD119
15RosieBakerMD89
16StellaBrooksIN105
17OscarParkerGS0
18NatalieMorrisIN115
19AlenGibsonGS105
20BriannaEvansMD111
21AmberJonesNT133
22MirandaArmstrongNT104
23BrookeMasonGS0
24MyraHillGS0
25RyanHillMD117
26RafaelGrantMD117
27LuciaHillIN256
28JuliaMasonGS105
29EdwardBaileyWW169
30AidenMurrayWW119
31LilyReedLE112
32KellanGibsonIN0
33DarylSullivanIN138
34DexterScottGS99
35MartinRobertsMD127
3 lowest (4)
Cell Formulas
RangeFormula
G9:G11G9=TEXTJOIN(",",1,FILTER(C$11:C$35,(D$11:D$35=G$8)*(E$11:E$35=SMALL(UNIQUE(FILTER(E$11:E$35,D$11:D$35=G$8)),ROWS(G$9:G9)))))
 
Upvote 0
Thanks Peter. The formula is much smaller. The filter and unique functions make it much smaller. If you could use column G (helper) and column H and use the filter and unique function, what would the formula be?
How would you get the top 5 for the whole list irrespective of office?
 
Upvote 0
If you could use column G (helper) and column H and use the filter and unique function, what would the formula be?

Dlloyd15 2020-02-22 1.xlsm
BCDEFGH
8GS
910Crawford,Parker,Mason,Hill
10First NameLast NameOfficeCo2 Emission299Scott
11OliverCaseyIN7573105Gibson,Mason
12AidaCrawfordGS0
13WilliamEllisGS108
14NatalieArmstrongMD119
15RosieBakerMD89
16StellaBrooksIN105
17OscarParkerGS0
18NatalieMorrisIN115
19AlenGibsonGS105
20BriannaEvansMD111
21AmberJonesNT133
22MirandaArmstrongNT104
23BrookeMasonGS0
24MyraHillGS0
25RyanHillMD117
26RafaelGrantMD117
27LuciaHillIN256
28JuliaMasonGS105
29EdwardBaileyWW169
30AidenMurrayWW119
31LilyReedLE112
32KellanGibsonIN0
33DarylSullivanIN138
34DexterScottGS99
35MartinRobertsMD127
3 lowest (5)
Cell Formulas
RangeFormula
G9:G11G9=SMALL(UNIQUE(FILTER(E$11:E$35,D$11:D$35=H$8)),F9)
H9:H11H9=TEXTJOIN(",",1,FILTER(C$11:C$35,(D$11:D$35=H$8)*(E$11:E$35=G9)))


How would you get the top 5 for the whole list irrespective of office?
1. For the sample data here, can you confirm your expected results?

2. Do you want the results with or without any helpers?
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top