cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
KilleenJeffrey
Frequent Visitor

M Language Remove Duplicate or Repeating Spaces or Characters

I couldn't find a example for how to do this (Removing Repeating Spaces) so I am posting this.  It would work for any repeating Characters beyond spaces.

 

let
    Source = "abc   ABC       DEF ABC",
    StringToCleanOfExtraSpaces = Source,
    SourceSingleSpaced = Text.Combine(
                                List.RemoveNulls(
                                    List.Transform(
                                        List.Positions(
                                            Text.ToList(
                                                StringToCleanOfExtraSpaces)), each 
                                                                                if _ = 0 then Text.Range(StringToCleanOfExtraSpaces, _ , 1) 
                                                                                else if Text.Range(StringToCleanOfExtraSpaces, _ -1,1)=" " and Text.Range(StringToCleanOfExtraSpaces, _ ,1)=" " then null 
                                                                                else Text.Range(StringToCleanOfExtraSpaces, _, 1))))
in
    SourceSingleSpaced

Screenshot 2022-09-07 152715.jpg

2 ACCEPTED SOLUTIONS
jbwtp
Memorable Member
Memorable Member

Hi @KilleenJeffrey,

 

There is no stock function to remove repeating spaces. If you only concerned about spaces, the function can be shorter, but not much:

let
    Source = "abc   ABC       DEF ABC",
    StringToCleanOfExtraSpaces = Source,
    SourceSingleSpaced = Text.Combine(
                            List.Accumulate(
                                    Text.ToList(StringToCleanOfExtraSpaces), {}, (a, n)=> if n = " " and List.Last(a) = " " then a else a & {n}))
in
    SourceSingleSpaced

Kind regards,

John

View solution in original post

Here are a couple of shorter options.

let
    StringToClean = "  abc   ABC       DEF ABC ",
    SplitText = Text.Split(StringToClean, " "),
    RemoveBlanks = List.Select(SplitText, each _ <> ""),
    Result = Text.Combine(RemoveBlanks, " ")
in
    Result

This is a slightly simplified version of this post (only works for spaces).

 

let
    StringToClean = "  abc   ABC       DEF ABC ",
    Result = Text.Combine(Splitter.SplitTextByWhitespace()(Text.Trim(StringToClean)), " ")
in
    Result

Credit to Frank Tonsen's comment on this post.

View solution in original post

3 REPLIES 3
jbwtp
Memorable Member
Memorable Member

Hi @KilleenJeffrey,

 

There is no stock function to remove repeating spaces. If you only concerned about spaces, the function can be shorter, but not much:

let
    Source = "abc   ABC       DEF ABC",
    StringToCleanOfExtraSpaces = Source,
    SourceSingleSpaced = Text.Combine(
                            List.Accumulate(
                                    Text.ToList(StringToCleanOfExtraSpaces), {}, (a, n)=> if n = " " and List.Last(a) = " " then a else a & {n}))
in
    SourceSingleSpaced

Kind regards,

John

Here are a couple of shorter options.

let
    StringToClean = "  abc   ABC       DEF ABC ",
    SplitText = Text.Split(StringToClean, " "),
    RemoveBlanks = List.Select(SplitText, each _ <> ""),
    Result = Text.Combine(RemoveBlanks, " ")
in
    Result

This is a slightly simplified version of this post (only works for spaces).

 

let
    StringToClean = "  abc   ABC       DEF ABC ",
    Result = Text.Combine(Splitter.SplitTextByWhitespace()(Text.Trim(StringToClean)), " ")
in
    Result

Credit to Frank Tonsen's comment on this post.

For spaces that last example is slick

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors