Understand the Label Clause in Google Sheets Query

Are you tired of struggling to organize and customize the output of your Google Sheets Query formula? Fear not! In this tutorial, I will reveal the secrets of the Label clause in Google Sheets Query and show you how to use it masterfully to set or remove labels for columns in a Query formula output.

The Purpose of the Label Clause

The Label clause in Google Sheets Query serves a crucial purpose – it allows you to set or remove labels for one or more columns in the output of a Query formula. With this powerful tool, you can easily rename output columns of Query aggregation and scalar functions or arithmetic operators, making it a breeze to understand and analyze your precious data.

Basic: Label Columns Using the Label Clause

Let’s start with the basics. The Label clause empowers you to set labels for individual columns or even multiple columns in the Query result.

1. Label Clause to Set One Label

To set a label for a single column in your Query, simply use the following syntax:

label column_id label_string

Here’s an example to label column A as “Subject 1”:

=query(A1:E7,"Select * label A 'Subject 1'")

2. Label Clause to Set Multiple Labels

When you want to set labels for multiple columns in your Query, use the following syntax:

label column_id label_string [,column_id label_string]

For instance, if you have columns C, D, and E in your data and you want to label them as “Subject 1”, “Subject 2”, and “Subject 3”, respectively, try the following formula:

=query(A1:E7,"Select * label C 'Subject 1', D 'Subject 2', E 'Subject 3'")

The Label clause grants you the power to customize, replace, or modify the labels of specified columns while retaining the original labels of other columns.

Advanced: Label Clause in Query Aggregation, Scalar Functions, and Operators

The Label clause is not limited to labeling columns alone; it can also be used with Query aggregation functions, scalar functions, and operators to modify or remove headers in the Query result.

1. Remove or Modify Headers of Aggregation Function Output

When using aggregation functions in your Query, you can modify the headers of the result columns using the Label clause. Take note of these two key points:

  1. Aggregation functions take a single-column identifier as an argument.
  2. The header of the result column should be in the format function_name label.

For example, to label the sum of column B as “Total Amount”, try the following formula:

=query(A1:B14,"Select sum(B) label sum(B) 'Total Amount'")

You can even use expressions as the data in your Query formula. If you want to combine two data ranges and total the second column, give this formula a whirl:

=Query({A1:B10;D1:E5},"Select Sum(Col2) label Sum(Col2) 'Total Amount'")

The Label clause grants you the freedom to customize the headers of the Query result based on your unique requirements.

2. Remove or Modify Headers of Scalar Function Output

Scalar functions in Query, such as year(), month(), day(), and upper(), can also have their headers modified using the Label clause.

For example, if you want to label the result of the year() function as “Year” and the sum of column C as “Total”, give this formula a go:

=Query(A1:C14,"Select year(A), sum(C) group by year(A) label year(A) 'Year', Sum(C) 'Total'")

Modifying or removing the headers of scalar function output has never been easier with the Label clause in Query.

3. Remove or Modify Headers of Operator Formula Output

When using arithmetic operators (+, -, *, /) in Query, the headers of the result columns can become cumbersome to read. Fear not, for the Label clause comes to the rescue!

For instance, to label the result of the formula C+D+E as “Total”, try this formula:

=Query(A1:E7,"Select A, C+D+E label C+D+E 'Total'")

The Label clause allows you to customize the headers of operator formula output, making them easier to comprehend and analyze.

Can I Use the Label Clause to Modify the Query Pivot Header?

Absolutely! You have the power to modify the Query Pivot Header by wrapping the Query formula that Pivots with another Query formula and using the Label clause in the second Query.

For example, let’s say you have a Query formula that pivots data, and you want to modify the Pivot Header labels. Try this formula:

=query(query(A1:F7,"Select A, sum(F) group by A Pivot B"),"Select * label Col2 'Term I', Col3 'Term II', Col4 'Term III'")

With this technique, you can effortlessly customize the labels/headers in your Query Pivot, making them more meaningful and relevant.

I hope this tutorial has provided you with a comprehensive understanding of the Label clause in Google Sheets Query. Don’t hesitate to experiment and tailor your Query formula outputs to suit your specific needs. Happy querying!

For more Google Sheets tips and tricks, visit Crawlan.com. Happy spreadsheeting!

Related posts