Text.BeforeDelimiter 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.BeforeDelimiter function gives you the part of a text value that comes before a character you pick, like the first name before a space or the username before the @ sign in an email.

In this article, I’ll show you how it works, how to stop at a later occurrence of the delimiter (or count from the end), and what you get back when the delimiter isn’t there.

Follow along with the example file

Text.BeforeDelimiter Function Examples.xlsx

Download

Text.BeforeDelimiter Function Syntax

Text.BeforeDelimiter(text as nullable text, delimiter as text, optional index as any) as any
  • text – the text value you want to extract from.
  • delimiter – the character (or characters) that marks where the part you want ends.
  • index (optional) – which occurrence of the delimiter to use. It counts from 0, so 0 is the first one (the default) and 1 is the second. You can also give a list like {0,RelativePosition.FromEnd} to count from the end of the text instead.

What it returns – the text before the delimiter, without the delimiter itself.

The delimiter is case-sensitive, so “ext” won’t match “Ext”. When the case is different, there’s no match, so you get the whole text back without an error.

The function also only accepts text. Pointing it at a number column gives you a “We cannot convert the value” error, so convert those values with Text.From first.

When to Use the Text.BeforeDelimiter Function

Use this function when you need to:

  • Get the first name from a full name
  • Get the username from an email address
  • Pull the number out of a value with a unit, like “45 min”
  • Keep the first part of a code or ID, like the team and year in “HR-2026-0417”
  • Remove a file extension from file names
  • Strip extra details from the end of a value, like a query string from a URL or an extension from a phone number

Example 1: Get the Text Before a Colon

Let’s start with a simple one.

Suppose you have a note that says “Invoice 4471: Paid in full”, and you only want the invoice part that comes before the colon.

Create a new blank query (in Excel, go to Data > Get Data > From Other Sources > Blank Query) and use this formula:

= Text.BeforeDelimiter("Invoice 4471: Paid in full",":")

Result: Invoice 4471

Text.BeforeDelimiter on "Invoice 4471: Paid in full" with ":" returns Invoice 4471

Here, “:” is the delimiter. Text.BeforeDelimiter finds the first colon in the text and returns everything before it, and the colon itself is not part of the result.

Example 2: Get the First Name From a Full Name

This is probably the most common use of this function.

Below is an Employees table with each person’s full name and department, and I want the first name in a new First Name column.

Employees table with a Full Name and a Department column

To follow along, select any cell in the Employees 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, you can delete it).

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

= Table.AddColumn(Source,"First Name",each Text.BeforeDelimiter([Full Name]," "),type text)

Result: A new First Name column with Eric, Paul, Heather, Martin, and Judith.

New First Name column with the text before the first space: Eric, Paul, Heather, Martin, Judith

In the above formula, Table.AddColumn adds a new column called First Name. For each row, Text.BeforeDelimiter looks at the Full Name value and returns the text before the first space.

Here, each tells Power Query to run the formula once for every row, and [Full Name] is the value in the Full Name column of that row. The last argument, type text, sets the data type of the new column to text.

Since it stops at the first space, a name like “Mary Ann Smith” gives you just Mary. And if a name starts with a space, you get an empty value, so clean those with Text.Trim first.

Example 3: Get the Username From an Email Address

Here’s another one you’ll use a lot.

Below is a Members table with each member’s name and email address, and I want the part of the email before the @ sign in a new Username column.

Members table with a Member and an Email column

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

= Table.AddColumn(Source,"Username",each Text.BeforeDelimiter([Email],"@"),type text)

Result: A new Username column with wcooper, mike.evans, angela.j, juliamason, and cpatterson.

New Username column with the part of each email before the @ sign

In these addresses, the text before the first @ sign is the username. Dots inside the username (like in mike.evans) don’t matter because the delimiter is @.

If you want the other side of the @ sign (the domain), use Text.AfterDelimiter instead. It works the same way but returns the text after the delimiter.

Example 4: Get the Number Before a Unit

This one comes up when numbers are stored with a unit next to them.

Below is a Workouts table where each duration is written like “35 min”, and I want the minutes as a number in a new Minutes column.

Workouts table with durations written like 35 min

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

= Table.AddColumn(Source,"Minutes",each Number.From(Text.BeforeDelimiter([Duration]," ")),Int64.Type)

Result: A new Minutes column with 35, 60, 45, 50, and 30.

New whole-number Minutes column: 35, 60, 45, 50, 30

Here, Text.BeforeDelimiter returns the text before the space, like “35”. It always returns text, so Number.From converts “35” into the number 35.

Int64.Type then sets the new column as a whole number column, so you can sum or average it right away.

Example 5: Use More Than One Character as the Delimiter

The delimiter doesn’t have to be a single character. You can use a space-hyphen-space or even a whole word.

Below is a Listings table where each listing has the product name, size, and color separated by ” – “, and I want only the product name in a new Product column.

Listings table with the product name, size and color separated by space-hyphen-space

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

= Table.AddColumn(Source,"Product",each Text.BeforeDelimiter([Listing]," - "),type text)

Result: A new Product column with Ceramic Mug, Non-Stick Pan, Glass Water Bottle, Cast Iron Skillet, and Pop-Up Food Cover.

New Product column with the full product name, including Non-Stick Pan and Pop-Up Food Cover

Here, the delimiter is ” – ” (a hyphen with a space on each side). This matters because some product names have a hyphen in them.

