cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
anjbauer1993 Frequent Visitor
Frequent Visitor

Replace every third occurence of a character

I have a string that I would would like to replace every third " " with "}, {". Below is an example of what I have currently and what I'm looking for.

 

current:

{24 1 0 188 1 0 190 1 0 192 1 0}

{724 1 0 259 1 0 430 1 0 376 1 0 228 1 0 194 1 0}

 

desired result:

{24 1 0}, {188 1 0}, {190 1 0}, {192 1 0}
{724 1 0}, {259 1 0}, {430 1 0}, {376 1 0}, {228 1 0}, {194 1 0}

 

Every string is made up of groups of three integers, but what's making this difficult is the fact that not every row has the same number of these groups. In another project I created a loop using the method described in this link, which I'm sure I could implement here but I have to believe that there is a simpler way of accomplishing this. Any help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Replace every third occurence of a character

Hi @anjbauer1993,

I have a solution for you.

Split every text into a list by space, generate triples of the list, and combine these triples in a string again. The solution also contains your sample data.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WqjYyUTBUMFAwtLCA0JYGUNoIRNcqxeoAFZlDVRmZWoJpE2OIKmNzM4i4EUy3CVRXLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    Triples = Table.AddColumn(
        Source, 
        "Triples", 
        (_) =>
            let
                // split text by space
                CurList = Text.Split([Column1], " "),
                // get count of values
                CurListLength = List.Count(CurList),
                // go through the list and group triples together
                Triples = List.Generate(
                    () => 0,
                    each _ < CurListLength,
                    each _ + 3,
                    each Text.Combine(List.Range(CurList, _, 3), " ")
                ),
                // combine triples
                CombineToText = Text.Combine(Triples, "}, {")
            in CombineToText
    )
in
    Triples

And a screenshot of the result.

Capture.PNG

View solution in original post

1 REPLY 1
Super User
Super User

Re: Replace every third occurence of a character

Hi @anjbauer1993,

I have a solution for you.

Split every text into a list by space, generate triples of the list, and combine these triples in a string again. The solution also contains your sample data.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WqjYyUTBUMFAwtLCA0JYGUNoIRNcqxeoAFZlDVRmZWoJpE2OIKmNzM4i4EUy3CVRXLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    Triples = Table.AddColumn(
        Source, 
        "Triples", 
        (_) =>
            let
                // split text by space
                CurList = Text.Split([Column1], " "),
                // get count of values
                CurListLength = List.Count(CurList),
                // go through the list and group triples together
                Triples = List.Generate(
                    () => 0,
                    each _ < CurListLength,
                    each _ + 3,
                    each Text.Combine(List.Range(CurList, _, 3), " ")
                ),
                // combine triples
                CombineToText = Text.Combine(Triples, "}, {")
            in CombineToText
    )
in
    Triples

And a screenshot of the result.

Capture.PNG

View solution in original post

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Users Online
Currently online: 228 members 2,327 guests
Please welcome our newest community members: