Smart Systems for Home

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

Power Query Substring: Extract Text in Excel & Power BI

· 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

Power Query has no single "substring" button, but you can extract any part of a text value with the Text.Start, Text.End, Text.Middle, Text.BeforeDelimiter and Text.AfterDelimiter functions. For most everyday cases the fastest route is the ribbon: select the column, then use Add Column > Extract > First Characters, Last Characters, Range or Text Between Delimiters. The ribbon writes the correct M code for you, so you get a reusable, refresh-safe step without typing a formula.

This guide covers the ribbon method first, then the M functions for cases the ribbon cannot handle, such as a start position that changes from row to row. Menu labels can differ slightly by Power Query version, Excel vs Power BI, and region, so treat the exact wording as a guide.

What You'll Need

  • Excel (Power Query is built in under the Data tab) or Power BI Desktop (Transform data opens Power Query Editor).
  • A table or range loaded into Power Query, or a connection to your data source.
  • Basic comfort with the Applied Steps pane, since every extraction you add becomes a step you can rename, reorder or delete.
  • A clear idea of the pattern in your text: fixed length, fixed delimiter, or a variable position.

Step-by-Step

Method 1: Use the ribbon (fixed length or delimiter)

  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.
  2. Select the column you want to extract from by clicking its header. If you need to keep the original, right-click the header and choose Duplicate Column first, then work on the copy.
  3. Go to Add Column > Extract. Choose the option that matches your pattern:
    • First Characters for a fixed number of characters from the left (for example, the first 3 characters of a product code).
    • Last Characters for a fixed number from the right.
    • Range for a starting position and a count. The starting position is 0-based in the dialog, so character 1 is entered as 0.
    • Text Between Delimiters for text sitting between two known characters, such as the part between two hyphens.
  4. Enter the count, range or delimiters in the dialog and click OK. Power Query adds a new column with the extracted text and a new step in Applied Steps.
  5. Rename the new column by double-clicking its header, and rename the step in Applied Steps so the query stays readable.
  6. Check the column's data type. Extracted text often looks like a number; set it to Text or Whole Number deliberately with the type icon in the column header.
  7. Remove the original column if you no longer need it, then close and load (Home > Close & Load in Excel, or Close & Apply in Power BI).

Method 2: Write the M function (variable position)

  1. Select the column, then go to Add Column > Custom Column.
  2. In the formula box, use one of these functions. Positions are 0-based, so the first character is position 0.
    • Text.Start([Column], 3) returns the first 3 characters.
    • Text.End([Column], 4) returns the last 4 characters.
    • Text.Middle([Column], 2, 5) starts at position 2 and returns 5 characters.
    • Text.BeforeDelimiter([Column], "-") and Text.AfterDelimiter([Column], "-") return the text before or after a delimiter.
    • Text.BetweenDelimiters([Column], "(", ")") returns the text between two delimiters.
  3. For a position that changes per row, nest a function inside Text.Middle. For example, Text.Middle([Column], Text.PositionOf([Column], "-") + 1, 4) finds the hyphen and takes 4 characters after it.
  4. Click OK, then set the new column's data type and rename it.
  5. If a row is shorter than the requested range, the function returns null rather than an error. Wrap it in try ... otherwise "" if you want a blank instead of null.

Common Mistakes

  • Forgetting that M positions start at 0. Text.Middle([Column], 1, 3) skips the first character. The ribbon's Range dialog follows the same rule.
  • Extracting from the wrong column. The ribbon's Extract options work on the selected column; if the preview looks wrong, check the selection before clicking OK.
  • Losing leading zeros. If you extract digits and let Power Query detect the type as a number, "007" becomes 7. Set the type to Text when the zeros matter, such as ZIP codes or part numbers.
  • Hard-coding a delimiter that varies. A hyphen in one row may be a slash in another. Test the pattern on a sample before applying it to the whole column.
  • Editing the source column instead of adding one. Use Add Column > Extract, not Transform > Extract, unless you are certain you want to replace the original.

Tips

  • Duplicate the column before extracting so you can compare the result against the original while you build the step.
  • Use Text.PositionOf and Text.PositionOfAny to locate a delimiter when its position changes, then feed that number into Text.Middle.
  • Use Text.Length to check the length of a value before choosing a range, and Text.Contains to test whether a delimiter exists at all.
  • Name every step. A query with ten steps called "Extracted Text" is hard to debug later.
  • Keep extraction steps near the top of Applied Steps so later transformations work on clean text.
  • For repeated patterns, build the logic once in a custom function and invoke it on each column instead of copying formulas.

Troubleshooting

  • The new column shows Error. Click the error cell to see the message. A common cause is a null value or a position beyond the text length. Add a null check with if [Column] = null then null else ....
  • The result is blank. The delimiter may not exist in that row, or the start position may be past the end of the text. Test with Text.Contains first.
  • The preview looks right but the loaded table does not. Check that the step order has not changed and that the data type step is applied after the extraction, not before.
  • The query is slow. Row-by-row custom columns are slower than the built-in Extract options. Use the ribbon where possible, and filter rows before extracting.
  • Refresh breaks the query. If the source column name changed, the step references an old name. Rename the step's reference or re-add the column.
  • If that did not work, delete the last step, re-select the column, and rebuild the extraction from the ribbon. Then compare the generated M code with your manual formula to spot the difference.

Frequently Asked Questions

Is there a SUBSTRING function in Power Query?

No. The equivalent is Text.Middle for a start position and length, Text.Start for the left side, and Text.End for the right side. The ribbon's Extract menu wraps these for you.

Are Power Query positions 0-based or 1-based?

0-based. The first character is position 0, both in the M functions and in the Range dialog.

How do I extract text between two characters?

Use Text.BetweenDelimiters([Column], "(", ")") in a custom column, or Add Column > Extract > Text Between Delimiters from the ribbon.

Why does my extracted number lose its leading zeros?

Power Query detected the column as a number. Change the data type to Text with the type icon in the column header.

Can I extract based on a delimiter that appears more than once?

Yes. Text.BeforeDelimiter and Text.AfterDelimiter accept an optional occurrence index, so you can target the second or third instance rather than the first.

Does this work the same in Excel and Power BI?

The M functions and the Extract menu are the same engine, so the steps match. Only the way you open Power Query Editor and load results back differs.