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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.