Text.TrimStart Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Text.TrimStart function removes characters from the start of a text value and leaves the end exactly as it is. By default it removes leading spaces, and you can also use it to strip leading zeros, bullet characters, or a prefix like INV-.

In this article, I’ll show you how Text.TrimStart works, along with a couple of cases where it removes more than you’d expect (and how to fix them).

Follow along with the example file

Text.TrimStart Function Examples.xlsx

Download

Text.TrimStart Function Syntax

Text.TrimStart(text as nullable text, optional trim as any) as nullable text
  • text – the text value you want to remove the leading characters from.
  • trim (optional) – the character to remove from the start instead of whitespace. This can be a single character (like “0”) or a list of single characters (like {“-“,” “}). Trimming stops at the first character that isn’t in this list.

What it returns – the same text with the leading characters removed. If you don’t use the second argument, it removes leading whitespace, which includes spaces, tabs, line breaks, and non-breaking spaces.

If the input is null, Text.TrimStart returns null instead of an error. A number does give an error (“We cannot convert the value 482 to type Text.”), so convert numbers to text first.

The Text.TrimStart function is part of the core Power Query M library, so it works wherever Power Query does, including Excel 2016 or later on Windows, Excel for Microsoft 365 (Windows and Mac), and Power BI.

When to Use the Text.TrimStart Function

Use this function when you need to:

  • Remove leading spaces while keeping whatever comes at the end of the text
  • Strip the leading zeros from IDs and codes stored as text
  • Clean up bullet characters or dashes left over from a pasted list
  • Remove a symbol like # from the start of a value so it can be converted to a number
  • Remove a letter prefix such as INV- or PO- from document numbers
  • Work out the indentation level of rows in an indented outline

Example 1: Remove Leading Spaces Only

Let’s start with what Text.TrimStart does when you give it just the text.

Suppose you have a status label with three spaces before it and three spaces after it, and you want to remove only the spaces at the start.

Since spaces are invisible in the result, I’ve wrapped the result in square brackets so you can see where the text starts and ends.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:

= "[" & Text.TrimStart("   Pending Review   ") & "]"

Result: [Pending Review] with no space after the opening bracket and three spaces before the closing one

Text.TrimStart removes the leading spaces and keeps the three trailing ones: [Pending Review   ]

Here, Text.TrimStart removes the three spaces before “Pending” and stops at the “P”. The three spaces after “Review” are still there, which is the big difference from Text.Trim (that one cleans both ends).

The & operator only joins the brackets to the result. You won’t need them in real use (they’re only there so you can see the spaces).

Example 2: Remove Leading Zeros From Product IDs

Now let’s use the second argument to clean up a whole column.

Below is an Excel Table named Products where the product IDs were padded with zeros to a fixed length, and I want to remove the leading zeros in the Product ID column.

Products table with a Product ID and a Product column, IDs padded with leading zeros

To follow along, select any cell in the Products table and go to Data > From Table/Range. This opens the table in the Power Query Editor, where the Source step holds it.

If Power Query adds a Changed Type step too, delete it so the IDs stay as text.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.TransformColumns(Source,{{"Product ID",each Text.TrimStart(_,"0"),type text}})

Result: The Product ID column now shows 482, 12300, 7, 960, and 4410.

Product ID column without leading zeros: 482, 12300, 7, 960, 4410

Here, Table.TransformColumns runs Text.TrimStart on every value in the Product ID column. The _ stands for the value in each row, and the “0” tells Text.TrimStart to remove zeros instead of spaces.

Notice that 0012300 became 12300, not 123. Text.TrimStart only looks at the start, so the zeros at the end stay.

Text.Trim would remove those end zeros too, because it trims both ends.

And 4410 had no leading zeros, so nothing changed. If you ever need to add the zeros back, use Text.PadStart.

Example 3: Keep One Zero When a Code Is All Zeros

Removing leading zeros has one trap, and it shows up when a value is nothing but zeros.

Below is an Excel Table named CostCenters with cost center codes and their departments, where two codes are all zeros, and I want to remove the leading zeros in the Code column.

CostCenters table with a Code and a Department column, two codes made only of zeros

If you use the formula from Example 2 here, 0000 and 00 become empty text, because every character in them is a zero. The Unassigned and Head Office rows would then have no code at all.

Load the table into the Power Query Editor (as in Example 2), then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.TransformColumns(Source,{{"Code",each
let Trimmed = Text.TrimStart(_,"0")
in if Trimmed = "" then "0" else Trimmed,type text}})

Result: The Code column now shows 410, 0, 25, 1200, and 0.

Code column without leading zeros, all-zero codes kept as a single 0: 410, 0, 25, 1200, 0

