Forum Discussion

sonnh's avatar
sonnh
Frequent Visitor
5 years ago
Solved

Power query - Replace multiple words in column using other table.

Hello all, I have a raw data needed to be procesed by replace multiple words by this table. OriginReplace ,District District ,district District Dist District I found solution here ...
  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi sonnh 

     

    I have adapted one custom function applied to different cases, you can have a look and modify accordingly, credit to original author:

    https://www.howtoexcel.org/power-query/bulk-replace-values/

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W0lFwySwuKcpMLlEwMFSK1QGKpMAFDIzAIiAVCoYGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Origin = _t]),
        BulkReplace = (DataTable as table, FindReplaceList as list, DataTableColumn as list) =>
        let   
            Counter = List.Count(FindReplaceList),
            BulkReplaceValues = (DataTableTemp, n) => 
            let 
                ReplaceTable = Table.ReplaceValue(
                    DataTableTemp,
                    FindReplaceList{n},
                    "District",
                    Replacer.ReplaceText,
                    DataTableColumn
                    )
            in
    
                if n = Counter - 1 
                    then ReplaceTable
                    else @BulkReplaceValues(ReplaceTable, n + 1),
            Output = BulkReplaceValues(DataTable, 0)   
        in
            Output,
    
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Origin", type text}}),
        Custom1 = BulkReplace( #"Changed Type",{", District",",district","Dist"},{"Origin"})
    in
        Custom1