Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
michellepace
Resolver III
Resolver III

type Text: Clean in-between white space

Hi. Is there a way to parse text to single white spaces only?

 

That is:

"mary    white     helloooo"

 

Becomes:

"mary white helloooo"

1 ACCEPTED SOLUTION
Anonymous
Not applicable

try this

 

 

let
    Source = Excel.Workbook(File.Contents("C:yourpath\Q.14 Data.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Single space everything, and trim both ends (don’t make a new column)", type text}}),
    tc=Table.TransformColumns( #"Changed Type", {"Single space everything, and trim both ends (don’t make a new column)", each Text.Combine(List.Select(Text.Split(_," "), each _<>""), " ")})
in
    tc

 

 

image.png

 

View solution in original post

10 REPLIES 10
Icey
Community Support
Community Support

Hi @michellepace ,

 

Could @Anonymous 's method meet your requirements?

 

If it could, please accept his reply as a solution so that people who may have the same question can get the solution directly.

 

If not, please let us know.

 

 

Best Regards,

Icey

ziying35
Impactful Individual
Impactful Individual

Hi, @michellepace 

Try this:

= let str="mary    white     helloooo" in Text.Combine(Splitter.SplitTextByWhitespace()(str), " ")

If my code solves your problem, mark it as a solution 

Anonymous
Not applicable

= Splitter.SplitTextByWhitespace()(" mary white helloooo ")

 

this function, that of which I now know the existence thanks to @ziying35 , for a string with leading or trailing whites spaces, gives this result

 

image.png

 

where the first and last element are the empty string "".

 

Why?

 

what kind of internal logic is applied in your opinion?

 

 

@Anonymous im not sure, but maybe Splitter.SplitTextByWhitespace() considers more spaces in a row ("      ") as a single "whitespace"... thats why there is only one empty element at the beginnning and end of the list:

whitespace.PNG

Anonymous
Not applicable

try to write, for example, in pseudo-code (simulating a low-level language) the various steps that produce that result.
Let's see where the different management of leading and trailing spaces intervenes with respect to those inside the text string.

 

 

 

PS

Un empty element "" is different from blank " "

ziying35
Impactful Individual
Impactful Individual

@Anonymous 

The function does have the problems you describe, and I'm not sure about its internal logic. The scenarios I've used it in before have been without leading and trailing spaces

 

= let str=" mary white helloooo " in Text. Trim(Text.Combine(Splitter.SplitTextByWhitespace()(str), " "))

Thank you all for your replies. In truth, I am struggling to use any of the solutions. Looking at my initial post, I did not explain my requirement sufficiently. If you look at the column name below, it is exactly what I need to do. Does anyone have the line of M-Code I can paste into the Advanced Editor to achieve this? I am happy for Format>Trim to be used as the first step if it simplifies the solution.

 

.pbix and data here: clickme

 

s.png

Many thanks once again.

Anonymous
Not applicable

try this

 

 

let
    Source = Excel.Workbook(File.Contents("C:yourpath\Q.14 Data.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Single space everything, and trim both ends (don’t make a new column)", type text}}),
    tc=Table.TransformColumns( #"Changed Type", {"Single space everything, and trim both ends (don’t make a new column)", each Text.Combine(List.Select(Text.Split(_," "), each _<>""), " ")})
in
    tc

 

 

image.png

 

Anonymous
Not applicable

 

 

Text.Combine(List.Select(Text.Split("mary    white     helloooo"," "), each _<>""), " ")

 

 

 

I do not intend to boast this solution, but just as a side observation, I point out that this cuts away the blanks that precede the first word or follow the last word.
This of course can be an advantage or a disadvantage, it depends on what one is looking for.

 

 

 

AlB
Super User
Super User

Hi @michellepace 

Paste this M code in a blank query to see the steps. The recursive function is the relevant one.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wyk0sqlQAgvKMzJJUEEMhIzUnJx8IlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    fx = (input as text) => let reduce = Text.Replace(input,"  "," "), res=if Text.Length(reduce)=Text.Length(input) then input else @fx(reduce) in res,  
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each fx([Column1]))
in
    #"Added Custom"

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors