The M Code Behind the Power Query M function Text.TrimStart

Understanding the M Language

To understand the M code behind the Text.TrimStart function, we first need to understand the M language. M is the language used by Power Query to manipulate data, and it’s a functional language that uses expressions and functions to transform data.

M is a case-sensitive language, which means that Text.TrimStart and text.trimstart are not the same thing. It’s also a strongly typed language, which means that all values are assigned a specific data type (such as text, number, or date) and can only be manipulated using functions that operate on that data type.

The Text.TrimStart Function

Now let’s take a closer look at the Text.TrimStart function itself. The function takes two arguments: the text string to be trimmed, and the character or set of characters to be removed from the beginning of the string.

Here’s an example of how the function works:

Text.TrimStart(" hello world", " ")

In this example, we’re passing in the string ” hello world” (with three spaces at the beginning) and the character ” ” (a single space). The function will remove the leading spaces and return the string “hello world”.

The M Code Behind Text.TrimStart

So what does the M code behind Text.TrimStart look like? Here’s the code:

(text as text, optional trimCharacter as text) =>


trimChars = if trimCharacter = null then {" "} else {trimCharacter},

trimStart = Text.TrimStart(text, trimChars)



Let’s break this code down piece by piece:

– `(text as text, optional trimCharacter as text) =>` This line defines the function and its arguments. The `text` argument is required, while `trimCharacter` is optional.

– `let` This keyword begins a variable declaration or assignment.

– `trimChars = if trimCharacter = null then {” “} else {trimCharacter}` This line assigns the variable `trimChars` to either the single character specified in `trimCharacter`, or a space character if `trimCharacter` is null.

– `trimStart = Text.TrimStart(text, trimChars)` This line assigns the variable `trimStart` to the result of calling the Text.TrimStart function on the `text` argument and the `trimChars` variable.

– `in trimStart` This line returns the `trimStart` variable as the output of the function.

In conclusion, the Text.TrimStart function is a powerful tool for cleaning and manipulating data in Power Query. Understanding the M code behind the function can help you better understand how it works and how you can use it to your advantage in your data transformation projects.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

20-22 Nov 23 (London or Online)

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)