If you use just “-” as the delimiter, “Non-Stick Pan – 8 inch – Red” gives you “Non”, because that’s the text before the first hyphen. With ” – “, the hyphen in Non-Stick doesn’t match, and you get the full product name.

Example 6: Get the Text Before the Second Hyphen

So far, every example used the first occurrence of the delimiter. The third argument lets you pick a later one.

Below is a Cases table with case IDs like HR-2026-0417, where the first part is the team and the second part is the year, and I want the team and year together in a new Team and Year column.

Cases table with case IDs like HR-2026-0417 and a Priority column

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

= Table.AddColumn(Source,"Team and Year",each Text.BeforeDelimiter([Case ID],"-",1),type text)

Result: A new Team and Year column with HR-2026, IT-2025, FIN-2026, IT-2026, and HR-2025.

New Team and Year column with the text before the second hyphen: HR-2026, IT-2025, FIN-2026, IT-2026, HR-2025

The 1 in the third argument tells Power Query to stop at the second hyphen. The count starts at 0, so 0 is the first hyphen (the default) and 1 is the second.

The result keeps the first hyphen, because it’s part of the text before the second one. And since the team codes have different lengths (HR, IT, FIN), this works better than taking a fixed number of characters with Text.Start.

If you ask for an occurrence that doesn’t exist, say 1 in “HR-0417” with only one hyphen, you get the whole text back instead of an error.

Example 7: Remove the File Extension From File Names

Sometimes the delimiter you need is the last one in the text, and the number of delimiters before it changes from row to row.

Below is an Attachments table with file names, some of which have dots in the name itself (like Q3.Sales.Report.xlsx), and I want the name without the extension in a new Name column.

Attachments table with file names, some with dots in the name itself

Using “.” as the delimiter with the default index won’t work here, because it stops at the first dot. For Q3.Sales.Report.xlsx, that would give you just Q3.

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

= Table.AddColumn(Source,"Name",each Text.BeforeDelimiter([File Name],".",{0,RelativePosition.FromEnd}),type text)

Result: A new Name column with Q3.Sales.Report, budget-2026, team.photo.final, meeting notes, and invoice.v2.

New Name column with everything before the last dot: Q3.Sales.Report, budget-2026, team.photo.final, meeting notes, invoice.v2

Here, {0,RelativePosition.FromEnd} tells Power Query to count the dots from the end of the text, and 0 means the last dot. So you get everything before the last dot, no matter how many dots the name has.

Be careful with names that have no dot. When you count from the end and the text has no dot at all (like a file named “Read Me”), you get an empty value, not the whole text.

This is different from the default, which returns the whole text when the delimiter is missing.

If some of your file names have no extension, check for a dot first and keep the name as it is when there isn’t one:

= Table.AddColumn(Source,"Name",each if Text.Contains([File Name],".") = true then Text.BeforeDelimiter([File Name],".",{0,RelativePosition.FromEnd}) else [File Name],type text)

The Text.Contains check goes to the first branch only when it returns true. A name with no dot stays as it is, and a blank file name stays null.

Example 8: When the Delimiter Is Missing

Not every row will have the delimiter you’re looking for, and it helps to know what you get back.

Below is a PageViews table with page URLs, where some have a query string after a “?” (like ?ref=newsletter) and some don’t, and I want the clean page path in a new Page column.

PageViews table with URLs (some with a ?ref query string) and a Views column

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

= Table.AddColumn(Source,"Page",each Text.BeforeDelimiter([URL],"?"),type text)

Result: A new Page column with /pricing, /blog/excel-tips, /signup, /contact, and /pricing.

New Page column: URLs without a ? come through unchanged

When the delimiter isn’t in the text, Text.BeforeDelimiter doesn’t give an error. It returns the whole text, so /blog/excel-tips and /contact come through as they are.

That’s exactly what you want here, because a URL without a “?” is already a clean page path. Now both /pricing rows show the same page, so you can group them with Table.Group and add up their views.

The same thing happens when the case doesn’t match. If the formula looks for “Ref:” but the text says “ref:”, you get the whole text back without any warning, so it’s worth checking a few rows after you add the step.

Example 9: Clean a Column in Place

All the examples so far added a new column. If you’d rather fix the values in the existing column, you can use Text.BeforeDelimiter with Table.TransformColumns.

Below is an Offices table with each office’s phone number, where some numbers have an extension at the end (like “ext. 22”), and I want to remove the extensions right in the Phone column.

Offices table with phone numbers, some ending in an extension like ext. 22

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

= Table.TransformColumns(Source,{{"Phone",each Text.BeforeDelimiter(_," ext."),type text}})

Result: The Phone column now shows 312-555-0147, 646-555-0193, 415-555-0128, 202-555-0175, and 713-555-0164.

Phone column with the extensions removed in place

Here, Table.TransformColumns changes the values in the Phone column instead of adding a new one. The _ stands for the current value in that column, so Text.BeforeDelimiter runs once on each phone number.

The inner list {“Phone”,each …,type text} holds the column to change, what to do to each value, and the column type. The outer braces can hold one such list per column, so you can clean several columns in one step.

The delimiter ” ext.” starts with a space, so the result doesn’t end with a space. And the numbers without an extension come back unchanged, because the delimiter is missing (just like in Example 8).

Other Excel Articles You May Also Like:

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.