Forum Discussion

Vampirtc's avatar
Vampirtc
Frequent Visitor
5 years ago
Solved

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.

  • 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

     

    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

    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

     

1 Reply

  • Jimmy801's avatar
    Jimmy801
    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

     

    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

    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