The Text.Range function pulls out part of a text value. You tell it where to start and how many characters you want, and it returns just that piece.
It’s the Power Query version of Excel’s MID function. The two differences are that it counts positions from 0, and it gives an error if you ask for more characters than the text has.
In this article, I’ll show you how to use it on single values and whole columns, how to combine it with other functions, and how to handle values that are too short.
Follow along with the example file
Text.Range Function Examples.xlsx
Text.Range Function Syntax
Text.Range(text as nullable text, offset as number, optional count as nullable number) as nullable text
- text – the text you want to extract from.
- offset – the position where the extraction starts. Positions start at 0, so the first character is at offset 0, the second at offset 1, and so on.
- count (optional) – how many characters you want. Leave it out to get everything from the offset to the end of the text.
What it returns – the extracted text. If the text is null, you get null back.
If the offset or the count goes past the end of the text, Text.Range returns an error instead of a shorter result. Example 6 shows what that looks like, and Example 7 shows how to handle it.
The Text.Range function is part of the Power Query M language, so it works wherever Power Query does in Excel: Excel 365, 2024, 2021, 2019, and 2016 for Windows, and Excel for Microsoft 365 on Mac.
When to Use the Text.Range Function
Use this function when you need to:
- Get a fixed number of characters from the middle of a text value
- Remove a fixed-length prefix and keep the rest
- Split up codes where each part always sits at the same position
- Pull out a date or number that’s stored inside a longer code
- Extract text that starts right after a marker, together with Text.PositionOf
Example 1: Extract Characters From the Middle of a Text
Let’s start with a simple one.
Suppose you have the warehouse receipt number “WH-2026-08813” and you want to get the year, 2026, out of it.
In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:
= Text.Range("WH-2026-08813",3,4)
Result: 2026

Here, the offset is 3 and the count is 4, so Text.Range starts at position 3 and returns four characters.
The year starts at the fourth character, but the offset is 3. That’s because Power Query counts positions from 0, so the “W” at the start is at position 0, “H” is at 1, and the hyphen is at 2.
If you’re used to Excel’s MID function, this is the part to watch. MID starts counting at 1, so MID(“WH-2026-08813”,4,4) returns the same 2026 with a start of 4.
Example 2: Remove a Fixed Prefix
Here’s a common cleanup job.
Below is a PrintJobs table where every Job ID starts with the letters JOB, and I want to remove those three letters and keep just the number in the Job ID column.

To follow along, select any cell in the PrintJobs table and go to Data > From Table/Range. This opens the table in the Power Query Editor, where the Source step holds it.
If Power Query also adds a Changed Type step, you can delete it, because the formula below builds on Source.
Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:
= Table.TransformColumns(Source,{{"Job ID",each Text.Range(_,3),type text}})
Result: The Job ID column now shows 10482, 10483, 10491, 10507, and 10512.

In the above formula, Table.TransformColumns changes the Job ID column in place, so you don’t end up with an extra column. The underscore (_) stands for the current value in that column.
Since there’s no count here, Text.Range starts at offset 3 and returns everything to the end of the text. That makes it handy for prefixes, because it works the same whether the number after JOB has five digits or eight.
Note that Text.Range doesn’t check what those first three characters are. It drops any three, so use it only when every value has the same prefix length.
The Job ID column is still text after this step. If you need the numbers for math, change its type to whole number, which would also drop any leading zeros.
Example 3: Get the Middle Part of a Fixed-Width Code
This next one comes up a lot with codes built from fixed-length parts.
Below is a Badges table with employee badge codes, where the first three letters are the office, the next three are the department, and the last four digits are the employee number, and I want the department in a new Department column.

Load the Badges table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:
= Table.AddColumn(Source,"Department",each Text.Range([Badge],3,3),type text)
Result: A new Department column with FIN, OPS, LEG, MKT, and FIN.

In the above formula, Table.AddColumn adds the Department column. For each row, Text.Range starts at offset 3 (the fourth character) of the Badge value and returns three characters.
There’s no hyphen or other separator in these codes, so delimiter functions can’t help here. Text.Range works because every part always sits at the same position.
If a Badge cell were blank, Text.Range would return null for that row instead of an error.
Example 4: Get a Date Out of a Lot Number
Sometimes the part you extract needs to become a real value, like a date.
Below is a Lots table where each lot number has the production date written as year, month, and day right after the letters LOT, and I want that date in a new Made On column.

Load the Lots table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:
= Table.AddColumn(Source,"Made On",
each Date.FromText(Text.Range([Lot Number],3,8),[Format="yyyyMMdd"]),
type date)
Result: A new Made On column with 11/3/2026, 11/4/2026, 11/10/2026, 11/17/2026, and 11/24/2026.

