Manually reshaping two-dimensional Excel matrices into flat, database-ready lists is a tedious, error-prone chore for data analysts. While traditional workflows rely on manual copying or launching Power Query, these standard methods often disrupt active modeling. Fortunately, modern dynamic array formulas grant immediate, real-time automation without leaving your workbook. As a brief stipulation, this technique requires Excel 365 to utilize advanced layout functions. For example, you can seamlessly convert a monthly cross-tabulated sales grid into a clean, three-column ledger. Below, we outline the exact nested formula construction to automate your data flattening.
In data analysis, the structure of your data determines the ease of your workflow. Often, we encounter data in a two-dimensional (2D) table (also known as a matrix or crosstab). While 2D tables are excellent for human readability and quick manual entry, they are highly inefficient for data analysis tools like Pivot Tables, Power BI, or Python's Pandas. These systems require data in a normalized, flat list format (often referred to as unpivoting or flattening).
This article provides a comprehensive guide on how to use modern and traditional Excel formulas to transpose 2D tables into flat lists, as well as the reverse process: unflattening flat lists back into structured matrices.
Imagine you have a sales table where row headers represent Products, column headers represent Months, and the intersecting cells represent Sales Value. We want to transform this into a three-column database list:
With the release of Excel 365 and Excel 2021, dynamic arrays and helper functions like LET, TOCOL, and HSTACK have turned complex array math into elegant, readable formulas. Below is the ultimate formula to flatten any 2D grid into a flat table dynamically.
=LET(
row_headers, A2:A10,
col_headers, B1:G1,
data_grid, B2:G10,
flat_rows, TOCOL(IF(SEQUENCE(1, COLUMNS(col_headers)), row_headers)),
flat_cols, TOCOL(IF(SEQUENCE(ROWS(row_headers), 1), col_headers)),
flat_vals, TOCOL(data_grid),
result, HSTACK(flat_rows, flat_cols, flat_vals),
FILTER(result, flat_vals <> "")
)
LET Function: This function defines variables (like row_headers, col_headers, and data_grid) to keep the formula organized, fast, and easy to edit.flat_rows):
The formula IF(SEQUENCE(1, COLUMNS(col_headers)), row_headers) broadcasts (stretches) the vertical row headers across multiple columns to match the width of our data grid. Applying TOCOL then flattens this temporary matrix into a single vertical array, perfectly repeating each row header for every corresponding column.
flat_cols):
Similarly, IF(SEQUENCE(ROWS(row_headers), 1), col_headers) broadcasts the horizontal column headers down across rows, matching the height of our grid. TOCOL flattens this so that the column headers cycle sequentially.
flat_vals):
The TOCOL(data_grid) function flattens the intersection values row-by-row into a single, continuous column.
HSTACK & FILTER):
HSTACK merges the three flat arrays side-by-side. Finally, FILTER removes rows where the value is blank, ensuring a clean, compact database list.If you are working on legacy versions of Excel that do not support dynamic arrays or TOCOL, you can achieve the same result using index-matching mathematical logic. We will combine INDEX, INT, MOD, and ROW functions.
To pull from a 2D grid of size R (rows) by C (columns) using a single, incrementing row counter k (starting at 0):
INT(k / C) + 1MOD(k, C) + 1Assuming your 2D table spans from A1 to G10 (where A2:A10 are Row Headers, B1:G1 are Column Headers, and B2:G10 are Values). In your flat list destination sheet, enter the following formulas on row 2 and drag them down:
=INDEX($A$2:$A$10, INT((ROW(A1)-1)/COLUMNS($B$1:$G$1))+1)
=INDEX($B$1:$G$1, MOD(ROW(A1)-1, COLUMNS($B$1:$G$1))+1)
=INDEX($B$2:$G$10, INT((ROW(A1)-1)/COLUMNS($B$1:$G$1))+1, MOD(ROW(A1)-1, COLUMNS($B$1:$G$1))+1)
Note: Drag these formulas down until they return an #REF! error, which indicates that you have successfully mapped all permutations of the table.
What if you have a database flat list (Product, Month, Value) and want to generate a structured 2D table from scratch using dynamic array formulas? This is often referred to as "unflattening."
Let's assume your flat list is a structured table named FlatTable with columns: [Product], [Month], and [Sales].
| Formula Location | Formula | Description |
|---|---|---|
| Row Headers (e.g., E2) | =UNIQUE(FlatTable[Product]) |
Extracts a distinct vertical list of all products. |
| Column Headers (e.g., F1) | =TRANSPOSE(UNIQUE(FlatTable[Month])) |
Extracts a distinct horizontal list of months. |
| Values Intersection (e.g., F2) | =SUMIFS(FlatTable[Sales], FlatTable[Product], E2#, FlatTable[Month], F1#) |
Populates the whole matrix using the spill ranges (#). |
By referencing E2# and F1#, the SUMIFS calculation automatically spills across both rows and columns to fill the entire matrix perfectly. If your data updates, the 2D matrix adjusts its dimensions dynamically.
When choosing a method to transpose your data, consider the size of your dataset and your Excel version:
LET and TOCOL formula. It's incredibly fast, clean, and requires no manual copy-paste actions.INDEX, INT, and MOD math approach. It is reliable but can slow down your workbook if applied to tens of thousands of rows.Data > From Sheet, select the header columns, right-click, and select Unpivot Other Columns.Mastering these formulas allows you to fluidly shift between readable presentation layouts and clean, normalized backend databases without ever leaving the formula bar.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.