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 Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

Pivot Table Formatting, Output & Special Behavior in Google Sheets

Pivot Tables in Google Sheets are powerful—but they can get tricky once you move...

Pivot Table Calculations & Advanced Metrics in Google Sheets

When it comes to built-in tools for data analysis and visualization in Google Sheets,...

Google Sheets Pivot Table Tutorial: Basics, Setup, and Date Grouping

The easiest way to summarize, analyze, and report data in Google Sheets is by...

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.