Text.TrimStart

Updated on

Text.TrimStart is a Power Query M function that removes leading whitespace from a text value. The function returns the modified text value with the leading whitespace removed.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Text.TrimStart(
   text as nullable text,
   optional trim as any,
) as nullable text

Description

Returns the result of removing all leading whitespace from text value text.

Examples

The Text.TrimStart function in Power Query M is a trusted technique for eliminating unwanted characters from the beginning of your text strings. We’ve curated a series of practical examples to showcase the powerful utility of the Text.TrimStart function.

The most straightforward use case for this function is to remove leading whitespace from a text string. When the second argument is not provided, the function defaults to trimming whitespace characters.

For instance, consider the string ” a b c d “. With Text.TrimStart, we can strip the leading spaces:

Text.TrimStart( "   a b c d    " ) // Returns "a b c d    "

In addition to spaces, you can instruct the function to remove any character from the start of a string. By specifying this character in the second argument, the function will only remove that particular character.

For example, the text “**gorilla**” has asterisks at both ends, but if you want to remove the asterisks only at the start, you would do as follows:

Text.TrimStart( "***gorilla***", "*" ) // Returns "gorilla***"

There may be situations where you need to remove multiple different characters from the start of a string. In these cases, the function can accept a list of text values in the second argument, indicating the characters to be removed.

To illustrate, let’s say we want to get rid of both spaces and asterisks from “* * gorilla”. Passing both ” ” and “*” in a list to Text.TrimStart does the job perfectly:

Text.TrimStart( "* * gorilla", { " ", "*" } ) // Returns "gorilla"

Pushing the boundaries further, you can feed Text.TrimStart with a generated list to strip all lowercase letters from the start of a string:

Text.TrimStart( "pizzaHut", { "a" .. "z" } ) // Returns "Hut"

Learn more about Text.TrimStart in the following articles:

Other functions related to Text.TrimStart are:

BI Gorilla Blog

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/text-trimstart