Power Query Replacing more than one comma

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
757
Office Version
2016
Platform
Windows
Hi All,

I have a file with varying amounts of words and commas in each row. To tidy things up and place one comma in front of every word (later to be split by delimiter). I am replacing every pair of commas with a single commas until the job is done. My problem is that it takes 20 lines as per below to get the job done. Is there a simpler method like a ",*". Obviously this won't work and I'm not up to speed with M code yet. Thanks for any pointers.

#"Replaced Value7" = Table.ReplaceValue(#"Replaced Value6",",,,,",",",Replacer.ReplaceText,{"Column1"}),
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,724
Office Version
365
Platform
Windows
Can you post some sample data?
 

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
757
Office Version
2016
Platform
Windows
I can't post a file as it's sensitive stuff, but here is an example of what the data looks like. Hope this is enough to get idea of the problem. Thanks.
Run,Date,:,,24/07/2020,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Page:,,,,,,1,
,,,,,,,,,,,,,,,,,,,,,,,,WORK,CATEGORY,COUNTS,AND,PERFORMANCE,INDICATORS,FOR,PERIOD,18/07/2020,TO,24/07/2020,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,MANAGEMENT,UNIT,NUMBER,01226,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,WORK,CATEGORY,,,,,,,,,,,,,,,BROUGHT,,,,REGISTERED,,,TRANSFER,,,,,TRANSFER,,WITHDRAWN,,,CLEARED,,,,OUTSTANDING,,,,,,,PERFORMANCE,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,FORWARD,,,,,,,,,,,,,,,,,,,,IN,,,,,,,,,,,OUT,,,,/DEFECTIVE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,INDICATORS,,,,,
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,724
Office Version
365
Platform
Windows
You could try creating a custom function to replace multiple delimiters with one.

To do that in Power Query:

1 Right click inside the Query panel on the left and select New Query>Other Sources>Blank.

2 Rename the newly created query with a suitable name, e.g. ReplaceMultipleDelimiters.

3 Right click the query and selected Advanced Editor.

4 Replace what you see in the editor with the code below.

Code:
(String as text, optional Delimiter as text) as text =>

let
    delimiter = if Delimiter = null then " " else Delimiter,
    TextToList = List.Buffer(Text.Split(String, delimiter)),
    FilterList = List.Select(TextToList, each _ <> ""),
    Result = Text.Combine(FilterList, delimiter)
in
    Result
5 Click Done.

Now you have the function you can implement it by going to Add Column>Invoke Custom Function.

When you do that give the new column an appropriate name, select the column you want to apply it to and set the delimiter to whatever you want.
 

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
757
Office Version
2016
Platform
Windows
That's a great solution, unfortunately I'm a little lost. The column I am using in my query is called 'Column1'. How would I add this to my applied steps. As you can guess I am very new to this.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,724
Office Version
365
Platform
Windows
Goto the Add Column tab in Power Query and select Invoke Custom Function.

You should now see a dialog where you can set the New Column Name and select the Function to apply, e.g. ReplaceMultipleDelimiters.

Two new fields should now appear, in the first one you can select the column to apply the function to, e.g. Column1, and in the second set the delimiter.

1596540600627.png
 

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
757
Office Version
2016
Platform
Windows
No Invoke.JPG


This is the problem I have. The Invoke Custom function is missing form my version of Power query. I suspect we have a basic version is there is such a thing
 

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
757
Office Version
2016
Platform
Windows
I think I will have to contact the 'I.T' department and see why it isn't there. Thanks for all the help, very much appreciate you helping me.
 

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
6,275
I think you should update your profile (Account Details) about Excel version, less troubles and wasted posts

anyway try in PQ Editor: Alt A CF
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,105,930
Messages
5,508,171
Members
408,669
Latest member
AgsikapAko

This Week's Hot Topics

Top