Smart Systems for Home

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

Power Query Text.Range: How to Extract Substrings

· 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.Range extracts a substring starting at a zero-based offset, and the shortest working call is Text.Range(text, start) for everything from that position to the end, or Text.Range(text, start, count) for a fixed number of characters. It is a Power Query M function, so you use it in the Power Query Editor rather than in a worksheet cell. If you have ever needed the first four characters of a product code, the middle segment of an account number, or everything after a delimiter, this is the function that does it.

What You'll Need

  • Excel (Power Query is built into Excel for Windows and Mac), Power BI Desktop, or another tool that hosts Power Query.
  • A table or text column already loaded into the Power Query Editor. You can also test the function on a blank query with Home > New Source > Blank Query and the formula bar.
  • Basic familiarity with the Power Query Editor: the ribbon, the Queries pane, and the formula bar. Menu labels can differ slightly by version, device or region.

Step-by-Step

  1. Load your data into Power Query. Select your table and use Data > From Table/Range (Excel) or Home > Get Data (Power BI). If you are only experimenting, create a blank query and type a literal text value in the formula bar, for example "ABC-12345-XY".
  2. Decide the start position. Text.Range counts from zero, not one. The first character is position 0, the second is 1, and so on. For "ABC-12345-XY", the letter A is at 0, the hyphen is at 3, and the first digit is at 4.
  3. Add a custom column. On the Add Column tab, choose Custom Column. Give it a name such as CodePart.
  4. Write the formula. In the Custom Column dialog, enter a call that references your text column. For example, to pull four characters starting at the fifth position: Text.Range([ProductCode], 4, 4). To take everything from position 4 onward, omit the third argument: Text.Range([ProductCode], 4).
  5. Check the result before closing. The dialog shows a preview. If you see Error, the start position is probably beyond the length of the string, or the column contains nulls. Adjust the numbers or wrap the call (see Troubleshooting).
  6. Click OK. The new column appears in the preview. Rename it if needed by double-clicking the header.
  7. Handle nulls and short strings deliberately. If the column can contain null, use try Text.Range([ProductCode], 4, 4) otherwise null so a single bad row does not break the whole query. If strings can be shorter than start + count, decide whether you want an error or a truncated result; Text.Range returns an error when the requested range runs past the end, so guard it with a length check such as if Text.Length([ProductCode]) >= 8 then Text.Range([ProductCode], 4, 4) else null.
  8. Apply and load. Use Home > Close & Load (Excel) or Close & Apply (Power BI) to bring the result back to your workbook or model.

Common Mistakes

  • Counting from one. The most frequent error is treating the start position as 1-based. Text.Range is 0-based, so Text.Range("Hello", 1, 3) returns ell, not Hel.
  • Forgetting the count argument when a fixed length is needed. Omitting the third argument returns everything to the end of the string, which is fine for suffixes but wrong when you want a fixed-width segment.
  • Requesting more characters than exist. If start + count exceeds the string length, Text.Range raises an error rather than returning a short string. Check lengths first or use try ... otherwise.
  • Using it on numbers. Text.Range expects text. Convert first with or change the column type to Text.
  • Assuming delimiters are handled. Text.Range does not search for a delimiter; it only counts positions. If the position of a hyphen varies, use Text.PositionOf or Text.BetweenDelimiters instead, or combine them with Text.Range.

Tips

  • Use the formula bar to test calls quickly on a blank query; you get an immediate result without adding a column.
  • Combine Text.Range with Text.Length to build safe dynamic ranges: Text.Range([Text], 0, Text.Length([Text]) - 2) strips the last two characters regardless of overall length.
  • For fixed-format codes (for example, a four-digit year at positions 0 to 3), Text.Range is simpler and faster than splitting and recombining columns.
  • Keep a comment or a clear column name noting the positions used, because zero-based offsets are easy to misread months later.
  • If you need the text after a delimiter, Text.AfterDelimiter is usually clearer than calculating positions by hand.

Troubleshooting

  • Error: "The start index cannot be greater than the length of the string." The start position is at or beyond the end of the text. Confirm the string length with Text.Length and reduce the start value.
  • Error on some rows only. Those rows likely contain nulls or shorter strings. Wrap the call in try ... otherwise null or add an explicit length check.
  • Result looks shifted by one character. You are counting from one. Subtract one from your intended position.
  • Unexpected characters at the end. You probably omitted the count argument, so the function returned the rest of the string. Add the third argument.
  • The column type changed to Any. Set the column type explicitly to Text after adding the custom column so later steps behave predictably.
  • Nothing updates after editing. Refresh the query (Home > Refresh Preview) and check that the step is applied rather than left in the formula bar.

Frequently Asked Questions

Is Text.Range zero-based or one-based?

Zero-based. The first character sits at position 0, so a start of 2 begins at the third character.

What happens if I leave out the count argument?

Text.Range returns everything from the start position to the end of the string. It does not return a single character or an error.

Can Text.Range handle null values?

No. A null input produces an error, so wrap the call in try ... otherwise or filter nulls before the step.

Does Text.Range work on numbers or dates?

Only on text. Convert the value first with Text.From or change the column type to Text.

How do I extract text between two delimiters?

Text.Range alone cannot find delimiters. Use Text.BetweenDelimiters, or calculate positions with Text.PositionOf and pass them to Text.Range.

Why does my query return an error when only one row is short?

Text.Range errors when the requested range exceeds the string length, and one error can fail the step. Guard each row with a length check or a try expression.