Forum Discussion
Replace Multiple Values (Character) Inside a Value (Column)
- 2 years ago
Hi oswin_aria ,
As always, in Power Query, there are many different ways to do the same thing. So, my way might not be the absolute best way but it works for me! 😁
I had this as my test data:
And I was able to convert it to this:
In one replace rather than three. Here is the M code that I used:
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
Replace123 = (text as text) as text => Text.Replace(Text.Replace(Text.Replace(text, "1", "x"), "2", "x"), "3", "x"),
#"Replaced Values" = Table.TransformColumns(#"Changed Type",{{"Column1", each Replace123(_), type text}})
in
#"Replaced Values" - Anonymous2 years ago
Hi oswin_aria ,
Provide another idea:let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQydnRyVorVATIdjZyMoUxDR0cjIycnY2NnoEAsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Coba = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Coba", type text}}), CharsToReplace = {"1", "2", "3"}, ReplaceWith = "X", ReplacedText = Table.TransformColumns(#"Changed Type",{{"Coba", each List.Accumulate(CharsToReplace, _, (state, current) => Text.Replace(state, current, ReplaceWith))}}) in ReplacedTextBest Regards,
Gao
Community Support TeamIf there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!How to get your questions answered quickly -- How to provide sample data in the Power BI Forum -- China Power BI User Group
- 2 years ago
Hi oswin_aria, another solution:
Result
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQydnRyVorVATIdjZyMoUxDR0cjIycnY2NnoEAsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Coba = _t]), AddedCustom = Table.AddColumn(Source, "Custom", each Text.Combine(List.ReplaceMatchingItems(Text.ToList([Coba]), {{"1", "X"}, {"2", "X"}, {"3", "X"}})), type text) in AddedCustom
Hi oswin_aria ,
Provide another idea:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQydnRyVorVATIdjZyMoUxDR0cjIycnY2NnoEAsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Coba = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Coba", type text}}),
CharsToReplace = {"1", "2", "3"},
ReplaceWith = "X",
ReplacedText = Table.TransformColumns(#"Changed Type",{{"Coba", each List.Accumulate(CharsToReplace, _, (state, current) => Text.Replace(state, current, ReplaceWith))}})
in
ReplacedText
Best Regards,
Gao
Community Support Team
If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!
How to get your questions answered quickly -- How to provide sample data in the Power BI Forum -- China Power BI User Group