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

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

Reply
Vampirtc
Frequent Visitor

Is there a nicer way to write this code (Text.Replace)

= Table.AddColumn(Category, "PictureURL", each Text.Replace(Text.Select(Text.Replace(Text.Replace(Text.Replace(Text.Replace(Text.Lower(Text.Replace([Title]," ","-")),"--","-"),"š","s"),"č","c"),"ž","z"),{"A".."z","-","0".."9"}),"--","-")&"-"&Text.Lower([Category]))

Second problem: I want to replace all -- with --, all --- with -, etc so I had to inject -- to - replace two times.

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @Vampirtc 

 

here a way to replace dynamically. Create a table with two column from and to

Maintain all your values there to be changed

Jimmy801_0-1616401207242.png

 

Add a new column to your data where you are using List.Zip to combine this table and use List.Accumulate to apply all your changes. What I didn't do is to apply the Text.Select. However this table above you have to put also in the right order to solve you problem about the --, and - stuff.

Here the complete code

let
    ChangeTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WOrxCSUcpVSlWJ1pJVxfI1NXVAXMObwNy8oESsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [From = _t, to = _t]),
    YourTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WOryiOCUtJ+vwNl1dpVidaKWKCggzFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [YourText = _t]),
    #"Added Custom" = Table.AddColumn(YourTable, "Custom", each let ChangeList = List.Zip({ChangeTable[From], ChangeTable[to]}),
ReplaceText = List.Accumulate(ChangeList, [YourText], (text,change)=> Text.Replace(text, change{0}, change{1}))
in 
ReplaceText)
in
    #"Added Custom"

and this is the output

Jimmy801_1-1616401389794.png

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

View solution in original post

1 REPLY 1
Jimmy801
Community Champion
Community Champion

Hello @Vampirtc 

 

here a way to replace dynamically. Create a table with two column from and to

Maintain all your values there to be changed

Jimmy801_0-1616401207242.png

 

Add a new column to your data where you are using List.Zip to combine this table and use List.Accumulate to apply all your changes. What I didn't do is to apply the Text.Select. However this table above you have to put also in the right order to solve you problem about the --, and - stuff.

Here the complete code

let
    ChangeTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WOrxCSUcpVSlWJ1pJVxfI1NXVAXMObwNy8oESsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [From = _t, to = _t]),
    YourTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WOryiOCUtJ+vwNl1dpVidaKWKCggzFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [YourText = _t]),
    #"Added Custom" = Table.AddColumn(YourTable, "Custom", each let ChangeList = List.Zip({ChangeTable[From], ChangeTable[to]}),
ReplaceText = List.Accumulate(ChangeList, [YourText], (text,change)=> Text.Replace(text, change{0}, change{1}))
in 
ReplaceText)
in
    #"Added Custom"

and this is the output

Jimmy801_1-1616401389794.png

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

Helpful resources

Announcements
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.