Text.EndsWith 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.EndsWith function checks whether a text value ends with the characters you give it. It returns TRUE if it does and FALSE if it doesn’t.

In this article, I’ll show you how to use it to flag rows, filter tables, label values, and fix the two things that trip it up most: upper and lower case, and trailing spaces.

Follow along with the example file

Text.EndsWith Function Examples.xlsx

Download

Text.EndsWith Function Syntax

Text.EndsWith(text as nullable text, substring as text, optional comparer as nullable function) as nullable logical
  • text – the text you want to check.
  • substring – the ending you’re looking for, like “.pdf” or ” LLC”.
  • comparer (optional) – controls how the text is compared. Use Comparer.OrdinalIgnoreCase to ignore upper and lower case.

What it returns – TRUE if the text ends with the substring, and FALSE if it doesn’t. If the text is null (an empty cell comes into Power Query as null), you get null back.

The check is case-sensitive by default, so “Report.PDF” does not end with “.pdf”. Example 2 shows this and how to fix it.

Text.EndsWith only works on text. If you give it a number, like 12300, you get the error “We cannot convert the value 12300 to type Text.”, so turn numbers into text first with the Text.From function.

The Text.EndsWith function is part of the Power Query M language, so it works wherever Power Query does in Excel: Excel 365, 2024, 2021, 2019, and 2016 for Windows, and Excel for Microsoft 365 on Mac.

When to Use the Text.EndsWith Function

Use this function when you need to:

  • Flag rows by their ending, like files with a certain extension
  • Keep or remove rows based on how a value ends, like email domains or test order IDs
  • Label rows with an if formula, based on a suffix in a code
  • Add an ending only to the values that are missing it

Example 1: Check if a Text Ends With Something

Let’s start with a simple one.

Suppose you have the file name “Q3-Budget-Final.xlsx” and you want to check whether it’s an Excel file, which means it ends with “.xlsx”.

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.EndsWith("Q3-Budget-Final.xlsx",".xlsx")

Result: TRUE

Text.EndsWith("Q3-Budget-Final.xlsx",".xlsx") returns TRUE

Here, the file name ends with “.xlsx”, so Text.EndsWith returns TRUE. It only looks at the very end of the text, so “.xlsx” showing up anywhere else wouldn’t count.

If you need to check how a text starts instead, use the Text.StartsWith function, which works the same way from the other end.

Example 2: Flag PDF Files (and Ignore Upper and Lower Case)

Here’s a common one with file lists.

Below is an Attachments table with file names, and I want a new Is PDF column that says TRUE for every PDF file.

Attachments table with File Name and Size (KB); some extensions use capital letters

To follow along, select any cell in the Attachments 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 also adds a Changed Type step, you can delete it, because the formula below builds on Source.

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

= Table.AddColumn(Source,"Is PDF",each Text.EndsWith([File Name],".pdf"),type logical)

Result: A new Is PDF column with TRUE, FALSE, FALSE, FALSE, FALSE, and FALSE.

New Is PDF column with TRUE only for Vendor-Agreement.pdf because the check is case-sensitive

In the above formula, the Table.AddColumn function adds the Is PDF column, and Text.EndsWith checks whether each file name ends with “.pdf”. The type logical part sets the new column’s data type to logical, which holds TRUE and FALSE values.

But only Vendor-Agreement.pdf got TRUE. Signed-Lease.PDF and Insurance-Cert.Pdf are PDF files too, but their extensions use capital letters, and Text.EndsWith is case-sensitive by default.

To fix this, add Comparer.OrdinalIgnoreCase as the third argument. Use this formula instead:

= Table.AddColumn(Source,"Is PDF",each Text.EndsWith([File Name],".pdf",Comparer.OrdinalIgnoreCase),type logical)

Result: A new Is PDF column with TRUE, FALSE, TRUE, FALSE, TRUE, and FALSE.

New Is PDF column with TRUE for all three PDF files after Comparer.OrdinalIgnoreCase

Here, Comparer.OrdinalIgnoreCase tells Text.EndsWith to ignore upper and lower case. So “.PDF” and “.Pdf” now count as “.pdf”, and all three PDF files get TRUE.

File extensions are often typed in different cases, so it’s a good habit to add the comparer whenever you check them.

Example 3: Keep Only Rows That End With a Domain

This next one is about filtering a table.

Below is a Registrations table with the people who signed up for a webinar, and I want to keep only the people whose email ends with “@crestview.com”.

Registrations table with Name and Email; one .com.au email and one blank email

Load the Registrations table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.SelectRows(Source,each Text.EndsWith([Email],"@crestview.com"))

Result: Only three rows remain: Melanie Butler, Ellen Allen, and Carrie Turner.

Only Melanie Butler, Ellen Allen and Carrie Turner remain

In the above formula, the Table.SelectRows function goes through the table row by row and keeps a row only when Text.EndsWith returns TRUE for it.

Joel Brooks’s email ends with “@crestview.com.au”, which is a different domain, so his row is removed. The Text.Contains function would have kept it, because it finds “@crestview.com” anywhere in the text.

