Google Sheets doesn't have a direct "flatten" function, but you can easily transform nested arrays into single-level data using built-in array formulas. This approach is perfect for converting hierarchical data (like nested lists of products with subcategories) into a simple tabular format where each sub-item appears as its own row alongside its parent category. Here's how to do it step by step with practical examples.
What You'll Need
- A Google Sheets document with your nested array data
- The
FLATTENfunction (introduced in Sheets 2023) - Optional: The
UNIQUEfunction for cleaner results
Step-by-Step
- Prepare your data: Start with a table where one column contains your nested arrays. For example: = In the first column (A2), enter this array formula and drag it down to cover all your categories.
-
Create a helper column: In column B (B2), enter this formula:
=ARRAYFORMULA(UNIQUE(A2:A))This extracts just the category names (Apples, Bananas, Electronics) and sorts them. Keep this column—it'll help identify parent categories later. -
Build the output range: In column C, enter:
=FLATTEN(A2:A)Drag this down to generate all items from your nested arrays. This will create a single column with all sub-items in arbitrary order (Red, Green, Yellow, Yellow, iPhone, Pixel, Tablets). -
Add category mapping: For each item in column C, find its parent category using this formula in column D (D2):
=IFERROR(MATCH(C2,$B$2:$B$5,0),"No category")Replace $B$5 with the last cell in your categories column. Drag this down to match each sub-item to its parent (e.g., Red → Apples). -
Optional: Sort results: Use this array formula in column E to order results by category:
=ARRAYFORMULA( IFERROR(SORT( SPLIT(FLATTEN(C2:C), ","), {"$D$2:$D$"&ROW($A$2:A)-1,"$D$2:$D$"&ROW($A$2:A)}, 0 ),""))Drag rightward to add additional sorting criteria (e.g., by subcategory).
Example output would look like this:
Category Subitem
Apples Red
Apples Green
Bananas Yellow
Bananas Yellow
Electronics Smartphones
Electronics Tablets
Common Mistakes
-
Forgetting to include parent categories: The
FLATTENfunction alone won't tell you which sub-item belongs to which parent. Always pair it with the helper column (B) and mapping formula (D). -
Array syntax errors: Google Sheets treats semicolons (;) as array separators. If your data uses commas, wrap the entire array in
=ARRAYFORMULA()and adjust the FLATTEN formula to use commas. -
Matching issues in step 4: If your parent categories aren't consecutive numbers, use
ARRAYFORMULA(IFERROR(MATCH(...)))to handle gaps safely. Example:=IFERROR(MATCH(C2,Index($B$2:$B$5,0,0),0),"No category") -
Overlooking empty cells: If your data has blank rows, add
IFERROR()to prevent errors in the helper column or matching steps.
Tips
-
Batch processing: For large datasets, combine with
QUERYto filter or transform results. Example:=QUERY(FLATTEN(A2:A), "SELECT * WHERE Col1='Electronics'", 1) -
Nested arrays beyond 2 levels: Flatten first to get a single-level array, then apply
FLATTENagain to the result. Example:=FLATTEN(FLATTEN(A2:A)) -
Data validation: After flattening, use
IFERROR()to handle any unmatched items gracefully (e.g., show "Unknown"). -
Indexing shortcut: For dynamic parent category ranges, use
Index($B$2:$B$"&ROW($A$2:A),0,0)instead of hardcoding cell references. -
Alternative for older Sheets: Use this workaround (no
FLATTEN):=SPLIT(FLATTEN(A2:A),"{;}")Then adjust the final steps to handle the semicolon-delimited output.
Troubleshooting
If your formula isn't working, start with these checks:
- Check array syntax: Open the cell with your array formula and press Ctrl+Shift+Enter (Windows/Linux) or Cmd+Shift+Enter (Mac) to see the array formula. Ensure semicolons match your data format.
- Verify cell references: In the helper column formula (step 2), drag the reference to the last category cell. If you get errors when dragging, adjust the formula to use row numbers instead of cell references.
- Test with simple data: Start with a single nested array like before expanding to your full dataset.
- Check for hidden characters: Copy your array data into Google Sheets from another source. Hidden formatting (like extra spaces or formatting) can break array formulas.
-
Update Sheets: If using an older version, enable the
FLATTENfunction by going toExtensions > Apps Script > ImportScript > Paste this code:function onOpen() {SpreadsheetApp.getUi().createMenu('Formulas').addItem('FLATTEN', 'showMenu').addToUi();} function showMenu() {SpreadsheetApp.getUi().alert('FLATTEN is now available. Type =FLATTEN in a cell to use it.');}
If you still encounter errors, examine the actual error message in the cell where the formula is entered. Common errors include:
#REF!→ Parent category range mismatch#VALUE!→ Data type mismatch (e.g., mixing numbers and text)#N/A→ Items without parent categories
Frequently Asked Questions
How do I flatten an array with mixed data types (numbers and text)?
Use ARRAYFORMULA(FLATTEN(IF(A2:A="",,"A2:A"))) to filter out empty cells or convert numbers to text with TEXT(A2:A,"") before flattening.
What if my sub-items are in separate columns?
Combine them into a single array first using =ARRAYFORMULA(FLATTEN(ARRAYFORMULA(SPLIT(A2:A,";")))) or =ARRAYFORMULA(FLATTEN(ARRAYFORMULA(SPLIT(B2:B,";")))) based on your data format.
Can I flatten a large dataset efficiently?
Yes, but avoid nested formulas. Split the array into steps:
1. Step 1: =UNIQUE(A2:A) for parent categories
2. Step 2: =FLATTEN(A2:A) for all items
3. Step 3: Use INDEX/MATCH with row numbers for faster mapping
What's the difference between FLATTEN and UNIQUE?
FLATTEN UNIQUE removes duplicates from existing single-level data (e.g., {"B","A","B"} → {"A","B"}).
How do I handle arrays with missing sub-items?
Use IFERROR() in your mapping formula to catch missing matches:
=IFERROR(MATCH(C2,$B$2:$B$5,0),"No category")
Can I flatten arrays in Google Sheets without FLATTEN?
Yes, use SPLIT() with semicolons:
=SPLIT(FLATTEN(A2:A),"{;}")
Then adjust the final steps to handle comma-separated output instead of semicolon-based.