HomeGoogle DocsSpreadsheetGrouping and Subtotal in Google Sheets and Excel

Grouping and Subtotal in Google Sheets and Excel

Published on

I am badly missing the Excel-like grouping and subtotal functionality in Google Sheets. I am sure that sooner or later Google will implement this feature in Google Spreadsheets.

In the meantime, we should find some workarounds to replicate the Excel-like grouping and subtotal in Google Sheets.

I have one solution that uses the SUMIF spreadsheet formula and the row grouping feature that Google added recently.

Grouping and Subtotal in Spreadsheets

In Excel, this grouping and subtotal functionality is built-in and named Subtotal (not the Subtotal function).

It helps users to total several rows of related data (groups of data) by automatically inserting subtotals.

In this type of subtotaling in Excel, you have the freedom to choose the total to appear at each change in your preferred column, and also the aggregation function to use (sum, average, count, etc.).

Let me show you how to group and subtotal in Excel first. This will give you a clear picture of what this topic is about.

Sample Data for Subtotal and Grouping in Google Sheets and Excel

The following is the sample data that I am using in the below examples in both Excel and Google Sheets.

sample data for grouping and subtotal

This is my sample data for row grouping and subtotaling. The data is sorted by Column A, and that is necessary. This is because my grouping of data is based on Column A, i.e., by continent names.

The list shows the world’s 20 largest apple-producing countries and their production in metric tons (MT). I am not mentioning the year of production, and I am also not confirming the authenticity of the data. This is just for our example purpose only.

Excel Grouping and Subtotal Example

In this part, I am focussing on Excel to make you understand what is row grouping and subtotal.

Steps:

  1. Select the data in the range A1:C21.
  2. Click on the Data menu and Select Subtotal in the Outline group.
  3. Follow the below settings and click OK.
Excel Subtotal settings

That’s all you need to do in Excel. You can now collapse the row grouping, and it will look like this:

Grouped Excel Data (expanded view by default):

Grouped Excel data with subtotal

Collapsed Excel Data:

Collapse grouping in Excel

The subtotal and grouping features are easy to implement in Excel since they are built-in. What about Google Sheets?

Grouping and Subtotal in Google Sheets

I will guide you on how to do grouping and subtotaling in Google Sheets, which includes some workarounds.

As mentioned, I will use the SUMIF function, and then the newly added row grouping to achieve this.

First of all, see how this subtotal and grouping (my workaround) works in Google Sheets.

Grouping and Subtotal in demo in Google Sheets

Let’s begin.

The following steps include some manual row inserting and helper columns. You can skip the row inserting if you use one of my Named Functions. We will come to that later on in this tutorial.

Note: The sample data is the same as what I used in the Excel example.

Steps involved in Subtotal and Grouping in Google Sheets

1. Insert new rows below each group. You can see the newly inserted rows in the image below (rows #3, 12, 19, 22, and 26).

2. Then, insert one helper column after column B and type the text strings as per the screenshot below (column C) in those rows.

3. Here is the SUMIF array formula that can populate subtotals in Google Sheets. You should apply this formula in cell E3 only.

Grouping and Subtotal Formula:
=ArrayFormula(if(len(A2:A),sumif(A2:A&" Total",C3:C,D2:D),))

Do not drag or copy down this formula. It will automatically expand. You only need to empty the range E3:E before inserting this SUMIF formula.

Grouping and Subtotal workarounds

In column E, the SUMIF formula returns the group-wise (continent-wise) total of apple (fruit) production. However, there are blank cells and cells with 0 (zero) values. To remove those unwanted zeroes, we can apply a conditional formatting rule.

4. Select the range E3:E and then in Format > Conditional formatting, set the rule as follows:

remove zero from Sumif array output

If you falter with this, here is the detailed tutorial: Two Ways to Remove 0 Values in Google Sheets.

We have now reached the final part of grouping and subtotaling in Google Sheets. I mean, we have now a subtotal for each group. The only thing left to do is row grouping. Here is how to do that.

5. Select the entire row A2 by right-clicking on the row number. From the opened shortcut menu, select “View more row actions” > “Group row”.

Then, select rows 4 to 11. To do this, go to any cell in row # 4 and then press and hold the Shift key while clicking on row # 11. Finally, right-click to open the shortcut menu and apply the grouping.

Follow this grouping for all the groups, namely rows 13 to 18, 20 to 21, and 23 to 25.

Must Check: How to Group Rows and Columns in Google Sheets.

Excel similar Subtotal in Google Sheets

That’s it! You have now completed the grouping and subtotaling in Google Sheets.

Get My Sample Sheet

Conclusion

I know this is not a perfect solution, as it requires some row inserting and an additional helper column. However, the output somewhat mimics Excel Subtotal.

If you only want to place the subtotal at the first or last row of each group, you can simply use my AT_EACH_CHANGE named function. One of the advantages of this custom function is that you can insert average, count, min, max, etc., in addition to sum.

I hope Google will include a built-in Subtotal data menu option in Sheets in the near future.

Related Resources:-

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

More like this

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

6 COMMENTS

  1. This is incredibly helpful. Thank you, Prasanth!

    I think there’s a small typo in the Grouping and Subtotal Formula example

    =ArrayFormula(if(len(A2:A),sumif(A2:A&" Total",B3:B,D2:D),))

    For the example, the formula should subtotal C3:C instead of B3:B.

    • Looking at the guidance, I notice that google sheets groups are based on the header above.

      So in the example, you would want the subtotals above the group to expand.

      Is there a workaround for this in Google Sheets? I know Excel allows the group header to be the subtotal row.

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.