How to Unstack Data into Groups in Google Sheets

You don’t need any plugins or Apps Script to unstack data in Google Sheets. All you need is my custom array formula.

Understanding Unstacking Data

Before we dive into unstacking data into corresponding groups in Google Sheets, let’s clarify what “unstacking data” means.

Stacked Data, also known as long-format data, has key characteristics that set it apart from unstacked (wide-format) data:

  • In stacked data, each row generally shows one record or entry related to a specific detail. For example, in a dataset tracking sales, each row might represent an individual sale.
  • Each row often includes an item, its category, and quantity. The next row may represent a different or the same item, possibly under the same or a different category, with its corresponding quantity.

Unstacking transforms stacked data into a format where categories or groups are represented as separate columns, allowing for easier analysis and visualization.

For instance, unstacking can organize items and their quantities under each respective category in separate columns, grouping data more visibly by category.

Side note: Long-format (stacked) data is especially useful for statistical analysis in Google Sheets. You can use functions like QUERY or the Pivot Table tool to summarize the data, and also apply lookup functions more efficiently.

Unstacking Two-Column Stacked Data in Google Sheets

Consider a sample dataset with country names in column A and their assigned groups in a football event in column B. This is stacked data since each row represents a single team.

Example of two-column stacked data in Google Sheets

When unstacking, there will be a key column. In this case, column B contains the groups assigned to each country.

You can use the following formula in cell D1 to unstack the data in columns A and B:

=IFNA(
   REDUCE(
      TOROW(,1), 
      TOCOL(UNIQUE(B1:B), 1), 
      LAMBDA(acc, val, HSTACK(acc, VSTACK(val, FILTER(A1:A, B1:B=val))))
   )
)

In this formula, replace B1:B with the key (category) column and A1:A with the other column.

Example of two-column unstacked data in Google Sheets

Formula Breakdown

This formula is simple to implement, but it can be somewhat complex to understand, as it uses a custom LAMBDA function within the REDUCE function. Here’s a step-by-step explanation of the formula: LAMBDA(acc, val, HSTACK(acc, VSTACK(val, FILTER(A1:A, B1:B=val)))).

Understanding the REDUCE Function:

The REDUCE function takes an initial value (an empty array specified as TOROW(,1)) and applies the LAMBDA function to each value in the unique array: TOCOL(UNIQUE(B1:B), 1).

Key Elements of the LAMBDA Function:

  1. FILTER(A1, B1=val) – This part filters the country names in A1:A where the group in B1:B matches val. Here, val refers to a unique group name from column B. For instance, if val is “Group A,” the formula will return:
    Russia
    Saudi Arabia
    Egypt
    Uruguay
  2. VSTACK(val, FILTER(A1, B1=val)) – This vertically stacks the val (e.g., “Group A”) with the filtered data, resulting in:
    Group A
    Russia
    Saudi Arabia
    Egypt
    Uruguay
  3. HSTACK(acc, VSTACK(val, FILTER(A1, B1=val))) – The HSTACK function horizontally appends the accumulated result (acc) with the output from the previous step. In the LAMBDA function, acc refers to the accumulated data, and val represents each unique category in column B:
    LAMBDA(acc, val, HSTACK(acc, VSTACK(val, FILTER(A1:A, B1:B=val))))

As a result, you obtain the unstacked data.

Unstacking Three-Column Stacked Data in Google Sheets

In the next example, suppose we have item names in column A, their categories in column B, and quantities in column C.

Example of three-column unstacked data in Google Sheets

To unstack the data based on the category in column B, we’ll modify the filter slightly.

Previously, the formula was:

FILTER(A1:A, B1:B=val)

Now, we want to include both A1:A and C1:C, so the filter becomes:

FILTER(HSTACK(A1:A, C1:C), B1:B=val)

The updated formula will be:

=IFNA(
   REDUCE(
      TOROW(,1), 
      TOCOL(UNIQUE(B1:B), 1), 
      LAMBDA(acc, val, HSTACK(acc, VSTACK(val, FILTER(HSTACK(A1:A, C1:C), B1:B=val))))
   )
)

Conclusion

That’s all about how to unstack data in Google Sheets. This process allows you to organize your data into a more usable format for analysis.

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.

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

More like this

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

4 COMMENTS

  1. Hi Prashanth!

    Thank you for this wonderful resource!

    I have some stacked data that is in 5 columns. Column headings are as follows:

    Survey | Language | ID | Question | Value

    Is there an expanded version of this formula that would work for me?

    Many thanks!

  2. Hello. Thank you for your great article. I have tried the solution for a Google Sheet I am working on however it is not producing the results we hoped for. I have a list of attendees for different events. The columns are First Name, Last Name, Email, and Event.

    The first three columns (Name, Last Name, Email) will contain the same information when someone signed up for a unique event. The data looks like this.

    first name A | last name A | email A | Event Name 1
    first name A | last name A | email A | Event Name 2
    first name B | last name B | email A | Event Name 1
    etc.

    What we would like to do is have a single row with:

    First Name, Last Name, Email, Event 1, Event, 2, etc. from our current stacked data.

    First Name A | Last Name A | email A | Event Name 1 | Event Name 2

    First Name B | Last Name B | email B | Event Name 1

    Is this possible with Google Sheets?

    Thank you in advance for your time.

    • Hi, Joe,

      Welcome to Info Inspired!

      The tutorial above is not the one that you may want to follow. Here is the proper one – How to Aggregate Strings Using Query in Google Sheets.

      The tutorial contains the basic but only deals with two columns of data. Since you have more columns I have customized the formula for you. Here is that.

      =ArrayFormula(query(query({A2:D,if(len(A2:A),row(A2:A)-match(A2:A&B2:B,A2:A&B2:B,0),)},"Select Col1,Col2,Col3, max(Col4) where Col1 is not null group by Col1,Col2,Col3 Pivot Col5"),"Select * offset 1",0))

      Make sure that you have sorted your data.

      I am considering the data range A2:D (I am normally leaving the first row as it may contain field labels). So adjust your data accordingly.

      Please do check the tutorial (link shared above) to understand this formula.

      Best,

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.