Here, Text.Range takes eight characters starting at offset 3. For “LOT20261103-07”, that’s “20261103”.
Then Date.FromText turns that text into a date. The Format option “yyyyMMdd” tells it the first four digits are the year, the next two the month, and the last two the day.
The capital MM matters, because a lowercase mm means minutes, and Date.FromText gives an error.
The dates show in your computer’s regional format. On a UK setup, the first one shows as 03/11/2026, but it’s the same date, November 3, 2026.
Follow along with the example file
Text.Range Function Examples.xlsx
Example 5: Extract Text That Starts After a Marker
So far, the part we wanted always started at the same position. In this example, it can be anywhere in the text.
Below is a PurchaseNotes table with free-text notes from vendors, where most notes mention a six-digit purchase order number right after “PO#”, and I want that number in a new PO Number column.

Load the PurchaseNotes table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:
= Table.AddColumn(Source,"PO Number",
each let Pos = Text.PositionOf([Note],"PO#") in
if Pos = -1 then null else Text.Range([Note],Pos + 3,6),
type text)
Result: A new PO Number column with 418230, 418257, 418261, null, 418274, and 418290.

In the above formula, Text.PositionOf finds where “PO#” starts in each note, and the let … in part stores that position in a variable called Pos.
Adding 3 to Pos skips past the three characters of “PO#”, so Text.Range starts at the first digit and returns six characters.
The Westbrook Supply note has no “PO#” in it, so Text.PositionOf returns -1 for that row. Without the if check, the offset would become 2 and you’d get six random characters from the note (“ote re”), with no error to warn you.
That’s why the formula returns null when Pos is -1.
Also note that Text.PositionOf is case-sensitive, so a note with “po#” in lowercase would also get null.
The formula also expects six characters after the marker. If a note ends with fewer than six, Text.Range gives an error for that row, which flags a PO number that got cut off.
If you just want everything after the marker and an empty result is fine when it’s missing, Text.AfterDelimiter([Note],”PO#”) is simpler. Text.Range is the better fit when you need an exact number of characters from that point.
Example 6: When the Text Is Too Short
Text.Range is strict about length, and this example shows what that means.
Below is a StyleCodes table where most codes are three letters for the item, three for the color, and one for the size, but a couple of codes are shorter, and I want the color in a new Color column.

Load the StyleCodes table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:
= Table.AddColumn(Source,"Color",each Text.Range([Style Code],3,3),type text)
Result: A new Color column with BLU, RED, Error, BLK, and Error.

Here, the formula asks for three characters starting at offset 3. “MUGW” has only one character from offset 3 onwards, and “BAG” has none, so Text.Range can’t return three characters for those rows.
Instead of returning whatever is there, it gives an error. If you click on the empty space next to the word Error in one of those cells, the preview shows the message “The ‘count’ argument is out of range.”
You’d get a similar error, “The ‘offset’ argument is out of range.”, if the offset itself were past the end of the text.
“BAG” shows that an offset exactly at the end is allowed, because its error comes from asking for three characters, not from offset 3. With no count, Text.Range(“BAG”,3) returns empty text.
Example 7: Handle Values That Are Too Short
Now let’s fix the errors from the previous example.
Below is a StyleCodesFixed table with the same style codes, and I want the color in a new Color column, with no Error cells for the short codes.

Load the StyleCodesFixed table into Power Query the same way (select a cell in it and go to Data > From Table/Range). Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:
= Table.AddColumn(Source,"Color",
each try Text.Range([Style Code],3,3) otherwise null,
type text)
Result: A new Color column with BLU, RED, null, BLK, and null.

Here, try runs Text.Range, and if it gives an error, otherwise returns null instead. So the good codes still get their color, and the short ones get null.
The null cells make the bad codes easy to spot. You can filter the Color column for null to find them and fix them at the source.
Keep in mind that try catches any error, not just short text. If the column could hold other problems, like a number instead of a code, those rows would quietly become null too.
If you’d rather take whatever characters are there, use Text.Middle instead. It works like Text.Range but never errors when the text runs out, so it returns “W” for “MUGW” and empty text for “BAG”.
The catch is that “W” looks like a real value, even though the code is incomplete. So when a short value means bad data, try … otherwise null is the safer choice, because the problem rows stand out.
Other Excel Articles You May Also Like:
- Table.SplitColumn function
- Text.Length function
- Text.End – Use Text.End in Power Query M to get the last characters of text.
- Text.Start – Use Text.Start in Power Query M to grab the first few characters from a text value.
- Text.BetweenDelimiters – Use Text.BetweenDelimiters in Power Query M to extract text between delimiters.
- Text.AfterDelimiter – Use Text.AfterDelimiter in Power Query M to get text after a delimiter.
- Text.BeforeDelimiter – Use Text.BeforeDelimiter in Power Query M to get text before a delimiter.
- Text.Remove – Use Text.Remove in Power Query M to strip characters from text.