Book1 | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Country | City | Population | Helper | ||
2 | country01 | city01 | 1,000,000 | 6 | ||
3 | country01 | city02 | 1,100,000 | 5 | ||
4 | country01 | city03 | 1,200,000 | 4 | ||
5 | country01 | city04 | 1,300,000 | 3 | ||
6 | country01 | city05 | 1,400,000 | 2 | ||
7 | country01 | city06 | 1,500,000 | 1 | ||
8 | country02 | city01 | 2,000,000 | 6 | ||
9 | country02 | city02 | 2,100,000 | 5 | ||
10 | country02 | city03 | 2,200,000 | 4 | ||
11 | country02 | city04 | 2,300,000 | 3 | ||
12 | country02 | city05 | 2,400,000 | 2 | ||
13 | country02 | city06 | 2,500,000 | 1 | ||
14 | country03 | city01 | 3,000,000 | 6 | ||
15 | country03 | city02 | 3,100,000 | 5 | ||
16 | country03 | city03 | 3,200,000 | 4 | ||
17 | country03 | city04 | 3,300,000 | 3 | ||
18 | country03 | city05 | 3,400,000 | 2 | ||
19 | country03 | city06 | 3,500,000 | 1 | ||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
D2:D19 | D2 | =COUNTIFS($A$2:$A$19,A2,$C$2:$C$19,">"&C2)+1 |
Book1 | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Country | City | Population | Helper | ||
5 | country01 | city04 | 1,300,000 | 3 | ||
6 | country01 | city05 | 1,400,000 | 2 | ||
7 | country01 | city06 | 1,500,000 | 1 | ||
11 | country02 | city04 | 2,300,000 | 3 | ||
12 | country02 | city05 | 2,400,000 | 2 | ||
13 | country02 | city06 | 2,500,000 | 1 | ||
17 | country03 | city04 | 3,300,000 | 3 | ||
18 | country03 | city05 | 3,400,000 | 2 | ||
19 | country03 | city06 | 3,500,000 | 1 | ||
20 | ||||||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
D5:D7,D11:D13,D17:D19 | D5 | =COUNTIFS($A$2:$A$19,A5,$C$2:$C$19,">"&C5)+1 |
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.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"
Book1 | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Country | City | Population | Helper | ||
2 | country01 | city01 | 1000000 | 6 | ||
3 | country01 | city02 | 1100000 | 5 | ||
4 | country01 | city03 | 1200000 | 4 | ||
5 | country01 | city04 | 1300000 | 3 | ||
6 | country01 | city05 | 1400000 | 2 | ||
7 | country01 | city06 | 1500000 | 1 | ||
8 | country02 | city01 | 2000000 | 6 | ||
9 | country02 | city02 | 2100000 | 5 | ||
10 | country02 | city03 | 2200000 | 4 | ||
11 | country02 | city04 | 2300000 | 3 | ||
12 | country02 | city05 | 2400000 | 2 | ||
13 | country02 | city06 | 2500000 | 1 | ||
14 | country03 | city01 | 3000000 | 6 | ||
15 | country03 | city02 | 3100000 | 5 | ||
16 | country03 | city03 | 3200000 | 4 | ||
17 | country03 | city04 | 3300000 | 3 | ||
18 | country03 | city05 | 3400000 | 2 | ||
19 | country03 | city06 | 3500000 | 1 | ||
20 | ||||||
21 | country01 | city04 | 1300000 | 3 | ||
22 | country01 | city05 | 1400000 | 2 | ||
23 | country01 | city06 | 1500000 | 1 | ||
24 | country02 | city04 | 2300000 | 3 | ||
25 | country02 | city05 | 2400000 | 2 | ||
26 | country02 | city06 | 2500000 | 1 | ||
27 | country03 | city04 | 3300000 | 3 | ||
28 | country03 | city05 | 3400000 | 2 | ||
29 | country03 | city06 | 3500000 | 1 | ||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
D2:D19 | D2 | =COUNTIFS($A$2:$A$19,A2,$C$2:$C$19,">"&C2)+1 |
A21:D29 | A21 | =FILTER($A$2:$D$19,D2:D19<=3) |
Dynamic array formulas. |