HomeGoogle DocsSpreadsheetHow to Use the FLATTEN Function in Google Sheets

How to Use the FLATTEN Function in Google Sheets

Published on

In this post, you can learn how to use the FLATTEN function in Google Sheets to flatten a range. Also, I have included the steps to unflatten a flattened range using the SKIPPING clause in QUERY.

As far as I know, both the FLATTEN function and the Query SKIPPING clause are not officially documented but work in Google Sheets.

Let’s begin with how to use the FLATTEN function in Google Sheets.

The better way to find whether a function is correctly spelled or available in Google Sheets is just by entering it in a cell. The function in question must be entered in the following manner.

=function_name()

If the function is misspelled or not available the result would be the #NAME? error. On the other hand, if the function is available, in most cases you will see the #N/A error.

You May Like: Different Error Types in Google Sheets and How to Correct It.

Some of the exceptions to the above are the functions ROW, COLUMN, TODAY, NOW, NA, FALSE, TRUE, and FALSE.

I am talking all these because even the function FLATTEN is not officially documented, or not available under the menu Insert > Function, it’s available in Google Sheets.

=flatten()

Key the above Flatten function in any cell in Google Sheets to see the #N/A? error, The tooltip (hover over the error) would say;

Wrong number of arguments to FLATTEN. Expected at least 1 arguments, but received 0 arguments.

Syntax and Arguments of the Flatten Function in Google Sheets

Since the function is not officially documented, the function argument is also not available in an official form. So here is my interpretation of the same.

Syntax: flatten(value1, [value2, ...])

Arguments:

value1 – The first value or range to consider for flatten.
value2, … – [ OPTIONAL ] – Additional values or ranges to consider for flatten.

Purpose:

The purpose of the FLATTEN function in Google Sheets is to flatten values in an array into separate rows in a column.

Example to the FLATTEN Function

In the below example, the array A1:D flattened to rows in F1:F.

Screenshot # 1:

Example to the FLATTEN Function in Google Sheets

Real Life Use (Unique Scattered Values and Summarize)

We can use this function to unique or summarize the scattered values in a sheet.

For example, see the berth preferences of passengers and unique preferences and summary.

Screenshot # 2:

Unique Scattered Values and Summarize in Google Sheets

The UNIQUE formula, i.e. =unique(B2:D), won’t yield the desired result. Instead, we can use it as =unique(flatten(B2:D)). The formula is in use in cell F2.

For summary, I have used the following QUERY in cell H2.

=query(
     flatten(B2:D),
     "Select Col1, count(Col1)
     where Col1 is not null
     group by Col1
     label count(Col1)''"
)

How to Unflatten a Flattened Range in Google Sheets?

We can use the QUERY function here.

See Screenshot # 1 above. The flattened values are in the array F1:F. Let’s see how to unflatten it using the QUERY.

Since there are numbers and text strings in column F, we can call it a mixed data type column. In such a column you must use the TO_TEXT function with QUERY. I’ll come to that later.

Earlier I’ve explained how to use the clause SKIPPING in QUERY. Didn’t see that? Then here it is – Import Every Nth Row in Google Sheets Using Query or Filter (Same File).

We are going to use that clause here.

Steps to Unflatten a Flattened Array in Google Sheets

From the dataset in A1:D (please refer to Screenshot # 1) you can understand that the range contains 4 columns.

If you only have the flattened range like the one in F1:F, you must analyze it to find the number of columns you want in the unflattened range in each column.

I’m considering it as 4. So we will skip 4 columns in QUERY. Now let’s begin.

Formula in cell H1:

=ArrayFormula(
    query(
      to_text(F1:F),"Skipping 4"
    )
)

I’ve wrapped the range F1:F with the TO_TEXT non-array function since the range contains mixed-type data. To make the range again an array I’ve used the ARRAYFORMULA function.

We need three more formulas as there are 4 columns and that will be in cells I1, J1, and K1. The changes in the formula in each cell are like this. Change the range F1:F in the formula to F2:F, F3:F, and F4:F respectively.

But if you replace F1:F in the above formula in cell H1 with indirect("F"&column(A1)&":F"), you can drag it to I1:K1.

=ArrayFormula(
     query(
        to_text(indirect("F"&column(A1)&":F")),"Skipping 4"
     )
)

Screenshot # 3:

Unflatten a Range in Google Sheets

Can you explain to us the Indirect part?

Yes! Here it is!

In the INDIRECT formula, the letter “F” represents the column F.

The cell reference A1 represents the row number. I mean, if the flattened values are in F5:F, not in F1:F, then A1 should be replaced by E1 as column(E1) would return 5.

That’s all about the use of the FLATTEN Function in Google Sheets. Enjoy!

Credit: https://support.google.com/docs/thread/33715001?hl=en

Resources:

  1. A Simple Formula to Unpivot a Dataset in Google Sheets (Using FLATTEN).
  2. How to Move Each Set of Rows to Columns in Google Sheets.
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.

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...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

SUMPRODUCT Differences: Excel vs. Google Sheets

The core purpose of the SUMPRODUCT function remains consistent across both Excel and Google...

More like this

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...

Analyzing Column A Between Non-Adjacent Values in Column B: Google Sheets

This tutorial addresses a common scenario encountered in Google Sheets: how to sum, average,...

5 COMMENTS

  1. This example gives a single-cell formula (without using Google Apps Script) to unpivot a matrix by using the FLATTEN function.

    Unpivot a pivot table data using Flatten function in Google Sheets

    Honestly speaking, I don’t think it can be further simplified.

    Thanks a ton for introducing the FLATTEN function.

    • That is a FANTASTIC formula. I’ve been using a script from GitHub called UNPIVOT for years. It works very well but this is built right into Sheets. I’m going to put it to use and see how it works in battle. Great find! Thanks!

  2. It is really incredible to know that there are formulas that are valid in Google Sheets but are not included in the list.

    This FLATTEN immediately gave me an idea to use it to unpivot a matrix. And it worked !!!

    Thanx and regards.

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.