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”
Click on “transform -> replace values -> replace values”
A popup window will appear and you just have to fill the fields:
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")
- SUBSTITUTE is case sensitive so make sure “find value” is written as it must be
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
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"))
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"))
Interesting Topics
-
Be successfully certified ITIL 4 Managing Professional
Study, study and study, I couldn’t be successfully certified without studying it, if you are interested...
-
Be successfully certified ITIL 4 Strategic Leader
With my ITIL 4 Managing Professional certification (ITIL MP) in the pocket, it was time to go for the...
-
Hide visual and change background color based on selection
Some small tricks to customize the background colour of a text box...
-
Stacked and clustered column chart or double stacked column chart
In excel, I use a lot the combination of clustered and stacked chart...