Text.PositionOf returns the zero-based position of the first (or last, or all) occurrence of a substring inside a text value, or -1 when the substring is not found. The function is case-sensitive by default, so "Apple" and "apple" are different needles. You do not need any add-in: it is a built-in Power Query M function available in Excel's Get & Transform, Power BI Desktop, and any tool that runs Power Query.

What You'll Need

  • A version of Excel, Power BI Desktop, or another Power Query host that includes the M engine (Text.PositionOf has been part of the standard library for years, so most current builds have it).
  • A table loaded into the Power Query Editor. If you are starting from scratch, select a cell in your data and choose Data > From Table/Range (Excel) or Get Data (Power BI).
  • Basic familiarity with adding a custom column or editing the formula bar. Menu labels can differ slightly by version, device, and region, so if a button name does not match exactly, look for the equivalent command in the same area.

Step-by-Step

  1. Open the Power Query Editor. In Excel, select your data and use Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the editor. Confirm your table appears in the Queries pane on the left.
  2. Decide where the result should live. If you want a new column, go to Add Column > Custom Column. If you only want to test the function, you can use the formula bar on an existing step instead, but a custom column keeps your original data intact.
  3. Write the basic call. In the Custom Column dialog, name the column (for example, Position) and enter a formula such as Text.PositionOf([ProductName], "Pro"). Here [ProductName] is the text column and "Pro" is the substring you are searching for.
  4. Understand the return value. Positions are zero-based: if the text is "ProVac" and the needle is "Pro", the result is 0. If the text is "SuperPro", the result is 5. If the substring is absent, the result is -1. This matters when you filter later, because -1 is a real number, not null.
  5. Choose which occurrence to return. Add the optional third argument: Occurrence.First (the default), Occurrence.Last, or Occurrence.All. For example, Text.PositionOf([Notes], "e", Occurrence.Last) returns the position of the final "e". Using Occurrence.All returns a list of every position, which is useful with List.Count to count matches.
  6. Make the search case-insensitive if needed. The fourth argument controls comparer behavior. Use Comparer.OrdinalIgnoreCase to ignore case: . Without it, "Pro" will not match "pro".
  7. Handle the -1 result. Wrap the call in an if expression, for example . This avoids treating -1 as a valid position when you slice text with Text.Middle or Text.Range.
  8. Click OK and review the column. Power Query adds the step and shows the new column. Scroll through the preview to confirm that found values show a number and missing values show -1.
  9. Change the column type. By default the new column may be typed as Any. Select the column header and set it to Whole Number so downstream steps and relationships behave predictably.
  10. Apply and close. Use Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to write the results back to your workbook or model.

Common Mistakes

  • Assuming 1-based indexing. Text.PositionOf is zero-based, unlike Excel's FIND and SEARCH functions. A match at the start returns 0, and 0 is not the same as "not found".
  • Forgetting case sensitivity. The default comparison is case-sensitive. If your data has mixed casing, pass Comparer.OrdinalIgnoreCase or normalise the text with Text.Lower first.
  • Treating -1 as a position. Passing -1 into Text.Middle or Text.Range produces an error or unexpected output. Always test for -1 before using the result.
  • Using Occurrence.All and expecting a single number. That option returns a list. If you need a count, wrap it in List.Count; if you need the first hit, use Occurrence.First.
  • Searching a null value. If the source cell is null, the function errors. Guard with if [Column] = null then null else Text.PositionOf(...).
  • Mixing up the argument order. The signature is Text.PositionOf(text, substring, optional occurrence, optional comparer). Putting the comparer in the third slot will fail.

Tips

  • Use Text.PositionOf as a quick existence check: Text.PositionOf([Email], "@") <> -1 is a compact way to flag malformed addresses.
  • Combine it with Text.Start or Text.Middle to extract everything before or after a delimiter, such as splitting a full name at the space character.
  • For counting occurrences, List.Count(Text.PositionOf([Text], "x", Occurrence.All)) is simpler than looping.
  • If you need the position in a 1-based system (for example, to match Excel's FIND), add 1 to the result, but only after checking for -1.
  • Keep the comparer consistent across related columns so filters and merges behave the same way.

Troubleshooting

If the formula returns -1 for text you can see: check for leading or trailing spaces with Text.Trim, confirm the case matches or add Comparer.OrdinalIgnoreCase, and verify you are searching the right column reference.

If you get an error instead of a number: the source value is likely null or not text. Use the Value.Is or Text.From checks, or wrap the call in try ... otherwise null to isolate the row.

If the custom column dialog rejects the formula: re-check the argument order and that Occurrence.First, Occurrence.Last, and Occurrence.All are spelled exactly. Comparer names are also case-sensitive in M.

If the result looks off by one: remember zero-based indexing. A match at the very start is 0, not 1.

If the column type changes unexpectedly: set it manually to Whole Number after adding the step.

Frequently Asked Questions

Is Text.PositionOf case-sensitive?

Yes, by default. Pass Comparer.OrdinalIgnoreCase as the fourth argument to make it case-insensitive.

What does Text.PositionOf return when the substring is missing?

It returns -1. Test for that value before using the result in other text functions.

Can I find the last occurrence?

Yes. Use Occurrence.Last as the third argument.

Can I get every position of a repeated substring?

Yes. Use Occurrence.All, which returns a list of positions. Wrap it in List.Count to count matches.

Does Text.PositionOf work on numbers?

It expects text. Convert the value with Text.From first, or search a text column instead.

Why do I get 0 instead of 1 for a match at the start?

Positions are zero-based. Add 1 if you need a 1-based position, but only after confirming the result is not -1.