Power Query doesn't natively support regular expressions, but you can use M language functions like Text.Select, Text.Replace, and custom functions to achieve regex-like text extraction and cleaning. This approach lets you isolate phone numbers, emails, dates, or structured text from messy data without manual editing—ideal for data cleansing in Excel or Power BI.

What You'll Need

  • Microsoft Excel 2016 or later, or Power BI Desktop
  • A data source with unstructured text (e.g., customer comments, log entries, CSV exports)
  • Basic familiarity with Power Query Editor (Access > Get Data > From Table/Range)

Step-by-Step

  1. Open your data source in Excel or Power BI and load it into Power Query Editor by selecting your table or range, then choosing Data > From Table/Range (Excel) or Get Data > From Table/Range (Power BI).
  2. In Power Query Editor, identify the column containing the unstructured text you want to clean—such as a column named "Notes" or "Description." Click on the column header to select it.
  3. Click Transform > Add Column > Custom Column. In the dialog, name your new column (e.g., "ExtractedEmails"). In the formula bar, enter: Text.Select([YourColumnName], {"A".."Z", "a".."z", "0".."9", "@", ".", "-"}). Replace "YourColumnName" with the actual name of your text column. This filters only alphanumeric characters and common email symbols.
  4. To extract email addresses more precisely, use a custom function. Click Advanced Editor in the Home tab. Paste this function definition above the existing code: let ExtractEmails = (input as text) as text => let parts = Text.Split(input, " "), emails = List.Select(parts, each Text.Contains(_, "@") and Text.Contains(_, ".") and Text.Length(_) > 5), result = Text.Combine(emails, ",") in result in ExtractEmails. Click Done.
  5. Back in the query editor, go to Add Column > Custom Column again. Name it "CleanedEmails" and use the formula: ExtractEmails([YourColumnName]). This applies the function to each row.
  6. To remove unwanted characters like parentheses or brackets, add another custom column: Text.Replace(Text.Replace([YourColumnName], "(", ""), ")", ""). Chain additional Text.Replace calls for other symbols.
  7. To extract phone numbers in format (XXX) XXX-XXXX, use: Text.Select([YourColumnName], {"0".."9", "(", ")", "-", " "}) then use Text.Replace to standardize spacing and dashes.
  8. Right-click on any intermediate columns you no longer need (e.g., the raw text or temporary extraction columns) and choose Remove Columns.
  9. Click Close & Load to push the cleaned data back to Excel or Power BI. Verify results in a sample of 5–10 rows before applying to the full dataset.

Common Mistakes

  • Using Text.Select with invalid character ranges—for example, including forward slashes without escaping them in the list, which causes syntax errors.
  • Assuming Text.Select removes all non-alphanumeric characters; it only filters, so you must manually exclude unwanted symbols like commas or semicolons.
  • Forgetting to replace the column name placeholder in custom functions, resulting in "Expression.Error: The name 'YourColumnName' was not recognized."
  • Trying to match complex patterns like US zip codes with a single Text.Select; this requires multiple steps or a more advanced custom function with List.Transform and Text.Contains.
  • Applying transformations before removing null or blank rows—this can cause errors when Text.Select tries to process null values.

Tips

  • Always test your regex-like logic on a small sample first—copy 10 rows to a new query to avoid corrupting your original data.
  • Use Text.Trim before any extraction to remove leading/trailing spaces that break pattern matching.
  • For date extraction, combine Text.Select with DateTime.FromText to convert cleaned strings like "2023-12-01" into actual date values.
  • Store reusable functions in a separate query (e.g., "RegexHelpers") and reference them across multiple queries for consistency.
  • When extracting multiple values per row (e.g., multiple emails), use List.Generate to loop through text and return a list, then expand it into separate rows using Transform > Expand to New Rows.
  • Use the "View" > "Formula Bar" option to see real-time feedback as you type M code—this helps catch syntax errors before applying them.

Troubleshooting

  • If you get "Expression.Error: We cannot apply operator < to types Text and Number", check that your column is set to Text type. Right-click the column header > Change Type > Text.
  • If your custom function returns blank, verify the input text contains the expected characters. Add a condition: if Text.Length([YourColumnName]) > 0 then ExtractEmails([YourColumnName]) else null.
  • If Power Query crashes during loading, reduce the dataset size temporarily. Large text fields with complex transformations can overwhelm memory.
  • If the column doesn't appear after applying a custom function, refresh the query (Home > Refresh) and check if the column was renamed or deleted accidentally.
  • When Text.Replace doesn’t remove a character, check for invisible Unicode characters like non-breaking spaces (U+00A0). Use Text.Replace([Column], " ", "") and also try Text.Replace([Column], Character.FromNumber(160), "").

Frequently Asked Questions

Can I use actual regex syntax like \d+ in Power Query?

No. Power Query uses M language, not JavaScript or Python regex. You must simulate regex behavior using combinations of Text.Select, Text.Replace, List functions, and custom logic.

How do I extract multiple phone numbers from one cell?

Use Text.Split to break the cell into words, then use List.Select to filter pieces that look like phone numbers (e.g., contain 10 digits and hyphens or parentheses), then Text.Combine them into a comma-separated string.

Why does my custom function work on some rows but not others?

This usually happens when some rows contain null, blank, or non-text values. Wrap your function call in an if-statement: if [Column] is text then YourFunction([Column]) else null.

Can I extract HTML tags using this method?

Yes, but only simple ones. Use Text.Replace to remove <div>, <p>, etc., by targeting the exact text strings. Complex HTML parsing requires external tools or APIs.

Is there a performance penalty for using custom functions?

Yes. Each custom function adds overhead, especially on large datasets. Use them sparingly and avoid nesting multiple functions in a single step. Prefer built-in functions like Text.Select where possible.

What if I need to match case-insensitive patterns?

Power Query is case-sensitive. Convert text to lowercase first: Text.Lower([Column]), then apply your extraction logic. To preserve original case, keep a copy of the original column.