• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Script Everything

  • Spreadsheets
  • Python
  • Blog
Home ● Spreadsheets ● Google Sheets ● How To Change Aggregate Name In Google Sheet QUERY Function (Examples)

How To Change Aggregate Name In Google Sheet QUERY Function (Examples)

August 9, 2022 by Ryan Sheehy

How do you change the default aggregate name created in Google Sheets when using the QUERY() function?

To change the header label of an aggregate column from a QUERY() function append LABEL aggregate_column 'YOUR LABEL' to your SELECT statement.

For example, if you had the following QUERY formula in your Google Sheet and you wanted to change the default label of sum(Sales Qty) to Total Sold then this is how this would look, here’s the original QUERY() formula:

=QUERY(Data!A:C, "SELECT SUM(B), SUM(C)")

Here’s how this formula looks by default as seen in the Agg1 sheet:

Custom Aggregate Column Name

To create your own label on the aggregate column, use the LABEL syntax in your query statement. If you wanted to add a more precise name to the SUM(B) column header, all you would need do is append the following syntax to your SELECT statement:

=QUERY(Data!A:C, "SELECT SUM(B), SUM(C) LABEL SUM(B) 'Total Sold', SUM(C) 'Total Sales'")

The main thing to emphasise when appending a label, just as it is here with LABEL SUM(B) 'Total Sold' that I’d like to emphasise, is that the aggregate column defined after the word LABEL needs to be written exactly the same as it is in the SELECT area.

As you can see with the example, the SELECT has one aggregate column defined as SUM(B) therefore, this needs to be used in the LABEL area if you wanted to rename this column header.

This is true even if the aggregate function is a formula containing multiple aggregates, for example, if you had an aggregate column like SUM(B) - SUM(C) to create a custom label for that aggregate, you would need to append the same in the label area, like LABEL SUM(B) - SUM(C) 'Differences' .

Here’s how the resulting aggregate result now looks on the spreadsheet in sheet Agg2 when you name your aggregate columns using the LABEL feature:

Aggregate Names Not Changing

If you are experiencing problems with using the LABEL in your query statement when using the QUERY function check each of the following:

1. The LABEL is using the correct aggregate function(s) in the SELECT area of your statement. Remember, the syntax needs to be LABEL aggregate_column 'Custom Name' where aggregate_column needs to match the aggregate column in the SELECT area.

2. The name of the aggregate label is wrapped in single quotes.

3. The position of LABEL is towards the end of your statement.

I have found one of these to be the reason why a name for an aggregate column did not work.

Summary

Aggregating your data in a spreadsheet can make finding data a lot easier, however, the output of an aggregate result may look a little ugly.

To change the default name produced by Google Sheets from an aggregate column use the LABEL syntax in your query statement and ensure the LABEL aggregate_column matches the function used in the SELECT section of your query statement.

Filed Under: Google Sheets

Primary Sidebar

Hello! My name is Ryan Sheehy the author of ScriptEverything.com

This website acts as a second brain of sorts for all the hacks and explorations I find when trying to solve problems at work.

About Me

Footer

Spreadsheets

I have been using spreadsheets since as early as 1996 and I continue to use this great productivity tool on a daily basis.

Check out some of my most recent discoveries about spreadsheets.

Python Code

I have been using Python since the late 1990’s due to the limitations of Excel’s VBA. I enjoy being able to wrangle and fetch data externally using Python.

Discover more of what I’ve found with Python.

Apps

Sometimes I play, hack and tinker with other applications to scratch an itch.

To find out the latest hack check out my blog feed.

Copyright © 2023 ScriptEverything.com

  • Contact
  • Privacy Policy