Power Query: Cleaning Repeating Delimiters

lneidorf

Board Regular
Joined
May 20, 2004
Messages
97
Office Version
  1. 365
Platform
  1. Windows
Hi there.

I've been using PowerQuery to collapse a flat file into combined, multi-value cells with delimiters. The inconsistent nature of the raw data results in repeating delimiters / spaces, like so:

; ; Smith, Judy; Carson, Kevin; ; Levy, Kevin; Bloom, Judy; ; ; ; Nicholas; ; ;

I'd like to add some code to strip out the excess delimiters (and any leading delimiters) so that the result is:
Smith, Judy; Carson, Kevin; Levy, Kevin; Bloom, Judy; Nicholas

A bonus would be if I could sort the entries between delimiters, like so:
Bloom, Judy; Carson, Kevin; Levy, Kevin; Nicholas; Smith, Judy

Many thanks.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Something like this:

Power Query:
let
    Source = "; ; Smith, Judy; Carson, Kevin; ; Levy, Kevin; Bloom, Judy; ; ; ; Nicholas; ; ;",
    SplitText = Text.Split(Source, ";"),
    RemoveEmpty = List.RemoveItems(SplitText, {" ", ""}),
    SortList = List.Sort(RemoveEmpty,Order.Ascending),
    JoinList = Text.Combine(SortList, ";")
in
    JoinList

Result:
1685237889327.png


You will probably need to do this in a table, so here is the function version that you can add a column by using it. I am just assuming since I don't know the exact data and current query structure. Let's say you have the table as Source instead of a simple text value, and Column1 is the column name that contains many of these values, then you can use the following to add a new column for each row in your table.

Power Query:
let
    fnCleanText = (txt as text) => 
        let
            SplitText = Text.Split(txt, ";"),
            RemoveEmpty = List.RemoveItems(SplitText, {" ", ""}),
            SortList = List.Sort(RemoveEmpty,Order.Ascending),
            JoinList = Text.Combine(SortList, ";")
        in
            JoinList,

    Source = #table(1, {{"; ; Smith, Judy; Carson, Kevin; ; Levy, Kevin; Bloom, Judy; ; ; ; Nicholas; ; ;"}}),
    Result = Table.AddColumn(Source, "Cleaned", each fnCleanText(_[Column1]) )
in
    Result
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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