Fix Auto-Sorting Issue in QUERY GROUP BY in Google Sheets

When using the GROUP BY clause in the QUERY function in Google Sheets, the result is automatically sorted by the group column. While this behavior works well in most cases, it can be limiting if you want to retain the original data order.

If you’re looking to fix the auto-sorting issue in QUERY GROUP BY in Google Sheets, there’s no built-in setting to turn it off. However, you can use a clever formula workaround with LET, XMATCH, and SORT to restore the original row order.

Let’s dive in.

The Problem: QUERY GROUP BY Auto-Sorts the Output

Here’s a sample dataset in range B1:C:

ItemQuantity
Peer100
Orange50
Apple75
Apple75
Peer100
Peer100
Banana50

Now try this standard QUERY formula:

=QUERY(B1:C, "select B, sum(C) where B is not null group by B", 1)

Result:

Itemsum Quantity
Apple150
Banana50
Orange50
Peer300

Notice how the fruits are automatically sorted alphabetically? That’s the default behavior of GROUP BY.

Expected Result: Maintain Original Group Order

What if you want the output in the order that fruits first appear in the source data?

Itemsum Quantity
Peer300
Orange50
Apple150
Banana50

Here’s how you can achieve that.

Fix Auto-Sorting Issue in QUERY GROUP BY: The Formula

Use the following formula to preserve the original order of the grouped values:

=LET(
   qry, QUERY(B1:C, "select B, sum(C) where B is not null group by B", 1),
   helper, ARRAYFORMULA(IFNA(XMATCH(CHOOSECOLS(qry, 1), B1:B), 1)),
   SORT(qry, helper, 1)
)
Example showing how to fix auto-sorting issue in QUERY GROUP BY using LET, XMATCH, and SORT in Google Sheets

How the Formula Works

Let’s break it down:

  • LET(): Assigns names to parts of the formula to make it cleaner and more efficient.
  • qry:
    QUERY(B1:C, "select B, sum(C) where B is not null group by B", 1)
    This is your standard query. It groups by the “Items” column and sums the quantities.
  • helper:
    ARRAYFORMULA(IFNA(XMATCH(CHOOSECOLS(qry, 1), B1:B), 1))
    • CHOOSECOLS(qry, 1)
      pulls just the group names from the query result.
    • XMATCH(..., B1:B)
      finds the first appearance of each group name in the original data.
    • IFNA(..., 1)
      ensures the header row returns 1 (instead of an error).
  • SORT(qry, helper, 1):
    Reorders the query output using the helper column—restoring the original data order.

Does This Work with a PIVOT Clause?

Yes, this method works with pivot queries too.

Here’s a dataset with an added “Category” column in range A1:C:

CategoryItemsQuantity
ImportedPeer100
LocalOrange50
LocalApple75
ImportedApple75
ImportedPeer100
LocalPeer100
ImportedBanana50

Use this formula:

=LET(
   qry, QUERY(A1:C, "select B, sum(C) where B is not null group by B pivot A", 1),
   helper, ARRAYFORMULA(IFNA(XMATCH(CHOOSECOLS(qry, 1), B1:B), 1)),
   SORT(qry, helper, 1)
)

It works the same way—preserving the order of the “Fruits” column as they first appear in the source, even with pivoted columns.

Conclusion

The QUERY function in Google Sheets automatically sorts grouped results—often helpful, but sometimes frustrating. Thankfully, using LET, XMATCH, and SORT, you can easily fix the auto-sorting issue in QUERY GROUP BY and maintain your original data order.

This approach is clean, flexible, and works even with pivot clauses—making it ideal for custom reports or dashboards.

You May Also Like

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.

How to Generate Semimonthly Dates in Google Sheets

If you need to generate semimonthly sequential dates in Google Sheets, you can do...

How to Make Just One Page Landscape in Google Docs

Sometimes, you may need to include a wide table, an organizational chart, or a...

Google Sheets: Get the Last Row with Any Data Across Multiple Columns

It’s common to have several empty rows at the bottom of a Google Sheet,...

How to Calculate Digital Root in Google Sheets

The digital root is the single-digit value you get by repeatedly summing the digits...

More like this

How to Generate Semimonthly Dates in Google Sheets

If you need to generate semimonthly sequential dates in Google Sheets, you can do...

Google Sheets: Get the Last Row with Any Data Across Multiple Columns

It’s common to have several empty rows at the bottom of a Google Sheet,...

How to Calculate Digital Root in Google Sheets

The digital root is the single-digit value you get by repeatedly summing the digits...

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.