filter a list on top-3 values

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
342
Office Version
  1. 2016
Platform
  1. Windows
I have a list of countries, and for each country the major cities (based on population). Now I want to filter this list, so that for each country only the top 3 major cities appear.

Any help is appreciated.
 

Attachments

  • cities.png
    cities.png
    21.4 KB · Views: 15

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Would you be open to a helper column? If so, how about the following:

Book1
ABCD
1CountryCityPopulationHelper
2country01city011,000,0006
3country01city021,100,0005
4country01city031,200,0004
5country01city041,300,0003
6country01city051,400,0002
7country01city061,500,0001
8country02city012,000,0006
9country02city022,100,0005
10country02city032,200,0004
11country02city042,300,0003
12country02city052,400,0002
13country02city062,500,0001
14country03city013,000,0006
15country03city023,100,0005
16country03city033,200,0004
17country03city043,300,0003
18country03city053,400,0002
19country03city063,500,0001
Sheet1
Cell Formulas
RangeFormula
D2:D19D2=COUNTIFS($A$2:$A$19,A2,$C$2:$C$19,">"&C2)+1


Filtered on the top 3 looks like:
Book1
ABCD
1CountryCityPopulationHelper
5country01city041,300,0003
6country01city051,400,0002
7country01city061,500,0001
11country02city042,300,0003
12country02city052,400,0002
13country02city062,500,0001
17country03city043,300,0003
18country03city053,400,0002
19country03city063,500,0001
20
Sheet1
Cell Formulas
RangeFormula
D5:D7,D11:D13,D17:D19D5=COUNTIFS($A$2:$A$19,A5,$C$2:$C$19,">"&C5)+1
 
Upvote 0
Solution
removed as wrong solution provided.
 
Last edited:
Upvote 0
Here is an alternative solution with Power query which is very fast especially if you have lots of records

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Sorted Rows" = Table.Sort(Source,{{"Country", Order.Ascending}, {"Population", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Country"}, {{"Count", each _, type table [Country=text, City=text, Population=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Top3", each Table.LastN([Count],3)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Top3"}),
    #"Expanded Top3" = Table.ExpandTableColumn(#"Removed Other Columns", "Top3", {"Country", "City", "Population"}, {"Country", "City", "Population"}),
    #"Sorted Rows1" = Table.Sort(#"Expanded Top3",{{"Country", Order.Ascending}})
in
    #"Sorted Rows1"
 
Upvote 0
Here is an alternative solution with Power query which is very fast especially if you have lots of records

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Sorted Rows" = Table.Sort(Source,{{"Country", Order.Ascending}, {"Population", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Country"}, {{"Count", each _, type table [Country=text, City=text, Population=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Top3", each Table.LastN([Count],3)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Top3"}),
    #"Expanded Top3" = Table.ExpandTableColumn(#"Removed Other Columns", "Top3", {"Country", "City", "Population"}, {"Country", "City", "Population"}),
    #"Sorted Rows1" = Table.Sort(#"Expanded Top3",{{"Country", Order.Ascending}})
in
    #"Sorted Rows1"
Thanls Alan. But I have no experience at all in Power Queries.
 
Upvote 0
This should help you get started.

Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

- Follow this link to learn how to install Power Query in Excel 2010 / 2013.

- Follow this link for an introduction to Power Query functionality.

- Follow this link for a video which demonstrates how to use Power Query code provided.
 
Upvote 0
You could use the filter function, after inserting the helper column:

Book1
ABCD
1CountryCityPopulationHelper
2country01city0110000006
3country01city0211000005
4country01city0312000004
5country01city0413000003
6country01city0514000002
7country01city0615000001
8country02city0120000006
9country02city0221000005
10country02city0322000004
11country02city0423000003
12country02city0524000002
13country02city0625000001
14country03city0130000006
15country03city0231000005
16country03city0332000004
17country03city0433000003
18country03city0534000002
19country03city0635000001
20
21country01city0413000003
22country01city0514000002
23country01city0615000001
24country02city0423000003
25country02city0524000002
26country02city0625000001
27country03city0433000003
28country03city0534000002
29country03city0635000001
Sheet1
Cell Formulas
RangeFormula
D2:D19D2=COUNTIFS($A$2:$A$19,A2,$C$2:$C$19,">"&C2)+1
A21:D29A21=FILTER($A$2:$D$19,D2:D19<=3)
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,225,760
Messages
6,186,870
Members
453,380
Latest member
ShaeJ73

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