How to split delimited into rows.
I tried using Power Query.
With Power Query I can do it only for 1 column.
If I select more than 1 column, the Split Column is disabled in the Power Query Editor.
Is there any other solution for splitting delimited into rows for more than one column?
This video offers three solutions.
Solution 1 is a formula such as =TEXTJOIN("|",,TEXTSPLIT(C2,",")&"-"&TEXTSPLIT(B2,",")) and then Power Query.
The 2nd solution is M code written by Suat Ozgur.
The 3rd solution is two intermediate queries in Power Query and then a merge query.
Table of Contents
(0:00) How to Split to Rows
(0:11) TEXTSPLIT formula
(1:30) M solution
(2:44) 3-Query solution
I tried using Power Query.
With Power Query I can do it only for 1 column.
If I select more than 1 column, the Split Column is disabled in the Power Query Editor.
Is there any other solution for splitting delimited into rows for more than one column?
This video offers three solutions.
Solution 1 is a formula such as =TEXTJOIN("|",,TEXTSPLIT(C2,",")&"-"&TEXTSPLIT(B2,",")) and then Power Query.
The 2nd solution is M code written by Suat Ozgur.
Power Query:
let
fnSplit = (row as record) =>
let
Invoice = row[Invoice],
Product = row[Products],
Qty = row[Quantities],
ProductList = Text.Split(Product, "," ),
QtyList = Text.Split(Qty, ","),
TotalProducts = List.Count(ProductList),
Result = List.Generate(
() => [i = 0],
each [i] < TotalProducts,
each [i = [i] + 1],
each [
Invoice = Invoice,
Product = ProductList{[i]},
Quantity = QtyList{[i]}
]
)
in
Result,
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
TotalRows = Table.RowCount(Source),
SplitRows = List.Generate(
() => [i = 0],
each [i] < TotalRows,
each [i = [i] + 1],
each fnSplit(Source{[i]})
),
ListToTable = Table.FromList(SplitRows, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
ExpandTable = Table.ExpandListColumn(ListToTable, "Column1"),
Result = Table.ExpandRecordColumn(ExpandTable, "Column1", {"Invoice", "Product", "Quantity"})
in
Result
The 3rd solution is two intermediate queries in Power Query and then a merge query.
Table of Contents
(0:00) How to Split to Rows
(0:11) TEXTSPLIT formula
(1:30) M solution
(2:44) 3-Query solution
Transcript of the video:
Power Query: the Split to Rows is amazing.
But what if we have to do that for two columns? If you try and select two columns and then split, it gets disabled. So here's what we have.
For each invoice, we have a list of products - In this case, there's six.
And a list of quantities. We want to figure out that there's three of T, 18 of B and so on. Now, today in Office Insiders, Microsoft 365, we have this great new function where we can split C by the commas.
Ampersand with a dash. Split B by the commas.
And then join that whole thing with a Pipe character in between.
And we would get this. That's just beautiful.
Ctrl+T to make that all into a table, and we're home free.
Data, from Table or Range. We're going to take this and split it at the Pipe.
Split column by delimiter. Advanced, into rows.
Click okay. Beautiful.
We don't need this, we don't need this. Then here, this is just a straight split column by delimiter at the dash.
Okay, done. Gorgeous.
Close & Load. Let's assume that you're not in Office Insiders and you don't yet have TEXTSPLIT. Look, while I could solve this in the Power Query editor, at this point, I just know that there has to be a better way to write this code.
I turned to Suat Ozgur, author of the soon-to-be upcoming "You Wouldn't Write Professional VBA Using the Macro Recorder, Would You - A Programmer's Guide to M".
And we'll put a link down to this code down in the YouTube description.
Here, this data – we will convert to table. Ctrl+T, click okay.
I'm going to call it Table1. That's perfect because this code is looking for Table1. Data, Get Data, From Other Sources.
Blank Query. View, Advanced editor.
Get rid of what's there. And we'll paste.
And click Done. And Home, Close & Load. That's the way to do it if you're a hot-shot M programmer, but unfortunately I'm not. We're going to turn to my way to do it using the Power Query editor. Which is a horrible set of steps. We're making this into a Table, click okay.
We'll call it OrigData. Data, From Table or Range.
You see all those commas out there so we have to get rid of the Change Type.
This first query is going to get invoice and products, but not quantities.
We take the Products - Split Column, By Delimiter. Split into Rows, click Okay.
We don't need quantities anymore. And on the Add Column, we will add an Index Column, From Zero. This is going to just be called the products. Home. Close & Load, Close & Load To, Only Create a Connection. Then we come back to the exact same table.
From Table or Range. This time we can get rid of the products. Get rid of the Change Type, that way our commas come back here. Split Column, By Delimiter, Comma, into Rows.
Click Okay, good. Add Column, add an Index Column, From Zero.
Make sure that you choose the same index both times.
These will be called the quantity. Home, Close & Load, Close & Load to, Only Create a Connection. Then just out here to the side somewhere, Get data, Combine Queries, Merge. We'll take the Products, the Quantity, Index, Index. Click Okay.
Expand this so we can get the Quantity out. Click Okay.
We don't need the second index. Invoice, Products, Quantities.
Home, Close & Load. There you go.
All right. Hey, I want to thank you for stopping by. We'll see you next time for another netcast from MrExcel.
But what if we have to do that for two columns? If you try and select two columns and then split, it gets disabled. So here's what we have.
For each invoice, we have a list of products - In this case, there's six.
And a list of quantities. We want to figure out that there's three of T, 18 of B and so on. Now, today in Office Insiders, Microsoft 365, we have this great new function where we can split C by the commas.
Ampersand with a dash. Split B by the commas.
And then join that whole thing with a Pipe character in between.
And we would get this. That's just beautiful.
Ctrl+T to make that all into a table, and we're home free.
Data, from Table or Range. We're going to take this and split it at the Pipe.
Split column by delimiter. Advanced, into rows.
Click okay. Beautiful.
We don't need this, we don't need this. Then here, this is just a straight split column by delimiter at the dash.
Okay, done. Gorgeous.
Close & Load. Let's assume that you're not in Office Insiders and you don't yet have TEXTSPLIT. Look, while I could solve this in the Power Query editor, at this point, I just know that there has to be a better way to write this code.
I turned to Suat Ozgur, author of the soon-to-be upcoming "You Wouldn't Write Professional VBA Using the Macro Recorder, Would You - A Programmer's Guide to M".
And we'll put a link down to this code down in the YouTube description.
Here, this data – we will convert to table. Ctrl+T, click okay.
I'm going to call it Table1. That's perfect because this code is looking for Table1. Data, Get Data, From Other Sources.
Blank Query. View, Advanced editor.
Get rid of what's there. And we'll paste.
And click Done. And Home, Close & Load. That's the way to do it if you're a hot-shot M programmer, but unfortunately I'm not. We're going to turn to my way to do it using the Power Query editor. Which is a horrible set of steps. We're making this into a Table, click okay.
We'll call it OrigData. Data, From Table or Range.
You see all those commas out there so we have to get rid of the Change Type.
This first query is going to get invoice and products, but not quantities.
We take the Products - Split Column, By Delimiter. Split into Rows, click Okay.
We don't need quantities anymore. And on the Add Column, we will add an Index Column, From Zero. This is going to just be called the products. Home. Close & Load, Close & Load To, Only Create a Connection. Then we come back to the exact same table.
From Table or Range. This time we can get rid of the products. Get rid of the Change Type, that way our commas come back here. Split Column, By Delimiter, Comma, into Rows.
Click Okay, good. Add Column, add an Index Column, From Zero.
Make sure that you choose the same index both times.
These will be called the quantity. Home, Close & Load, Close & Load to, Only Create a Connection. Then just out here to the side somewhere, Get data, Combine Queries, Merge. We'll take the Products, the Quantity, Index, Index. Click Okay.
Expand this so we can get the Quantity out. Click Okay.
We don't need the second index. Invoice, Products, Quantities.
Home, Close & Load. There you go.
All right. Hey, I want to thank you for stopping by. We'll see you next time for another netcast from MrExcel.