How to Easily Flatten Every Other Column in Google Sheets

Have you ever wanted to flatten every other column in Google Sheets? Well, you’re in luck! In this article, I’ll show you a simple and flexible formula that will make this task a breeze.

Introducing the Magic Formula

Before we dive into the formula, let me give you a quick overview. You can use brackets to combine every other column horizontally and then flatten it. And by brackets, I mean braces (curly brackets)!

Let’s take a look at an example. The formula below flattens the arrays C5:C10, E5:E10, and G5:G10:

=flatten({C5:C10,E5:E10,G5:G10})

Easy, right? But the drawback of this approach is that it only works well when you have a few columns to flatten. Don’t worry, though, because I’ve got you covered with a more flexible formula.

A Flexible Formula to Solve All Your Flattening Needs

Imagine you have a price list of items in the range C4:H10. You want to flatten every other column, but you don’t want to rely on a fixed range. Here’s what you can do:

  1. Find the column number of the first column in the range. You can use the formula =column(C4) for that.
  2. If the output is an odd number, use the ISODD function within FILTER; otherwise, use ISEVEN.
  3. Combine FILTER and ISODD (or ISEVEN) in the following formula:
=flatten(filter(C5:H10,isodd(column(C4:H4))=TRUE))

This formula will dynamically flatten every other column in Google Sheets, no matter the size of your range. It filters the range C5:H10 based on the first-row column number, ensuring that only the desired columns are flattened.

Troubleshooting Blank Rows and Columns

Now, let’s address some common questions you may have when flattening every other column in Google Sheets.

  1. What if I have blank columns in my range? Won’t that create blank rows in the result?

Fear not! You can solve this issue by including an additional criterion in the formula. Here’s an updated formula that takes care of blank columns:

=flatten(filter(C5:Z10,isodd(column(C4:Z4))=TRUE,len(C4:Z4)))

By adding len(C4:Z4), the formula excludes the columns with no field label, resulting in a flattened range without any blank rows.

  1. But what about blank rows?

To remove blank rows from the flattened range, you can apply a QUERY function. Open the range C5:Z10 (closed) to C5:Z and wrap the formula with the following QUERY:

=Query(flatten(filter(C5:Z,isodd(column(C4:Z4))=TRUE,len(C4:Z4))),"Select * where Col1 is not null")

This query will remove any rows with a blank first column (Col1) from the flattened range.

  1. Can I use the same formulas to flatten the price columns using ISEVEN?

Absolutely! If you want to flatten the even columns instead of the odd ones, simply replace ISODD with ISEVEN in the formulas mentioned earlier.

Alternatively, if you want to flatten both the odd and even columns, you can use a combined formula like this:

=Query({flatten(filter(C5:Z,isodd(column(C4:Z4))=TRUE,len(C4:Z4))),flatten(filter(C5:Z,iseven(column(C4:Z4))=TRUE,len(C4:Z4)))},"Select * where Col1 is not null")

Make sure to exclude any unwanted columns, like G4 and H4, from the combined formula to avoid mismatching row sizes.

Adding Field Labels to Your Flattened Columns

Flattening every other column may not always be enough, especially when you want to perform lookups. Adding field labels can make your data more meaningful and useful. Here’s how you can do it:

=query(ArrayFormula(split(flatten(filter(C4:Z4,isodd(column(C4:Z4))=TRUE,len(C4:Z4)))&"|"&<strong>filter(C5:Z,isodd(column(C4:Z4))=TRUE,len(C4:Z4))</strong>),"|")),"Select * where Col2 is not null")

In this formula, we combine two filters (one for the header and the other for the records) inside the FLATTEN function. The SPLIT function splits the combined string using the “|” delimiter, creating two columns for the header and corresponding records. Finally, the QUERY function selects the non-null rows.

Similarly, you can modify formula #4 to flatten every other even column and add row headers.

Conclusion

Congratulations! You now have the power to easily flatten every other column in Google Sheets. Whether you need to flatten odd or even columns, troubleshoot blank rows and columns, or add field labels, these formulas have got you covered.

If you want to explore more ways to optimize your Google Sheets experience, head over to Crawlan.com. There, you’ll find a wealth of resources and tips to supercharge your productivity.

Thanks for joining me on this journey. Enjoy exploring the wonders of Google Sheets!

Resources:

  • How to Move Values in Every Alternate Row to Columns in Google Sheets.
  • How to Sum Every Alternate Column in Google Sheets [Flexible Formula].
  • Dynamic Formula to Select Every nth Column in Query in Google Sheets.
  • How to Partially Flatten a Multi-Column Array in Google Sheets.
  • Array Formula to Multiply Every Two Columns and Total in Google Sheets.
  • How to Highlight Every Nth Row or Column in Google Sheets.
  • A Simple Formula to Unpivot a Dataset in Google Sheets.

Related posts