HomeGoogle DocsSpreadsheetHow to Sort Pivot Table Rows by Second Column in Google Sheets

How to Sort Pivot Table Rows by Second Column in Google Sheets

Sorting rows in a Google Sheets pivot table usually defaults to the first column, but what if you need to sort by the second column instead?

Whether you’re organizing sales data by totals or ranking tasks by priority, sorting pivot table rows by the second column can make your data more meaningful and easier to analyze.

In this tutorial, I’ll show you step-by-step how to sort pivot table rows by the second column in Google Sheets.

This tutorial is included in our complete guide on How to Sort and Filter Pivot Tables in Google Sheets, where you’ll find step-by-step methods for sorting, filtering, and ranking Pivot Table data efficiently.

Introduction

Google Sheets doesn’t offer a built-in option in the pivot table editor to sort rows by any column other than the first. To work around this limitation, we’ll use a helper column.

Let’s say your pivot table (not the source data) includes this structure: Item, P.O. Date, and SUM of Order Qty.

  • The first two fields — Item and P.O. Date — are placed under Rows.
  • The last field — SUM of Order Qty — is placed under Values.

Now, suppose you want to sort the pivot table rows by P.O. Date. Since it’s the second row field, the default sorting behavior won’t produce the expected order.

Why?

Google Sheets lets you sort each row group by the field itself or by its corresponding Values field (e.g., SUM of Order Qty). However, sorting of the first row field always takes precedence and overrides the sorting of the second field.

So even if you try to sort by P.O. Date, the grouping and sort order of Item will dominate, making it hard to achieve the intended result.

To fix this, I’ll show you how to sort pivot table rows by the second column — like P.O. Date — using a helper column.

Sorting Pivot Table Rows by Another Column in Google Sheets

Let’s walk through the steps using a mock dataset:

ItemSupplierP.O. NumberP.O. DateUnitOrder Qty
Gravel 20 mmVendor 1150001/01/2022315
Gravel 40 mmVendor 2150101/01/2022180
Gravel 40 mmVendor 2150201/01/2022450
RoadbaseVendor 3150302/01/2022225
RoadbaseVendor 6150603/01/2022225
Gravel 5-10 mmVendor 4150403/01/202245
Gravel 10-20 mmVendor 5150503/01/202290

You can copy this data into cell range A1:F8 of your Sheet.

We want to group this purchase order data by Item → P.O. Date and show the sum of Order Qty.

Pivot Table Settings

  • Drag the Item and P.O. Date into the Rows section.
  • Drag Order Qty into the Values section.

This setup will create a pivot table, but the P.O. Date entries won’t be sorted as intended, because sorting defaults to the first column in the row grouping.

Default pivot table sorting where rows are ordered by the first column only

To sort pivot table rows by the second column (e.g., P.O. Date), we’ll use a helper column.

Using a Helper Column to Sort Pivot Table Rows by Second Column

We’ll use column G as a helper column (which is currently empty).

In cell G1, enter the following array formula:

=ArrayFormula(
  VSTACK(
    "Helper",
    IFNA(
      XMATCH(
        A2:A,
        CHOOSECOLS(
          SORT(
            SORTN(
              SORT(HSTACK(A2:A, D2:D), 2, TRUE),
              9^9, 2, 1, TRUE
            ),
            2, TRUE, 1, 1
          ),
          1
        )
      )
    )
  )
)
  • Replace A2:A with the first grouping column (placed first under Rows in the Pivot Table editor).
  • Replace D2:D with the column you want to sort by (e.g., P.O. Date, the second row grouping in the pivot table).
Helper column used to sort pivot table rows by the second column (e.g., P.O. Date) in Google Sheets

How the Formula Works (Brief Explanation)

This formula generates a helper column that assigns a sort order to the first row group based on the second column (e.g., P.O. Date):

  • HSTACK combines the first and second ‘Rows’ fields.
  • SORT and SORTN organize the values based on the second column and return unique rows.
  • CHOOSECOLS selects only the first column from the sorted output.
  • XMATCH matches each original value to the sorted list and returns the corresponding order.
  • VSTACK adds the header row Helper to make the array usable in the pivot table.

Update the Pivot Table

Now, open the Pivot Table editor and do the following:

  • Update the data range from A1:F8 to A1:G8.
  • Add the Helper column to the top of the Rows section.
Example showing how to sort pivot table rows by the second column in Google Sheets

Optionally, hide the Helper column in the pivot table to keep the display clean.

With this setup, the pivot table will now be sorted by the second column (e.g., P.O. Date) instead of the default first column.

Sorting Pivot Table Rows in Descending Order

If you’d like to sort the pivot table rows by the second column in descending order:

  • In the Pivot Table editor, select Descending as the sort order for the Helper field.
  • Also, set P.O. Date to sort in Descending order to reflect the reverse chronological order within each group.

Sample Sheet

Resources

Prashanth K V
Prashanth K V
Your Trusted Google Sheets and Excel Expert Prashanth K V 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

How to Use the SHEET and SHEETS Functions in Google Sheets

The SHEET and SHEETS functions let you retrieve information about worksheets in a Google...

How to Create a Self-Healing Table of Contents in Google Sheets

A table of contents makes navigating large Google Sheets workbooks much easier. However, a...

Sort a Tab Name List Dynamically by Workbook Order in Google Sheets

When your workbook contains many sheets (tabs), you may create a table of contents...

2 COMMENTS

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.