How to Calculate Percentage of Grand Total in Google Sheets Query

Published on

We can take the help of the LET function to simplify the formula that calculates the percentage of grand total in the Google Sheets QUERY function. But before jumping into that, let me show you how to do the same using a Pivot Table. Because, honestly, Pivot Table is the easiest method.

Then, you might ask—why use QUERY? The reason is simple: when you want more flexibility and the ability to create pivot charts, the better option is the QUERY function.

How to Properly Display the Percentage of Grand Total in a Pivot Table

For this example, I’m using a small dataset with five rows (including the header row), like this:

Displaying Grand Total and Percentage of Grand Total in a Google Sheets Pivot Table

Follow these five steps to group the data and display the percent of grand total using a Pivot Table:

Steps

  1. Select the range A1:B5. (If your range is different, select accordingly.)
  2. Click Insert > Pivot table.
  3. Check Existing Sheet and enter a cell address, for example, D1.
  4. Click Create.
  5. In the Pivot Table Editor:
    • Add Item under Rows.
    • Add Amount under Values.
    • Ensure Summarise by is set to SUM (this is usually the default).
    • Add Amount again under Values.
    • For the second Amount, under Show as, select % of grand total.

That’s all.

Tip: You can click on cells E1 and F1 in the Pivot Table to modify the field labels.

That’s how we display the percentage of grand total in a Pivot Table.

How to Calculate the Percentage of Grand Total Using the QUERY Function

Let’s now go step-by-step using the QUERY function. To summarize the data by item, like in the Pivot Table above, use this formula:

=QUERY(A1:B, "SELECT Col1, SUM(Col2) WHERE Col1 IS NOT NULL GROUP BY Col1", 1)

This formula returns:

Itemsum Amount
Apple250
Mango200
Orange250

Now, how do we add a percentage of grand total column just like the Pivot Table?

The logic is:

Percent of grand total = Total for each item ÷ Grand total

To achieve this, we’ll use the CHOOSECOLS function to extract columns from the QUERY result, and the LET function to simplify the formula.

Here’s the formula:

=ARRAYFORMULA(
  LET(
    qry, QUERY(A1:B, "SELECT Col1, SUM(Col2) WHERE Col1 IS NOT NULL GROUP BY Col1", 1),
    col_1, CHOOSECOLS(qry, 1),
    col_2, CHOOSECOLS(qry, 2),
    HSTACK(qry, IFERROR(col_2/SUM(col_2), "% of Grand Total"))
  )
)
Calculating Percentage of Grand Total in QUERY Function in Google Sheets

Formula Breakdown:

  • QUERY(...): Summarizes the data.
  • col_1 and col_2: Extract individual columns using CHOOSECOLS.
  • col_2/SUM(col_2): Divides each item’s total by the grand total.
  • IFERROR(..., "% of Grand Total"): Replaces the header row error with a label.
  • HSTACK: Combines the QUERY result with the new percentage column.

Note: You can format the third column by selecting it and choosing Format > Number > Percent.

To rename headers, wrap the formula with another QUERY like this:

=QUERY(
  your_formula,
  "LABEL Col1 'Item', Col2 'Total', Col3 '% of Grand Total'",
  1
)

Add a Grand Total Row to the QUERY Result with Percentage of Grand Total Column

To append a dynamic total row, use this formula. Replace your_formula with the full formula above and set n as the number of text columns (e.g., 1 in this case):

=ArrayFormula(LET(
   ftr, your_formula, 
   tc, n, 
   VSTACK(ftr, HSTACK("Total", DSUM(ftr, SEQUENCE(1, COLUMNS(ftr)-tc, tc+1), IFNA(VSTACK(CHOOSEROWS(ftr, 1), )))))
))

For a detailed breakdown of this logic, check out my tutorial: Dynamic Total Row for FILTER, QUERY, or ARRAY Results in Sheets

Resources

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.

Custom Order for QUERY Pivot Headers in Google Sheets

By default, when you use the PIVOT clause in a Google Sheets QUERY, the...

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

More like this

Custom Order for QUERY Pivot Headers in Google Sheets

By default, when you use the PIVOT clause in a Google Sheets QUERY, the...

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

4 COMMENTS

  1. Thank you for this.

    Is there a way for this to work with additional where conditions?

    For example, if you add where not(Col1)= 'apple', the orange and mango % will still show the same when they should be 200/(200+250)-mango and 250/(200+250)-orange.

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.