Filter the Top 3 Values in Each Group in a Pivot Table – Google Sheets

Published on

You can filter a Google Sheets Pivot Table using various conditions. Among them, you can use Filter by Condition > Custom Formula to filter the top 10, 5, 3, or any number of values in each group in a Pivot Table.

Unlike Excel, Google Sheets does not provide a built-in “Value Filters” option for filtering the Top 10 values. This can make it challenging for beginners to filter the top 3 values by group in a Pivot Table.

However, by following my data settings, you can use a custom formula within the Pivot Table to achieve this.

Custom Formula to Filter the Top N Items in Each Group in a Google Sheets Pivot Table

=XMATCH(
   Category & Item, 
   ArrayFormula(LET(
      qry, QUERY(QUERY(data, "SELECT Col2, Col1, SUM(Col3) WHERE Col2 IS NOT NULL GROUP BY Col2, Col1", 1), "OFFSET 1", 0), 
      rnk, COUNTIFS(CHOOSECOLS(qry, 1), CHOOSECOLS(qry, 1), CHOOSECOLS(qry, 3), ">"&CHOOSECOLS(qry, 3))+1, 
      FILTER(CHOOSECOLS(qry, 1)&CHOOSECOLS(qry, 2), rnk<=n)
   ))
)
  • data: The data range (columns: Item, Category, Value).
  • Category & Item: Headers combined for matching.
  • n: The number of values to retrieve (e.g., n=3 for the top 3).

If 3, this formula filters the Pivot Table by the top 3 values in each group. For top 10, specify 10 instead.

Tie-Breaking Mode in Filtering the Top N Values in a Pivot Table

The custom formula follows the Excel Pivot Table interpretation: Highest N Values + Duplicates of the Nth Highest.

Example Data with Tie Handling

ValuesIncluded in Top 3
42
42
16
16
11
10

Explanation:

When applying a Top 3 filter, Excel includes all ties at the Nth position. The values 42, 42 are clearly in the top 3. The next highest value 16 is ranked 3rd, but since there is a tie at 16, both occurrences are included instead of arbitrarily excluding one. This approach ensures fairness in ranking.

Since rank positions determine filtering, we adopt the same logic in our custom formula.

Example: Filtering the Top 3 Items by Group in a Pivot Table in Google Sheets

Sample Data:

Sample data setup to filter the top 3 values in each group within a Pivot Table in Google Sheets

The goal is to create a Pivot Table that summarizes data by Area and Product and then filters the top 3 products in each region.

Steps to Create a Pivot Table:

  1. Select A1:C.
  2. Click Insert > Pivot Table.
  3. Choose New Sheet in the popup and click Create.
  4. Drag and drop Area and Product under Rows.
  5. Drag and drop Qty under Values.
Grouping in Pivot Table with two fields under Rows and one field under Values

This will create a summarized Pivot Table. Since the data range contains empty rows, the Pivot Table may include an empty group, which can be ignored.

Pivot Table result before applying the Top N filter for groups

Applying the Custom Formula to Filter the Top 3 Items in Each Group

  1. Drag and drop Area under Filters.
  2. Click the filter dropdown and select Filter by Condition > Custom Formula Is.
  3. Copy-paste the following formula:
=XMATCH(
   Area & Product, 
   ArrayFormula(LET(
      qry, QUERY(QUERY(Sheet1!A1:C, "SELECT Col2, Col1, SUM(Col3) WHERE Col2 IS NOT NULL GROUP BY Col2, Col1", 1), "OFFSET 1", 0), 
      rnk, COUNTIFS(CHOOSECOLS(qry, 1), CHOOSECOLS(qry, 1), CHOOSECOLS(qry, 3), ">"&CHOOSECOLS(qry, 3))+1, 
      FILTER(CHOOSECOLS(qry, 1)&CHOOSECOLS(qry, 2), rnk<=3)
   ))
)
  1. Click OK.

This will filter the top 3 items in each group within the Pivot Table.

Example of filtering the top 3 values in each group in a Pivot Table

Breakdown of the Formula:

qry:

QUERY(Sheet1!A1:C, "SELECT Col2, Col1, SUM(Col3) WHERE Col2 IS NOT NULL GROUP BY Col2, Col1", 1)
  • Groups the data by Area and Product, summing the Qty.
QUERY(..., "OFFSET 1", 0)
  • Removes the header row from the first query result.

rnk:

COUNTIFS(CHOOSECOLS(qry, 1), CHOOSECOLS(qry, 1), CHOOSECOLS(qry, 3), ">"&CHOOSECOLS(qry, 3))+1
  • Returns the group-wise ranking of each item.

Final Filtering Expression:

FILTER(CHOOSECOLS(qry, 1)&CHOOSECOLS(qry, 2), rnk<=3)
  • Filters the Area & Product wherever the rank ≤ 3.

XMATCH:

XMATCH(Area&Product, ...)
  • Matches the filtered Area & Product and returns the relevant rows.

Conclusion

By using this custom formula, you can effectively Filter the Top 3 Values in Each Group in a Pivot Table in Google Sheets. This method replicates Excel’s behavior while handling ties efficiently.

Sample Sheet

Additional 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.

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Filter Top N per Category in Excel (Using FILTER & COUNTIFS)

You can use a combination of FILTER and COUNTIFS to filter the top N...

Rank per Group in Excel

You have two groups of 20 students each. How do you determine the rank...

More like this

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

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.