Smart Systems for Home

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

Power Query Text.End: Extract Characters from the End

· 5 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.End returns the last N characters of a text value in Power Query. Use it when you need to pull a suffix, check the tail of a code, or split a string from the right. The function is available in both Excel Power Query and Power BI, and the steps below work in either. You do not need any add-ins or advanced setup—just the Power Query Editor and a column of text.

What You'll Need

  • Excel (any recent version with Power Query) or Power BI Desktop.
  • A table with at least one text column, or a blank query where you can type a literal string.
  • Basic familiarity with the Power Query Editor: adding a custom column, editing M code, and closing/applying changes.

Step-by-Step

  1. Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI, use Home > Transform data to open the Power Query Editor. If you are starting from scratch, use Data > Get Data > Blank Query and enter a value like "ABCDE12345" in the formula bar.
  2. Identify the text column. Click the column header that contains the text you want to trim. Note the exact column name—you will reference it in the formula. For this example, assume the column is named Code.
  3. Add a custom column. On the Add Column tab, click Custom Column. In the dialog, give the new column a name (for example, LastThree). In the formula box, type:
    Text.End([Code], 3)
    This returns the last three characters of each value in the Code column. Click OK.
  4. Check the result. The new column appears with the extracted text. If a value is shorter than the number you requested, Text.End returns the whole string rather than an error. For example, Text.End("AB", 3) returns "AB".
  5. Adjust the number as needed. To get a different length, change the second argument. Use Text.End([Code], 1) for the last character, or Text.End([Code], 5) for the last five. You can also use a column reference for the count, but that is uncommon and requires the count column to be a number.
  6. Handle nulls and errors. If the column contains null, Text.End returns null. If the column contains a non-text value (such as a number), you may get an error. To avoid this, wrap the column in Text.From: Text.End(Text.From([Code]), 3). This converts numbers to text first.
  7. Rename and reorder if needed. Right-click the new column header to rename it, or drag it to a new position. This step is optional but helps keep the query readable.
  8. Apply the changes. In Excel, click Close & Load > Close & Load To to send the result to a worksheet. In Power BI, click Close & Apply. The new column is now part of your data model.

Common Mistakes

  • Forgetting the square brackets. Column references in M use square brackets: [Code], not Code. Without brackets, Power Query treats it as a function or keyword and throws an error.
  • Passing a negative number. Text.End expects a non-negative count. A negative value produces an error. If you need to count from the left, use Text.Start instead.
  • Assuming Text.End pads short strings. It does not. If the string is shorter than the requested count, you get the whole string, not an error and not padded spaces.
  • Using Text.End on numbers without conversion. Numeric columns cause errors. Use Text.From or change the column type to Text before applying the function.
  • Mixing up Text.End with Text.AfterDelimiter. Text.End counts characters from the right; Text.AfterDelimiter splits on a delimiter. Choose based on whether you know the position or the separator.

Tips

  • Use Text.Length first to see how long your strings are. That helps you pick a count that does not exceed the shortest value.
  • Combine Text.End with Text.Start to split a fixed-format code into two parts: Text.Start([Code], 5) for the prefix and Text.End([Code], 3) for the suffix.
  • If you need the last word rather than the last characters, use Text.AfterDelimiter with a space delimiter and the optional index argument set to the last occurrence.
  • Test on a small sample before applying to a large table. You can duplicate the query, apply the step, and compare results.
  • Remember that Text.End is case-sensitive only in the sense that it returns characters as they appear; it does not change case. Use Text.Lower or Text.Upper if you need consistent casing.

Troubleshooting

If you see an error like "We cannot convert the value null to type Text", check for nulls in the source column. You can filter them out or use try Text.End([Code], 3) otherwise null to handle them gracefully. If the function returns unexpected results, verify the column data type is Text. A column that looks like text may be stored as Any or Number. Change the type using the Transform tab > Data Type > Text. If the custom column dialog rejects your formula, check for missing commas or parentheses. The formula must be a single expression; line breaks are allowed but not required. If the step works in the editor but fails on refresh, the source data may have changed—for example, a column was renamed. Update the column reference in the formula. Finally, if you are using Excel and the query is slow, consider filtering rows before adding the custom column.

Frequently Asked Questions

What does Text.End do in Power Query?

It returns a specified number of characters from the end of a text value. The syntax is Text.End(text as nullable text, count as number) as nullable text.

Can I use Text.End on a column of numbers?

Not directly. Convert the column to text first, either by changing the data type or by wrapping the value in Text.From inside the formula.

What happens if the count is larger than the string length?

Text.End returns the entire string. It does not pad with spaces or throw an error.

Is Text.End case-sensitive?

No. It simply returns the characters as they are; it does not change case or perform any comparison.

How do I get the last character only?

Use Text.End([Column], 1). Replace [Column] with your actual column name.

Can I use Text.End with a dynamic count from another column?

Yes, but the count column must be of type number. For example, Text.End([Code], [Count]) works if [Count] is a number. Otherwise, convert it first.