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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
anjbauer1993
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
Nolock
Resident Rockstar
Resident Rockstar

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
Nolock
Resident Rockstar
Resident Rockstar

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors