Extract Top N Items From a Data Range in Google Sheets

Published on

I have a sales report in Google Sheets where I want to extract only the top n items based on sales volume. How can I do that?

Let’s explore how to extract the top n items from a table or list in Google Sheets.

In the sample sales report below, columns A to F contain sales data for various landscaping materials.

Sample Data:

ItemUnitQtyRateSales AmountProject
3/8″ Full Crushed Lime StoneTon624.428.004995.36DXB
3/16″ Full Crushed Lime StoneTon70.128.00560.96DXB
Black Washed SandTon77.868.00622.88DXB
Sub BaseTon631.648.005053.12ABD
Black SandTon300.008.002400.00DXB
Cobble Stone 35-80 MMTon300.0020.006000.00DXB
Cobble Stone 40-60 MMTon149.9220.002998.40DXB
River PebblesTon7.00232.001624.00ABD
Road Base 50 mm DownTon165.807.001160.60DXB
Limestone 40-80 MMTon815.3611.008,968.96DXB

To extract the top 5 items based on sales volume in column C, you can use either the QUERY function or a combination of FILTER and SORTN, depending on your needs:

  • QUERY: Suitable for extracting the top n records with the option to add additional criteria.
  • FILTER + SORTN: Apply conditions before extracting the top n items plus duplicates, if any. In this case, items with the same sales quantity are considered as a single occurrence (counted as one entity).

Extract Top N Items Using QUERY Function in Google Sheets

In the sample data, we have the sales volume in column C. Assume you want to extract the top 5 records from this dataset, where n is 5.

You can use the following QUERY formula:

=QUERY(A1:F, "SELECT A, B, C, D, E, F ORDER BY C DESC LIMIT 5", 1)
Extract Top N Items Using the QUERY Function in Google Sheets

This formula selects columns A to F and sorts column C (Qty) in descending order, so the top items are moved to the top. It limits the output to 5 rows.

The number 1 in the last part indicates the number of header rows in your data range. If your data starts from A2:F, excluding the header in A1:F1, you would specify it as 0.

This formula has one advantage and one disadvantage. The advantage is that you can apply filtering before returning the top 5 items from the range.

For example, you can extract the top 5 items supplied to a specific project, here “DXB”

=QUERY(A1:F, "SELECT A, B, C, D, E, F WHERE F ='DXB' ORDER BY C DESC LIMIT 5", 1)
Top 5 Values Based on QUERY Filtering

Note: The QUERY function is case-sensitive. Please take care when applying criteria.

The disadvantage is that when there are duplicate values in the column being analyzed for the top n (in this case, Qty), it does not return the top n unique sales volumes.

Here’s where the FILTER and SORTN combo shines.

Extract Top N Items Using FILTER and SORTN Functions in Google Sheets

Do you want to extract the top n unique values and all duplicates of these values, if any? Look no further—the SORTN function is the answer.

The following formula will return the top 5 records from the sales data in A2:F (when using SORTN, exclude the header row):

=SORTN(A2:F, 5, 3, 3, FALSE)
Top N Values Plus Duplicates Using SORTN

There are 6 records when we expect 5. This is because the quantity 300, which is in the top 5, occurs twice. This means all occurrences of the same top n sales volume will be included.

The formula follows the syntax:

SORTN(range, [n], [display_ties_mode], [sort_column], [is_ascending], [sort_column2, …], [is_ascending2, …])

Where:

  • range: A2:F (the range to extract the top n items)
  • n: 5 (the number of items to extract)
  • display_ties_mode: 3 (mode to extract the top n items plus all duplicates)
  • sort_column: 3 (column index on which to apply the tie mode, here the Qty column)
  • is_ascending: FALSE (to sort the range in descending order based on sort_column)

If you want to apply a condition, use the FILTER function with the range argument.

For example, to extract the top 5 items supplied to the “DXB” project, use the formula below:

=SORTN(FILTER(A2:F, F2:F="DXB"), 5, 3, 3, FALSE)
Extract Top N Unique Values and Duplicates Based on Condition

In this, the FILTER function filters A2:F where F2:F="DXB".

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

More like this

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

8 COMMENTS

  1. Thanks so much!

    Is there a way to add a condition to it?

    So, for your example, if you had another column titled “product type,” and you wanted the top quantity sales for all sand products?

    • Hi, Abigail Ryan,

      Assume column F contains the “Product Type.”

      Here are the formulas based on it.

      Query:

      =Query(A1:F,"Select A,B,C,D,E where lower(F)='sand' Order by C Desc Limit 5")

      SORTN:

      =sortn(filter(A2:E,F2:F="Sand"),5,0,3,0)

    • Hi, Brad,

      We can do that. In the below example, cell E1 controls the Query Limit clause.

      =Query(A1:B,"Select A,sum(B) group by A order by sum(B) desc limit "&E1&" label sum(B)'Sales'")

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.