Caleb Morris has no email, so that cell comes into Power Query as null. Text.EndsWith returns null for it, and Table.SelectRows drops the row without giving an error.

Example 4: Remove Rows That End With Something

You can also flip the check to remove rows instead of keeping them.

Below is an Orders table where test orders have “-TEST” at the end of the Order ID, and I want to remove those test orders.

Orders table with Order ID, Customer and Amount; two test orders end with -TEST

Load the Orders table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.SelectRows(Source,each not Text.EndsWith([Order ID],"-TEST"))

Result: The two test orders are removed, leaving SO-10421, SO-10423, SO-10424, and SO-10426.

Test orders removed, leaving SO-10421, SO-10423, SO-10424, SO-10426

Here, the not keyword flips each result. The test orders return TRUE, which not turns into FALSE, so Table.SelectRows removes them.

Follow along with the example file

Text.EndsWith Function Examples.xlsx

Download

Example 5: Label Rows Based on How a Code Ends

Text.EndsWith also works inside an if formula, so you can turn its result into a label.

Below is a Products table where refurbished products have “-RF” at the end of the SKU, and I want a new Condition column that says Refurbished or New.

Products table with SKU, Product and Price; refurbished SKUs end with -RF

Load the Products table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(Source,"Condition",each if Text.EndsWith([SKU],"-RF") then "Refurbished" else "New",type text)

Result: A new Condition column with New, Refurbished, New, Refurbished, New, and Refurbished.

New Condition column with New or Refurbished

In the above formula, Text.EndsWith checks whether each SKU ends with “-RF”. When it does, the if formula returns “Refurbished”, and when it doesn’t, it returns “New”.

Example 6: Check for More Than One Ending

Sometimes more than one ending should count as a match.

Below is a Leads table with the email of each contact, and I want a new Email Type column that says Personal for Gmail and Yahoo addresses and Work for the rest.

Leads table with Contact and Email

Load the Leads table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(Source,"Email Type",each if Text.EndsWith([Email],"@gmail.com") or Text.EndsWith([Email],"@yahoo.com") then "Personal" else "Work",type text)

Result: A new Email Type column with Work, Personal, Work, Personal, and Personal.

New Email Type column with Personal for Gmail and Yahoo, Work for the rest

Here, each Text.EndsWith checks one ending, and the or keyword returns TRUE if either check is TRUE. So both Gmail and Yahoo addresses get Personal.

Text.EndsWith takes only one ending at a time, and giving it a list like {“@gmail.com”,”@yahoo.com”} gives an error. That’s why the formula uses two checks joined with or.

Example 7: Add a Missing Ending Only Where It’s Needed

You can also use Text.EndsWith to clean up values.

Below is a Folders table with a shared folder path for each team, and I want every path in the Folder Path column to end with a backslash.

Folders table with Team and Folder Path; three paths have no backslash at the end

Load the Folders table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.TransformColumns(Source,{{"Folder Path",each if Text.EndsWith(_,"\") then _ else _&"\",type text}})

Result: Every path in the Folder Path column now ends with a backslash, like S:\Sales\Leads\.

Every Folder Path now ends with one backslash

In the above formula, the Table.TransformColumns function changes the Folder Path column in place, and the underscore (_) stands for the current path.

If a path already ends with a backslash, the if formula leaves it as it is. Otherwise, the & operator adds one at the end.

Without the Text.EndsWith check, the Finance and HR paths would end up with two backslashes. Now every path has exactly one, so you can safely add a file name to the end of it later.

Example 8: Watch Out for Trailing Spaces

This last one is a problem that’s easy to miss.

Below is a Vendors table where some vendor names have extra spaces at the end that you can’t see, and I want a new Is LLC column that says TRUE for every vendor name that ends with ” LLC”.

Vendors table with Vendor and City; two names have invisible trailing spaces

Load the Vendors table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(Source,"Is LLC",each Text.EndsWith([Vendor]," LLC"),type logical)

Result: A new Is LLC column with TRUE, FALSE, FALSE, FALSE, FALSE, and TRUE.

New Is LLC column with FALSE for Harper Lane LLC and Oakline Services LLC because of trailing spaces

Harper Lane LLC and Oakline Services LLC got FALSE, even though both names end with LLC. That’s because their names have trailing spaces, so the real last characters are spaces, not “LLC”.

Power Query gives no error here, so wrong FALSE results like these are easy to miss.

To fix this, remove the trailing spaces with the Text.TrimEnd function before Text.EndsWith checks the ending. Use this formula instead:

= Table.AddColumn(Source,"Is LLC",each Text.EndsWith(Text.TrimEnd([Vendor])," LLC"),type logical)

Result: A new Is LLC column with TRUE, TRUE, FALSE, FALSE, TRUE, and TRUE.

New Is LLC column with TRUE for all four LLC vendors after Text.TrimEnd

Here, Text.TrimEnd removes the spaces at the end of each vendor name first, so Text.EndsWith sees the real ending.

The trim only happens inside the check, so the Vendor column itself still has the trailing spaces. If you’ll use those names later, for example to merge with another table, trim the whole column in its own step first.

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.