Custom Order for QUERY Pivot Headers in Google Sheets

Published on

By default, when you use the PIVOT clause in a Google Sheets QUERY, the pivoted column headers are sorted alphabetically (A–Z). But what if you want them in a custom order, like "Q4", "Q3", "Q2" , "Q1", or "High", "Medium", "Low"?

This tutorial shows you how to use the XMATCH function to sort QUERY pivot column headers in a custom order — including cases with more than one aggregation column.

This tutorial is part of the QUERY Pivot & Reporting in Google Sheets series and focuses on controlling pivot header order when the default sorting is not suitable.

Example 1: Custom Sort QUERY Pivot Headers with One Aggregation Column

Let’s start with a basic example using this sample data in A1:C10:

DateRegionSales
11/01/2024North1100
15/02/2024South850
06/03/2024East1200
18/01/2024East1000
20/02/2024North880
23/03/2024South1150
25/01/2024South920
11/02/2024East1075
15/03/2024North1300

Assume you want a pivot table showing monthly sales per region.

Here’s the standard QUERY:

=QUERY(A1:C, "select month(A), sum(C) where A is not null group by month(A) pivot B", 1)

This will sort the pivot headers (regions) alphabetically:

month(Date)EastNorthSouth
010001100920
11075880850
2120013001150

Note: The month() function in Google Sheets returns months numbered from 0 (January) to 11 (December). To get standard month numbers from 1 to 12, use month(A) + 1 instead of month(A) in your QUERY formula.

But suppose you want to manually reorder the pivot columns as "North", "South", "East". Here’s how:

=ArrayFormula(
   LET(
      qry, QUERY(A1:C, "select month(A), sum(C) where A is not null group by month(A) pivot B", 1), 
      order, HSTACK("*Date*", "North", "South", "East"), 
      CHOOSECOLS(qry, XMATCH(order, chooserows(qry, 1), 2))
   )
)

How It Works

  • qry: Stores the QUERY result.
  • order: Specifies your custom order using HSTACK. Include the group column (with wildcard *Date*) followed by your desired header order.
  • XMATCH: Finds the position of your desired column headers in the original result.
  • CHOOSECOLS: Selects columns in your specified order.
Example showing custom sort order of QUERY pivot headers with one aggregation column in Google Sheets

This is how you reorder QUERY pivot column headers manually using XMATCH.

Example 2: Custom Sort QUERY Pivot Headers with Multiple Aggregation Columns

Now, let’s say you want to analyze both Quantity and Amount in your pivot.

Here’s the new sample data:

DateRegionQuantityAmount
09/01/2024North111320
15/02/2024South91069
07/03/2024East101182
18/01/2024East91100
20/02/2024North7980
22/03/2024South7729
27/01/2024South81020
05/02/2024East91058
13/03/2024North121292

Here’s the regular pivot query:

=QUERY(A1:D, "select month(A), sum(C), sum(D) where A is not null group by month(A) pivot B", 1)

This returns pivot headers in default alphabetical order:

month(Date)East sum QuantityNorth sum QuantitySouth sum QuantityEast sum AmountNorth sum AmountSouth sum Amount
09118110013201020
197910589801069
21012711821292729

Now let’s set a custom sort order for QUERY pivot headers:

=ArrayFormula(
   LET(
      qry, QUERY(A1:D, "select month(A), sum(C), sum(D) where A is not null group by month(A) pivot B", 1), 
      order, HSTACK("*Date*", "North*Quantity", "North*Amount", "South*Quantity", "South*Amount", "East*Quantity", "East*Amount"), 
      CHOOSECOLS(qry, XMATCH(order, chooserows(qry, 1), 2))
   )
)

Key Points:

  • Use * as a wildcard for matching parts of the headers, e.g., "North*Quantity" matches "North sum Quantity".
  • The order HSTACK should reflect the desired order of regions, each repeated for every aggregation column.

This method works even with multiple aggregation columns, allowing full control over QUERY pivot header ordering.

Example showing custom sort order of QUERY pivot headers with multiple aggregation columns in Google Sheets

SAMPLE SHEET

FAQs

❓ What about multiple group columns?

If you have more than one group column, specify both (or more) labels in the order array with wildcards around them like "*Product*", "*Date*".

❓ Can I rename the custom-sorted pivot headers?

Yes! Wrap the final formula in another QUERY to use the label clause, or use SUBSTITUTE to remove or rename parts like "sum".

❓ Can I custom sort QUERY pivot headers from IMPORTRANGE data?

Absolutely. Just refer to columns using Col1, Col2, etc., instead of A, B, C. Your QUERY will still work with XMATCH and CHOOSECOLS.

We’ve previously covered related sorting use cases in Google Sheets:

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.