Forum Discussion

prankin's avatar
prankin
Frequent Visitor
5 years ago
Solved

Find and Replace text string in one table using value from another table

Hi I am trying to create a query using M language that allows me to correct text in a certain column in a certain table using data from two columns in another table. I have a table of bank transact...
  • CNENFRNL's avatar
    CNENFRNL
    5 years ago

    Hi, prankin , why bother to use such an intimidating chunk of codes given that there already exists a native function List.Accumulate? You might want to try such a solution,

    let
    
        BulkReplaceFunction = (DataTable as table, FindReplaceTable as table, DataTableColumn as list) =>
        let
            BufferedFRT = List.Buffer(Table.ToRecords(FindReplaceTable)),
            Substitution = List.Accumulate(//outer List.Accumulate for iteration
                DataTableColumn,
                Table.Buffer(DataTable),
                (s, c) =>
                    Table.TransformColumns(
                        s, {{c, each List.Accumulate(//core function, inner List.Accumulate to find & replace
                                BufferedFRT, _, (x, y) => Text.Replace(x, y[Find], y[Replace])
                                )}}
                    )
            )
        in
            Substitution,
    
        Replacements = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZGxDoIwEIZfpeJGcGhwcWxrSRtDSSvSGMLgQKKLC2H3WXw0n8S7Ahowcbn+33eXa5rWdRQznkdJhLVJAIUDghJAcgAoATJxBsIaUJtSOs7MAeQ3YyuZr3w9nkQ6LZhBmY9x7Cwnj7BIMLcHm7MPYJOS/21TVR409d4Hkfbd7d52HTg+RfSCKGLX+EgFRzB0U9vtzOwWE0quyEkGJS2EIK/tpW8XTsniZ07ZeFg24fwuTVOBQnMxiMKQUruwZT9G9JLAe/E/SJnB/zRv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Find = _t, Replace = _t]),
    
        BankRecords = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZfLbuM2FIZf5TTpog0kmodXcSnL9FhTS05kx5k0mEUKGBhggBS9AV3OoxR9lD7KPMmQEikzmcyCBQz70LL9+T+Xn9TDwwVeFBdvH59OsPr15EJkXJRSKl2Vxhj3RvPhN/e8fHz6CP6jfIFiwSj18SUzFO7avrfD3i2V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o8tuB0dGvrz379UVn49dIiGVKF2Ujlrcfc5778A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, #"Account Name" = _t, #"Account Number" = _t, #"Account Type" = _t, #"Branch Reference" = _t, Date = _t, Description = _t, Debit = _t, Credit = _t, Value = _t, Balance = _t]),
    
        Invoking = BulkReplaceFunction(BankRecords, Replacements, {"Description"})
    in
        Invoking

     

    As you can see, the very essential part of the definition of the function

    BulkReplaceFunction = (DataTable as table, FindReplaceTable as table, DataTableColumn as list) =>
    let
        BufferedFRT = List.Buffer(Table.ToRecords(FindReplaceTable)),
        Substitution = List.Accumulate(//outer List.Accumulate for iteration
            DataTableColumn,
            Table.Buffer(DataTable),
            (s, c) =>
                Table.TransformColumns(
                    s, {{c, each List.Accumulate(//core function, inner List.Accumulate to find & replace
                            BufferedFRT, _, (x, y) => Text.Replace(x, y[Find], y[Replace])
                            )}}
                )
        )
    in
        Substitution,