Smart Systems for Home

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

Excel Power Query MID Function: Extract Text

· 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 does not have a MID function with that exact name. Instead, you use the M function Text.Middle, which works the same way: you give it a text value, a starting position, and a number of characters to return. The short version is: add a custom column with a formula such as Text.Middle([Column], 2, 5), where the start position is zero-based (the first character is position 0), then expand or replace the original column. This guide walks through the exact clicks, the formula syntax, and the traps that catch most people.

What You'll Need

  • Excel 2016 or later on Windows, Excel 2016 or later on Mac, or a Microsoft 365 subscription. Power Query is built in under the Data tab (called Get & Transform in some versions).
  • A table of data already loaded into Power Query, or the willingness to load one. Select your data, then go to Data > From Table/Range.
  • Basic familiarity with the Power Query Editor window: the Queries pane on the left, the data preview in the middle, and the Applied Steps list on the right.

Menu names vary slightly by Excel version, device, and region. If you cannot find a button exactly as named, look for the closest match in the same tab.

Step-by-Step

  1. Load your data into Power Query. Select a cell inside your table, then choose Data > From Table/Range. If prompted, confirm the range and tick My table has headers. The Power Query Editor opens.
  2. Decide what you are extracting. Write down the start position and length. Remember Power Query counts from zero, not one. So to get characters 1 through 5 of a string, you use start 0 and count 5. To skip the first two characters and take the next three, use start 2 and count 3.
  3. Add a custom column. On the Add Column tab, click Custom Column. Give it a name such as Extracted. In the formula box, type the Text.Middle expression. For example, to pull five characters starting at the third character of a column named Code, enter: Text.Middle([Code], 2, 5) Column names with spaces need to be wrapped in square brackets and a hash, like Text.Middle([#"Order Code"], 2, 5). Click OK.
  4. Check the preview. The new column appears at the right of the table. Scroll through a sample of rows to confirm the output matches what you expect. If a row shows an error, that usually means the source value is null or not text.
  5. Handle nulls and numbers. Text.Middle expects text. If your column contains numbers, wrap it: Text.Middle(Text.From([Code]), 2, 5). If it may contain nulls, wrap the whole thing in try ... otherwise null, for example try Text.Middle([Code], 2, 5) otherwise null. This prevents the whole query from failing on one bad row.
  6. Remove or keep the original column. Right-click the original column header and choose Remove if you no longer need it, or leave it in place. Removing it keeps the output tidy.
  7. Rename the new column. Double-click the new column header and type a clear name, such as Product Code.
  8. Load the result. On the Home tab, click Close & Load to send the table to a new worksheet, or Close & Load To to choose a location such as an existing worksheet or the Data Model.

If you prefer not to write a formula, you can also use the interface: select the column, then Transform > Extract > Range (or First Characters / Last Characters). This generates the same Text.Middle step for you, which is a good way to learn the syntax.

Common Mistakes

  • Using one-based positions. Excel's worksheet MID function starts at 1. Power Query's Text.Middle starts at 0. If your results are shifted by one character, this is almost always why.
  • Forgetting the length argument. Text.Middle needs three arguments: text, start, count. Omitting the count returns characters to the end of the string, which is often not what you intended.
  • Passing a number column directly. Text.Middle fails on numeric types. Convert with Text.From first.
  • Ignoring case and spaces. Text.Middle is case-sensitive in the sense that it does not change case, and leading or trailing spaces count as characters. Trim with Text.Trim first if your data is messy.
  • Hard-coding positions in a column that varies in length. If the text before the part you want varies, a fixed start position will fail. Use Text.PositionOf or Text.BeforeDelimiter to locate the split point instead.

Tips

  • Use the formula bar to edit the generated M code directly if you want to fine-tune a step created by the Extract buttons.
  • Test on a small sample first. Duplicate the query, apply the step, and compare a handful of rows before loading the full dataset.
  • Combine Text.Middle with Text.Trim and Text.Upper or Text.Lower to clean up extracted codes in one step.
  • If you extract the same pattern repeatedly, save the query as a function or template so you do not rebuild the steps each time.
  • Watch the Applied Steps list. Each click adds a step, and you can delete a mistaken step by clicking the X next to it.

Troubleshooting

Every row shows an error. Check the column type. If it is Whole Number or Decimal Number, wrap the column in Text.From. Also confirm the column name in the formula matches the actual header, including spaces.

Results are shifted by one character. You are using a one-based start position. Subtract one from your start value.

Some rows are blank. The start position may be beyond the length of shorter strings. Text.Middle returns an empty string when the start is past the end, so guard with a length check or accept the blanks.

The query fails to refresh later. A new row may contain a null or a number. Use try ... otherwise null around the expression so one bad row does not break the whole refresh.

The Extract button is greyed out. Make sure the column is selected and that its data type is Text. Change the type using the icon at the left of the column header.

If that did not work, close the query without saving, reopen it, and rebuild the step from the Add Column > Custom Column dialog. Copy the generated M code into a text file first so you can compare it against the manual formula.

Frequently Asked Questions

Is there a MID function in Power Query?

Not by that name. The equivalent is Text.Middle, which takes text, a zero-based start position, and a character count.

Why does Text.Middle start at zero?

Power Query's M language uses zero-based indexing for text positions, unlike the worksheet MID function. The first character is position 0.

Can I extract text between two delimiters?

Yes. Use Text.BetweenDelimiters, which is often simpler than calculating a start position and length for Text.Middle.

Does Text.Middle work on numbers?

Only after conversion.

What happens if the start position is longer than the text?

Text.Middle returns an empty string rather than an error, so the row will appear blank. Check your positions if that is unexpected.

Can I use Text.Middle in a calculated column in the worksheet?

No. Text.Middle is an M function used inside Power Query. In the worksheet, use the MID function instead.