Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
replace all the special character in a column containing sentences with white space
input
hi.you/love
output
hi you love
Solved! Go to Solution.
You need to have an approach where you need to replace all other characters other than allowed. In below approach, a to z and 0 to 9 are allowed. Hence, it will replace any other character encountered
{"A".."Z","a".."z","0".."9"} - If you feel 0 to 9 not allowed, make it {"A".."Z","a".."z"}
So basically you are taking Whitelist approach rather than Blacklist approach as creating Blacklist is a big task and also prone to error.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8sjU89TPyS9Lja/ML1WK1YlW8q10yEvMTVXNLAZzwzNSi1JjSg0MjMwSi1K1gaoU0/Mz89LBko4pKUWpxcUKmcUKxiY6Cs75OTmZeUB9sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
BuffListAllowedChars = List.Buffer({"A".."Z","a".."z","0".."9"}),
SourceList = List.Buffer(Source[Column1]),
ListCount = List.Count(SourceList),
GenList = List.Generate(()=>[x=Text.Combine(List.Transform(Text.ToList(SourceList{0}),(a)=>if List.Contains(BuffListAllowedChars,a) then a else " ")),i=0], each [i]<ListCount, each [i=[i]+1, x=Text.Combine(List.Transform(Text.ToList(SourceList{i}),(a)=>if List.Contains(BuffListAllowedChars,a) then a else " "))], each [x]),
Result = Table.FromColumns({GenList},{"Result"})
in
Result
You need to have an approach where you need to replace all other characters other than allowed. In below approach, a to z and 0 to 9 are allowed. Hence, it will replace any other character encountered
{"A".."Z","a".."z","0".."9"} - If you feel 0 to 9 not allowed, make it {"A".."Z","a".."z"}
So basically you are taking Whitelist approach rather than Blacklist approach as creating Blacklist is a big task and also prone to error.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8sjU89TPyS9Lja/ML1WK1YlW8q10yEvMTVXNLAZzwzNSi1JjSg0MjMwSi1K1gaoU0/Mz89LBko4pKUWpxcUKmcUKxiY6Cs75OTmZeUB9sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
BuffListAllowedChars = List.Buffer({"A".."Z","a".."z","0".."9"}),
SourceList = List.Buffer(Source[Column1]),
ListCount = List.Count(SourceList),
GenList = List.Generate(()=>[x=Text.Combine(List.Transform(Text.ToList(SourceList{0}),(a)=>if List.Contains(BuffListAllowedChars,a) then a else " ")),i=0], each [i]<ListCount, each [i=[i]+1, x=Text.Combine(List.Transform(Text.ToList(SourceList{i}),(a)=>if List.Contains(BuffListAllowedChars,a) then a else " "))], each [x]),
Result = Table.FromColumns({GenList},{"Result"})
in
Result
Here is one way to do it. Just create a blank query, open the Advanced Editor, and replace the code there with the below, to see how it works. This approach updates the values in an existing column. You could also take the code from the Custom1 step and create a new column with it, then delete the original.
Pat
To learn more about Power BI, follow me on Twitter or subscribe on YouTube.
Check out the July 2025 Power BI update to learn about new features.