How to Combine Multiple Power Query Text Transformations into One Function
Question details
The user wants to combine several text operations into a single reusable custom function in Power Query to efficiently clean text values.

- Product
- Microsoft Excel / Power Query
- Device & OS
- not provided
- Scenario
- Cleaning text values in a dataset where multiple specific text manipulation steps are required repeatedly.
- Observed behavior
- The goal state is a functional custom M query function that applies all text transformation steps in sequence and can be invoked easily on any column.
Ensure you have your workbook open in Excel and the Power Query Editor launched. You will need to use the Advanced Editor to write and test your custom M code.
Create a Reusable Custom Function in Power Query
By using the Advanced Editor, you can define a custom function that processes text step-by-step and can be invoked on any column in your dataset.
Power Query allows you to write custom functions using the M formula language. By chaining operations using a let...in block, you can execute multiple text transformations sequentially without cluttering your main query with repetitive steps.
In the Power Query Editor, right-click in the Queries pane on the left side, select 'New Query', and then click 'Blank Query'. Navigate to the Home tab and click 'Advanced Editor'.
In the Advanced Editor window, delete the default code and paste the following M code: (t as text) => let Step1 = Text.Remove(t,{"0".."9","#"}), Step2 = if Text.StartsWith(Step1,";") then Text.AfterDelimiter(Step1,";") else Step1, Step3 = Text.Replace(Step2,";;",";") in Step3
Click 'Done' to close the Advanced Editor. In the Query Settings pane on the right, rename the query to 'fnCleanText'. You will now see an 'fx' icon next to it indicating it is a custom function.
Go to the query containing your dataset. Click on 'Add Column' in the top ribbon and select 'Invoke Custom Function'. Choose 'fnCleanText' from the Function query dropdown and select the column you wish to clean as the input argument.

Try WPS Office for Your Spreadsheet Data Needs
While Power Query (M code) is an advanced tool specific to Microsoft Excel and Power BI, WPS Office Spreadsheets offers a lightweight, highly compatible, and free alternative for standard data cleaning using built-in formulas like SUBSTITUTE, REPLACE, and TRIM. It perfectly opens and edits Microsoft Excel files without altering your layout.
- 1. Download and Install WPS Office: Visit the official WPS Office website to download the free application and install it on your device.
- 2. Open Your Excel Workbook: Launch WPS Spreadsheets and click 'Open' to browse and open your existing .xlsx or .csv data files.
- 3. Apply Built-In Text Functions: Use standard spreadsheet functions like SUBSTITUTE and TRIM directly in your worksheet to clean and transform your data without needing to write custom M code.

Frequently Asked Questions
How do I apply a custom Power Query function to an entire column?
Go to the 'Add Column' tab in the Power Query Editor, click on 'Invoke Custom Function', select your created function from the dropdown list, and choose the target column as the input parameter.
Why is my custom text function returning an error on empty cells?
If your target column contains null values, an M function expecting a text argument will return an error. You can fix this by adding a null check in your function code, such as: if t = null then null else ...
Can I edit the custom function after I create it?
Yes. Select the function (e.g., fnCleanText) in the Queries pane, then click 'Advanced Editor' on the Home tab. You can safely modify the transformation logic, and any column invoking this function will update automatically.




