Power Query - split text to variable number of columns

pepe74287

New Member
Joined
Feb 4, 2015
Messages
43
Hello,

I would need to split text by number of characters (in my case 1) into variable number of columns. For instance, text "adg" should be splitted into 3 columns (a d g), "adge" into 4 columns (a d g e)and so on. If I use native power Query Split Column by Number of Characters, PQ creates only 3 columns for "adg" and if I change that string to "adge", it still splits into 3 columns. I know I could set Custom Number of columns to split into, but I would like PQ to split the string into dynamic number of columns. Additionaly, I'm also looking for the opposite - merge all columns (no matter how many columns there are) into 1.

Thanks
Pepe
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi
You can do that dynamically, for example
Code:
let
    source = Table.FromColumns({ {"abc", "abcde"} }, {"text"}),
    added = Table.AddColumn(source,"list", each Table.FromRows({Text.ToList([text])}),type table),
    length = Table.AddColumn(added,"len", each Table.ColumnCount(
[list]),type number),
    maxCols = List.Max(length[len]),
    colNames = Table.ColumnNames(Table.SelectRows(length,each [len]=maxCols)
[list]{0}),
    return = Table.ExpandTableColumn(added,"list",colNames)
in
    return
Regards,
 
Upvote 0
Or directly from UI
Code:
let
    source = Table.FromColumns({ {"abc", "abcde"} }, {"text"}),
    AddIndex = Table.AddIndexColumn(source, "Indeks", 0, 1),
    LengthOfText = Table.AddColumn(AddIndex, "Length", each Text.Length([text]), type number),
    SortByLengthDescending = Table.Sort(LengthOfText,{{"Length", Order.Descending}}),
    SplitColumn = Table.SplitColumn(SortByLengthDescending,"text",Splitter.SplitTextByRepeatedLengths(1),{"text.1", "text.2", "text.3", "text.4", "text.5"}),
    SortByIndexAscending = Table.Sort(SplitColumn,{{"Indeks", Order.Ascending}}),
    RemoveColumns = Table.RemoveColumns(SortByIndexAscending,{"Indeks", "Length"})
in
    RemoveColumns

Regards :-)
 
Upvote 0
ooopsss..there is a mistake, so not only from UI.
The new code should be like this below
Code:
let
    source = Table.FromColumns({ {"abc", "abcde"} }, {"text"}),
    AddIndex = Table.AddIndexColumn(source, "Indeks", 0, 1),
    LengthOfText = Table.AddColumn(AddIndex, "Length", each Text.Length([text]), type number),
    SortByLengthDescending = Table.Sort(LengthOfText,{{"Length", Order.Descending}}),
    ListOfColumns = List.Transform({1..SortByLengthDescending{0}[Length]}, each Text.From(_)),
    SplitColumn = Table.SplitColumn(SortByLengthDescending,"text",Splitter.SplitTextByRepeatedLengths(1), ListOfColumns),
    SortByIndexAscending = Table.Sort(SplitColumn,{{"Indeks", Order.Ascending}}),
    RemoveColumns = Table.RemoveColumns(SortByIndexAscending,{"Indeks", "Length"})
in
    RemoveColumns
Only "ListOfColumns" step was manualy written

Regards
 
Upvote 0
@Pepe74287 Thanks for feedback.
@anvg (Andrey) There was only an example for not to much advanced users :-) Of course it is not better than good manualy written code :-)
Regards
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,163
Messages
6,176,812
Members
452,744
Latest member
Alleo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top