Extract names of top two products based on their values and criteria in other columns

smide

Board Regular
Joined
Dec 20, 2015
Messages
162
Office Version
  1. 2016
Platform
  1. Windows
Hello.

In column A (A2:A600) I have a list of cities and towns, in column B (B2:B600) product names and in column C (C2:C600) the price of each product.
The same city can appear more than once in column A and the same product can appear more than once in column B.

In first row from column D (cells D1, E1, F1...) I have all possible cities.
I need a top two product names from each city based on their price and result(s) should be in rows 2 and 3 below the name of corresponding city.

example.


A​
B​
C​
D​
E​
F​
1
City
Product
Price
MunchenBrisbaneOslo
2Munchencabbage
12​
orangeapplegrapes
3Brisbaneorange
30​
cabbageorangefish
4Oslofish
44​
5Brisbanetomato
19​
6Brisbaneapple
37​
7Munchenorange
35​
8Osloplum
28​
9Munchenpotato
6​
10Oslograpes
52​

explanation. Two most expensive products in Munchen are orange 35 (cell C7) and cabbage 12 (cell C2) so results are in cells D2 and D3 (below München in D1). The same for other cities.

* I'm using excel 2007.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi Smide,

Press Ctrl+Shift+Enter to enter array formula in cell D2
=INDEX($B$2:$C10,MATCH(MAX(IF($A$2:$A10=D$1, $C$2:$C10)),$C$2:$C10,0),1)

Press Ctrl+Shift+Enter to enter array formula in cell D3
=INDEX($B$2:$C10,MATCH(LARGE(IF($A$2:$A10=D$1,$C$2:$C10),2),$C$2:$C10,0),1)


Copy D2:D3 to E2:F3
 
Upvote 0
Solution
An alternative but seemingly long means to the solution is to employ Power Query. An advantage is that any data added to the original table is automatically evaluated and the out put is updated when the Refresh All is selected

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Product", type text}, {"Price", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"City"}, {{"Data", each _, type table [City=nullable text, Product=nullable text, Price=nullable text]}}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"Product", "Price"}, {"Data.Product", "Data.Price"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Data",{{"Data.Price", Order.Descending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
    #"Renamed Columns" = Table.RenameColumns(#"Added Index",{{"Index", "Index1"}}),
    #"Grouped Rows1" = Table.Group(#"Renamed Columns", {"City"}, {{"Data", each _, type table [City=nullable text, Data.Product=nullable text, Data.Price=nullable text, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows1", "Custom", each Table.AddIndexColumn([Data], "Index", 1, 1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Data.Product", "Index"}, {"Data.Product", "Index"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Data"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Index] <> 3)),
    #"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[City]), "City", "Data.Product"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns1"

Book4
ABC
1OsloBrisbaneMunchen
2grapesappleorange
3fishorangecabbage
Sheet2
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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