Power Query M code
(inputText as nullable text) as nullable text =>
if inputText = null then null else Text.Proper(Text.Clean(Text.Trim(inputText)))
A reusable custom function that trims, cleans and proper-cases any text value passed to it.
(inputText as nullable text) as nullable text =>
if inputText = null then null else Text.Proper(Text.Clean(Text.Trim(inputText)))
Save this as its own query named fnCleanText, then call it from other queries with Table.TransformColumns(Source, {{ColumnName, fnCleanText, type text}}).
Browse all 145 snippets in the Power Query M Code Snippets, or search the library for something similar to fnCleanText.