Often times, when you get data from a source, there will most probably be something incorrect that needs to be fixed using an ETL process of Power Query. And most of those transformations would require either Replacing or Removing string's that are not necessary.
For example, if you extract any financial data from a source which has multiple currency symbols, there's a chance that the numbers get extracted along with the currency symbols like "$700"/"€700" instead of 700. And these type of cases makes it tough to Transform the Data to our wish as the value will be considered as a text instead of a number.
In those type of cases, we would most likely have to create multiple steps to remove the multiple currency symbols from the Column. But thanks to the solution provided by Marcel for one of the questions posted on Stack Overflow, with a little manipulation of the Query language (M), we can replace/Remove multiple strings in one step using the List. Accumulate function.
Remove Multiple Strings:
Below is a sample code that you can use to remove all the strings/text fields highlighted in blue from any column.
= Table.TransformColumns(#"Previous Step",{{"Column Header", each List.Accumulate(Text.ToList("⬣£€$"),_,(String,Remove) => Text.Replace(String,Remove,""))}})
You can copy-paste the above code in your query and replace the highlighted text to match your query.
Replace Multiple Strings:
Below is a sample code that you can use to replace all the strings/text fields highlighted in blue from any column to the text highlighted in Red.
You can copy-paste the above code in your query and replace the highlighted text to match your query.
P.S:
- You can check out the following video from Miguel on how to create a custom function that does the same thing. Although, the Syntax that he uses is for Advanced users.
- Also, you can also have a look at the following video by Marcel where he explains the List.Accumulate function.
Comments
Post a Comment