Power BI: find and replace words

The easy way will be to used the “replace values” function in the Power Query Editor but based on my data, I may not be able to use it so I will have to create a DAX formula. Let me show you the query editor first.

Click on “home -> transform data -> transform data”

power bi

Click on “transform -> replace values -> replace values”

power bi

A popup window will appear and you just have to fill the fields:

power bi

Now let´s do the DAX one. I will use 2 table examples, the first one always contains the word I am looking for. I will create 2 calculated columns by clicking on “new column” and put this formula:

SUBSTITUTE('table'[argument],"find value","new value")

NOTE:

  • Change “table”, “argument”, “find value” and “new value” by yours
  • For multiple values, change 'table'[argument] by the formula. For instance:
    SUBSTITUTE(SUBSTITUTE('table'[argument],"find value2","new value2"),"find value1","new value1")
    power bi
  • SUBSTITUTE is case sensitive so make sure “find value” is written as it must be
power bi

For case-insensitive (no matter how “find value” is written), I will use this formula:

REPLACE('table'[argument],SEARCH("find value",'table'[argument]),number,"new value")

NOTE: change “number” by the number of characters of “find value”. In my example, I am asking to find “inc003” which has 6 characters

power bi

Now, if in my table, some cells have not the value I am looking for, I will get an error as you can see in the picture below. To resolve it, I will use this formula:

IF(ISERROR(SEARCH("find value",'table'[argument])),'table'[argument],REPLACE('table'[argument],SEARCH("find value",'table'[argument]),number,"new value"))

power bi

For multiple values, I will use variables like that:


var frw1=IF(ISERROR(SEARCH("find value1",'table'[argument])),'table'[argument],REPLACE('table'[argument],SEARCH("find value1",'table'[argument]),number,"new value1"))
Return
IF(ISERROR(SEARCH("find value2",frw1)),frw1,REPLACE(frw1,SEARCH("find value2",frw1),number,"new value2"))             
              
power bi

Interesting Topics