To achieve this in Excel 365, you can use advanced formula functions like TEXTSPLIT, TOCOL, and SEQUENCE to dynamically split the comma-separated lists into individual rows while repeating the corresponding KEY value for each item. Here's how you can do it step-by-step:
Assumptions
- Your table is structured as follows:
- Column A:
KEY
- Column B:
LIST (comma-separated values)
- You want a dynamic array output where:
- Column 1 contains the
KEY.
- Column 2 contains the individual items from the
LIST.
Solution
Step 1: Use TEXTSPLIT to Split the Comma-Separated Values
The TEXTSPLIT function splits text strings into an array based on a delimiter. In this case, we'll split the LIST column by commas.
Step 2: Use TOCOL to Flatten the Array
After splitting the LIST column, we need to flatten the resulting array into a single column. The TOCOL function helps with this.
Step 3: Repeat the KEY Values
We use the SEQUENCE function to repeat the KEY values for each item in the LIST.
Final Formula
Here’s the complete formula to generate the desired dynamic array:
=LET(
keys, A2:A100, // Range of the KEY column
lists, B2:B100, // Range of the LIST column
split_lists, TEXTSPLIT(lists, ","), // Split the LIST column by commas
flattened_lists, TOCOL(split_lists, 1), // Flatten the split lists into a single column
repeated_keys, INDEX(keys, INT(SEQUENCE(ROWS(flattened_lists), 1, 1, 1/ROWS(split_lists)))), // Repeat keys for each item
HSTACK(repeated_keys, flattened_lists) // Combine keys and items into two columns
)
Explanation of the Formula
keys and lists: Define the ranges for the KEY and LIST columns.
TEXTSPLIT(lists, ","): Splits each cell in the LIST column into an array of items based on the comma delimiter.
TOCOL(split_lists, 1): Flattens the resulting 2D array from TEXTSPLIT into a single column.
SEQUENCE and INDEX: Dynamically repeats the KEY values for each item in the flattened list.
HSTACK: Combines the repeated KEY values and the flattened list items into two columns.
Dynamic Output
This formula will automatically adjust to the size of your data range (A2:A100 and B2:B100 in this example). If your data grows or shrinks, the dynamic array will update accordingly.
Example Input and Output
Input Table:
| KEY | LIST |
|---|
| 1 | Apple,Banana |
| 2 | Carrot |
| 3 | Dog,Elephant |
Output Dynamic Array:
| KEY | ITEM |
|---|
| 1 | Apple |
| 1 | Banana |
| 2 | Carrot |
| 3 | Dog |
| 3 | Elephant |
Notes
- Ensure that the
LIST column does not contain extra spaces around the commas or items. If it does, you can clean the data using the TRIM function before applying TEXTSPLIT.
- If your data contains empty cells in the
LIST column, you may need to filter them out using FILTER to avoid errors.
Let me know if you need further clarification or adjustments!