Title: Replicate Excel's TRIM Function In Power Query | How To Excel
Headings: Replicate Excel’s TRIM Function In Power Query
Replicate Excel’s TRIM Function In Power Query
Table of Contents
The Problem
Video Tutorial
M Code For The Query Function
Create A Blank Query
Add M Code To The Advanced Editor
Using The Query Function
Conclusions

Content: Cancel
Confirm
More videos
More videos
Switch camera
Share
Include playlist
An error occurred while retrieving sharing information. Please try again later. Watch on
0:00
0:00
0:00 / 5:54
Live
•
M Code For The Query Function
let ExcelTrim = (TextToTrim) => let ReplacedText = Text. Replace (TextToTrim, " ", " "), Result = if not (Text. Contains (ReplacedText, " ")) then ReplacedText else @ExcelTrim (ReplacedText) in Text. Trim (Result) in ExcelTrim
This function takes a string of text and replaces double spaces with single spaces and repeats the process recursively until it doesn’t find any more double spaces. Then it applies the Text. Trim function to take care of any leading or trailing space characters. Creating a recursive function solves the problem of manually applying repeated Replace Values steps to remove double spaces and is a much more elegant and re-usable solution. Create A Blank Query
The first step to creating a query function is to create a new blank query.