Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Text.EndsWith Function: How to Use It

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Text.EndsWith is a Power Query M function that returns true or false depending on whether a text value ends with a specified substring. It is case-sensitive by default, which is the single most common source of confusion. You use it inside a custom column, a filter step, or any expression where you need to test the tail of a string, for example to flag file extensions, product codes, or email domains. The function takes two arguments: the text to test and the text to look for. The syntax is Text.EndsWith(text as nullable text, substring as text) as nullable logical. If the first argument is null, the function returns null rather than false, so null handling matters in real datasets.

What You'll Need

  • Power BI Desktop, Excel with Power Query, or Analysis Services with Power Query, any current version.
  • A query with at least one text column you want to test.
  • Basic familiarity with the Power Query Editor: the ribbon, the Applied Steps pane, and the formula bar.
  • Optional but helpful: the M formula language reference open in a browser tab, since the formula bar is where you will type the function.

Menu labels and button names can differ slightly by version, edition, and region, so treat the paths below as a guide rather than an exact map of your ribbon.

Step-by-Step

  1. Load your data into Power Query. In Excel, select your table and use Data > From Table/Range. In Power BI Desktop, use Home > Get data and pick your source. Confirm the text column you want to test appears in the preview grid.
  2. Check the column type. Click the column header icon and confirm it is set to Text. If it shows Any or a number type, change it to Text first. Text.EndsWith expects text; passing a number raises an error rather than converting silently.
  3. Add a custom column. On the Add Column tab, click Custom Column. In the dialog, give the new column a name such as EndsWithTest. In the formula box, type an expression like Text.EndsWith([FileName], ".pdf"), replacing FileName with your actual column name and ".pdf" with the substring you want to test. Column names with spaces need to be wrapped in square brackets exactly as shown, and the substring must be in double quotes.
  4. Click OK and inspect the result. The new column should contain true, false, or null values. If every row is false when you expected some true, jump to the Common Mistakes section: case sensitivity is almost always the cause.
  5. Make it case-insensitive if needed. Wrap the first argument in Text.Lower, for example Text.EndsWith(Text.Lower([FileName]), ".pdf"). Alternatively, lower-case the substring too. Do not lower-case only one side; the comparison will still fail.
  6. Filter or use the result. Click the filter arrow on your new column and keep only true to isolate matching rows. If you would rather filter in place without adding a column, use the column's filter menu and choose Text Filters > Ends With, which builds the same test for you.
  7. Rename and document the step. Right-click the new column header and rename it to something meaningful. In the Applied Steps pane, right-click the step and choose Rename so the query stays readable for anyone who opens it later.
  8. Close and load. Use Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to push the result back to your workbook or model.

Common Mistakes

Forgetting case sensitivity. Text.EndsWith("Report.PDF", ".pdf") returns false. The function compares characters exactly. Wrap the text in Text.Lower or Text.Upper on both sides of the comparison.

Passing a number or date. If the column type is not Text, the function errors. Convert the column first with Table.TransformColumnTypes or wrap the value in Text.From.

Leaving out the quotes. The substring argument must be a text literal in double quotes. Writing Text.EndsWith([Code], .pdf) produces a syntax error because .pdf is read as an identifier.

Ignoring nulls. Rows where the source text is null return null, not false. A filter keeping only true will drop them, which is usually fine, but a filter keeping only false will also drop them. Decide explicitly what null should mean in your report.

Assuming it matches whole words. Text.EndsWith("catalog", "log") returns true. The function tests the tail of the string, not word boundaries. If you need a whole-token match, add a delimiter to the substring, such as ".log".

Tips

Use Text.EndsWith inside Table.AddColumn for row-by-row tests, and inside Table.SelectRows when you want to filter without creating a visible column. For example, Table.SelectRows(Source, each Text.EndsWith([Email], "@example.com")) keeps only matching rows and leaves the table shape unchanged.

Combine it with Text.StartsWith and Text.Contains to build richer classification rules. A common pattern is a nested if expression that tags a row as Image, Document, or Other based on the file extension.

When testing many suffixes, build a list and use List.AnyTrue with List.Transform rather than chaining dozens of or conditions. It is easier to read and easier to maintain when the list changes.

Keep the substring in a parameter or a small lookup table if it changes between reports. Hard-coded literals scattered through steps are the usual reason a query breaks when requirements shift.

Troubleshooting

The custom column dialog rejects the formula. Check three things in order: the column name is wrapped in square brackets, the substring is in double quotes, and the function name is spelled Text.EndsWith with the capital T and E. Power Query is case-sensitive for function names.

Every row returns false. Confirm the column really contains the characters you expect. Leading or trailing spaces in the source will not break an EndsWith test, but invisible characters such as non-breaking spaces inside the tail will. Use Text.Trim on the source column before testing, and inspect a sample value with Text.Length if you suspect hidden characters.

The step errors with "We cannot convert the value to Text". The column type is wrong. Change it to Text in a step before the custom column, then refresh the preview.

The result is null for many rows. Those source values are null. Add a fallback with if [Column] = null then false else Text.EndsWith([Column], ".pdf") if you want a clean true/false output.

If that did not work, remove the custom column, refresh the preview, and rebuild the step from scratch. A stale preview can show results from an earlier version of the query. If the error persists, open the Advanced Editor and read the generated M code; the exact line that fails is usually visible there, and the error message names the argument that is the wrong type.

Frequently Asked Questions

Is Text.EndsWith case-sensitive?

Yes. It compares characters exactly. Use Text.Lower or Text.Upper on both the text and the substring to make the test case-insensitive.

What happens if the text argument is null?

The function returns null, not false. Handle nulls explicitly if your downstream filter or measure depends on a clean boolean.

Can I test more than one ending at once?

Not in a single call. Combine calls with or, or use List.AnyTrue with List.Transform over a list of suffixes.

Does Text.EndsWith work on numbers or dates?

No. Convert the value to text first with Text.From, or change the column type to Text before testing.

Can I use it to filter rows without adding a column?

Yes. Use Table.SelectRows with an each expression, or the column's Text Filters > Ends With menu option, which builds the equivalent step for you.

Why does a match fail when the text looks identical?

Hidden characters, trailing spaces, or different Unicode forms can make two visually identical strings differ. Trim the source and inspect a sample with Text.Length to confirm what is actually there.