Here, the let part stores the trimmed code as Trimmed. The if then checks if Trimmed is empty, and if it is, it returns “0” instead, so an all-zero code ends up as a single zero.

The same trap hits decimals. Text.TrimStart(“0.75″,”0”) gives “.75”, because the zero before the decimal point is also a leading zero.

Codes like these are labels, not amounts, so they stay as text (for example, to match the codes in another table). That’s why this example trims them instead of converting them.

But if your values really are amounts stored as text, like 0.75, change the column type to a number instead. Numbers don’t keep leading zeros, so they’re gone without any trimming.

Example 4: Remove Bullet Characters From a Pasted List

This one comes up when a list gets copied from an email or a document.

Below is an Excel Table named Tasks with a to-do list where some tasks start with a bullet, a dash, or an asterisk (one also has spaces before its bullet), and I want just the task text in the Task column.

Tasks table with a Task and an Owner column, tasks starting with bullets, dashes, or asterisks

Load the table into the Power Query Editor (as in Example 2), then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.TransformColumns(Source,{{"Task",each Text.TrimStart(_,{"•","-","*"," "}),type text}})

Result: The Task column now shows Book the venue, Send follow-up emails, Confirm catering, Order name badges, and Print the agenda.

Task column with the leading bullets, dashes, asterisks, and spaces removed

Here, the list {"•","-","*"," "} tells Text.TrimStart to remove any mix of bullets, dashes, asterisks, and spaces from the start. It keeps going until it hits a character that isn’t in the list, which is the first letter of the task.

The space has to be in the list. Once you give Text.TrimStart your own characters, it no longer removes whitespace by default, so the spaces after each bullet (and before the bullet in “Order name badges”) would stay.

The hyphen in “follow-up” isn’t touched, because it’s not at the start. And “Print the agenda” had nothing to remove, so it’s unchanged.

Follow along with the example file

Text.TrimStart Function Examples.xlsx

Download

Example 5: Remove the # Sign and Convert Ticket Numbers

Sometimes a symbol at the start is the only thing stopping a value from becoming a number.

Below is an Excel Table named Tickets with support tickets, where most ticket numbers start with a # sign (one has two), and I want the Ticket column as whole numbers.

Tickets table with a Ticket and an Issue column, most ticket numbers starting with a # sign

If you try to change this column’s type to a number directly, the values with a # sign give the error “We couldn’t convert to Number.” So the # has to go first.

Load the table into the Power Query Editor (as in Example 2), then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.TransformColumns(Source,{{"Ticket",each Number.From(Text.TrimStart(_,"#")),Int64.Type}})

Result: The Ticket column now shows the whole numbers 10482, 10483, 10484, and 10485.

Ticket column as whole numbers: 10482, 10483, 10484, 10485

Here, Text.TrimStart removes the # from the start, and Number.From converts what’s left into a number. Int64.Type then sets the column type to Whole Number.

The ##10485 value had two # signs, and both were removed, because Text.TrimStart keeps trimming until it reaches a character that isn’t a #. And 10484 had no # at all, so it went straight to Number.From.

This works because every ticket in the Excel table is stored as text. If a ticket without a # is stored as a real number, Text.TrimStart gives an error for that row, so use Text.TrimStart(Text.From(_),"#") in that case.

Example 6: Remove a Letter Prefix From Document Numbers

You can also give Text.TrimStart a whole range of characters.

Below is an Excel Table named Documents with invoice, credit note, and purchase order numbers, each with a letter prefix and zero padding, and I want just the number part in a new Number column.

Documents table with a Document and an Amount column, document numbers with letter prefixes and zero padding

Load the table into the Power Query Editor (as in Example 2), then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(Source,"Number",each Text.TrimStart([Document],{"A".."Z","-","0"}),type text)

Result: A new Number column with 318, 1200, 45, and 10050.

New Number column: 318, 1200, 45, 10050

Here, Table.AddColumn adds the Number column. Inside it, {“A”..”Z”} is a list of all the uppercase letters from A to Z, so you don’t have to type all 26.

Adding “-” and “0” to the same list removes the hyphen and the padding zeros too.

Since every letter is in the list, it doesn’t matter whether the prefix is INV, CR, or PO. Trimming stops at the first digit that isn’t a zero.

That’s why PO-010050 gives 10050. The zero after the 1 isn’t at the start anymore, so it stays, just like the zeros at the end of 1200.

The list has only uppercase letters. If your prefixes use lowercase too, add “a”..”z” inside the same list, like {"A".."Z","a".."z","-","0"}.

Also, the Number column is text (the formula ends with type text). If you want real numbers, change its type to Whole Number after this step.

