Forum Discussion

Imrans123's avatar
Imrans123
Advocate V
4 years ago
Solved

Replacing Values based on WildCard

Hi,    Was wondering if anyone had idea about using wildcard entries to replace values in power Query.  I have a column with many rows, each row representing string of text. I want to replace all v...
  • smpa01's avatar
    4 years ago

    Imrans123  A complete dynamic solution in PQ is possible with JS and regex

     

    https://community.powerbi.com/t5/Community-Blog/Using-JavaScript-in-power-query-for-regex-Part2/ba-p/2132960

     

    https://community.powerbi.com/t5/Community-Blog/How-to-use-JavaScript-inside-power-query-for-data-extraction/ba-p/1632844

     

     

    let
    regex=let   
    fx=(input)=>
    Web.Page(
    "<script>
    var x='"&input&"'; 
    function dynamicReplace(a) {
        var b = a.match(/\d+th/gm); /*checks for a pattern-'th' preceded by digit in a string*/
        if (b == null) {            /*if no match returns the original string*/ 
            return a
        } else {
            return a.replace(/th/gm, '') /*if match replace only 'th' with a blank space*/
        }
    };
    document.write(dynamicReplace(x));
    </script>"){0}[Data]{0}[Children]{1}[Children]{0}[Text]
    
    in
    fx,
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyLslQCC4pSk0tUYrViVbySE3MKclQio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each regex([Name]))
    in
    #"Added Custom"