Formula to get Top 5 sales detail

Zubair

Active Member
Joined
Jul 4, 2009
Messages
332
Office Version
  1. 2016
Platform
  1. Windows
Dear Experts,

Please help me to get Top 5 sales with regions in D2:E6

1737566704106.png
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
If we insert a column in column D to show the rank, and list out 1-5, we can get this, which I think is what you want:
1737569193871.png


Here is the formula for cell F2:
Excel Formula:
=LARGE(B$2:B$11,D2)
and the formula for cell E2:
Excel Formula:
=INDEX(A$2:A$11,MATCH(F2,B$2:B$11,0))

Then, just copy E2:F2 all the way down to row 6.

Note that think could get a little hairy if the Sales values are not unique, i.e. you have two or more records with the exact same sales.
 
Upvote 0
Solution
An alternative is with Power Query
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Sales", Order.Descending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
    #"Filtered Rows" = Table.SelectRows(#"Added Index", each [Index] <= 5),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index"})
in
    #"Removed Columns"
 
Upvote 0
Note that think could get a little hairy if the Sales values are not unique,
Not just hairy - it wouldn't work.

@Zubair
What do you want to happen if there is more than one region with 5th highest sales as withy my sample below?
I have included two sets of results. The first lists all that are 5th or higher, including ties. The second stops after 5 even if there are others equal.

25 01 23.xlsm
ABCDEFGH
1RegionSalesRegionSalesRegionSales
2A230D987D987
3B456E888E888
4C770F888F888
5D987C770C770
6E888B456B456
7F888H456
8G234J456
9H456  
10I347  
11J456  
Top 5
Cell Formulas
RangeFormula
D2:D11D2=IF(E2="","",INDEX(A:A,AGGREGATE(15,6,ROW(A$2:A$11)/(B$2:B$11=E2),COUNTIF(E$2:E2,E2))))
E2:E11E2=IF(LARGE(B$2:B$11,ROWS(E$2:E2))<LARGE(B$2:B$11,5),"",LARGE(B$2:B$11,ROWS(E$2:E2)))
G2:G6G2=INDEX(A:A,AGGREGATE(15,6,ROW(A$2:A$11)/(B$2:B$11=E2),COUNTIF(E$2:E2,E2)))
H2:H6H2=LARGE(B$2:B$11,ROWS(H$2:H2))
 
Upvote 0
Many thanks all
Yes I was thinking the same if sales figures are duplicate, thanks again for the solution.
 
Upvote 0
A couple of other ways to achieve this. These could probably be simplified / improved.

Top 5
Excel Formula:
=VSTACK({"Rank","Region","Sales"},HSTACK(SEQUENCE(5,1,1,1),TAKE(SORTBY(A2:B11,B2:B11,-1),5,2)))

All records with a Top 5 score, ranked sequentially
Excel Formula:
=LET(filteredtable,FILTER(A2:B11,B2:B11>INDEX(SORT(UNIQUE(B2:B11),,-1),5,1)),sorted,SORTBY(filteredtable,CHOOSECOLS(filteredtable,2),-1),hstacked,HSTACK(SEQUENCE(COUNT(sorted),1,1),sorted),VSTACK({"Rank","Region","Sales"},hstacked))

All records with a Top 5 score, ranked by score (i.e. two records can have the same rank)
Excel Formula:
=LET(filteredtable,FILTER(A2:B11,B2:B11>INDEX(SORT(UNIQUE(B2:B11),,-1),5,1)),sorted,SORTBY(filteredtable,CHOOSECOLS(filteredtable,2),-1),ranks,BYROW(sorted,LAMBDA(row,MATCH(INDEX(row,1,2),SORT(UNIQUE(B2:B11),,-1),0))),VSTACK({"Rank","Region","Sales"},HSTACK(ranks,sorted)))
 
Upvote 0
As the OP's profile shows they are using 2016, none of those formulae will work for them. ;)
 
Upvote 0
Question for learning purposes
Using the data in post #4 (Peters response)

When this formula is applied =VSTACK({"Rank","Region","Sales"},HSTACK(SEQUENCE(5,1,1,1),TAKE(SORTBY(A2:B11,B2:B11,-1),5,2))) it gives the below


RankRegionSales
1D987
2E888
3F888
4C770
5B456


What my question is when I use the formula =LARGE(TAKE($B$2:$B$11,5),ROWS($I$15:I15)) I get the below, why does it ignore the second 888 what is the TAKE in first formula doing to include both 888's but my TAKE formula excludes one of the 888's

987
888
770
456
230

Many thanks as always
 
Upvote 0
You are taking the 1st 5 rows of the data & then sorting it, you need to sort the data & then take the 1st 5 rows.
 
Upvote 0

Forum statistics

Threads
1,226,074
Messages
6,188,729
Members
453,495
Latest member
Pippie4trnc

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