Example 7: Find the Indentation Level in an Outline

Text.TrimStart can also help you measure how deeply each row is indented.

Below is an Excel Table named Parts with a bill of materials for a bicycle, where each part is indented with two spaces per level, and I want each part’s level in a new Level column and the part names without the indentation.

Parts table with a Part and a Qty column, parts indented with two spaces per level

Load the table into the Power Query Editor (as in Example 2), open the Advanced Editor (Home > Advanced Editor), and replace the code with this:

let
Source = Excel.CurrentWorkbook(){[Name="Parts"]}[Content],
#"Added Level" = Table.AddColumn(Source,"Level",each (Text.Length([Part])-Text.Length(Text.TrimStart([Part])))/2,Int64.Type),
#"Trimmed Part" = Table.TransformColumns(#"Added Level",{{"Part",Text.TrimStart,type text}})
in
#"Trimmed Part"

Result: The Part column shows the names without the indentation, and the Level column shows 0, 1, 2, 2, 1, 2, 2, and 3.

Part names without indentation and a new Level column: 0, 1, 2, 2, 1, 2, 2, 3

The Added Level step compares the length of each part name before and after Text.TrimStart. The difference is the number of leading spaces, and dividing by 2 turns that into a level, since each level adds two spaces.

For example, Seat Post with its four leading spaces is 13 characters long, and Seat Post on its own is 9. The difference is 4, and 4 divided by 2 is level 2.

The Trimmed Part step then removes the indentation from the Part column. Here Text.TrimStart is written without each, _ or brackets, because it needs no second argument.

When you write just the function name, Table.TransformColumns hands each value straight to it. That does the same thing as each Text.TrimStart(_), which removes the leading whitespace.

The level has to be worked out before the trim. Once the spaces are gone, there’s nothing left to count.

This assumes every level adds exactly two spaces. If your outline uses four spaces per level, divide by 4 instead.

Example 8: Text.TrimStart Removes Characters, Not Words

This one trips people up when they try to remove a word from the start.

Suppose you want to remove “www.” from the start of a website like www.walnutstudio.com. If you try Text.TrimStart(“www.walnutstudio.com”,”www.”), you get the error “The value isn’t a single-character string.”, because the second argument only takes single characters.

So you might split it into its characters. Create a new blank query (as in Example 1) and use this formula:

= Text.TrimStart("www.walnutstudio.com",{"w","."})

Result: alnutstudio.com

Text.TrimStart("www.walnutstudio.com",{"w","."}) returns alnutstudio.com, removing the w of walnut too

That’s not what we wanted.

Text.TrimStart treats the list as a set of separate characters, not as a word. So after it removes “www.”, it sees the “w” at the start of “walnutstudio”, and since “w” is in the list, that goes too.

To remove a whole word, check if the text starts with it, and then skip that many characters.

Below is an Excel Table named Websites with client websites, where most addresses start with “www.”, and I want to remove it in the Website column.

Websites table with a Website and a Client column, most websites starting with www.

Load the table into the Power Query Editor (as in Example 2), then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.TransformColumns(Source,{{"Website",each
if Text.StartsWith(_,"www.")
then Text.Middle(_,4)
else _,type text}})

Result: The Website column now shows walnutstudio.com, westbayfitness.org, harborlights.net, and wildroseinn.com.

Website column with www. removed: walnutstudio.com, westbayfitness.org, harborlights.net, wildroseinn.com

Here, Text.StartsWith checks whether the website starts with “www.”. If it does, Text.Middle returns everything from position 4 onward, which skips the four characters of “www.” (positions start at 0).

The harborlights.net website doesn’t start with “www.”, so the else part returns it unchanged. And walnutstudio.com and westbayfitness.org keep their “w” this time, because the formula skips exactly 4 characters.

If the Website column has empty (null) cells, this formula shows the error “We cannot convert the value null to type Logical.” in those cells, because the if can’t test a null. So replace or filter out the empty cells before this step.

Other Excel Articles You May Also Like:

  • Text.Clean function
  • Text.TrimEnd – Use Text.TrimEnd in Power Query M to remove trailing spaces or characters.
  • Text.Remove – Use Text.Remove in Power Query M to strip characters from text.
  • Text.Start – Use Text.Start in Power Query M to grab the first few characters from a text value.
  • Text.Length – Use Text.Length in Power Query M to count characters in text.
  • Text.BeforeDelimiter – Use Text.BeforeDelimiter in Power Query M to get text before a delimiter.
  • Text.End – Use Text.End in Power Query M to get the last characters of text.
  • Text.Middle – Use Text.Middle in Power Query M to extract characters from the middle of text.

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.