How to Reference the Current Query Name in Excel Power Query M
Question details
The user needs a method to dynamically reference the name of the currently evaluating query within a Power Query M script to avoid hardcoding and improve maintainability.

- Product
- Microsoft Excel Power Query
- Device & OS
- not provided
- Scenario
- Writing and managing advanced data transformation scripts in the Power Query Advanced Editor.
- Observed behavior
- Power Query M lacks a native, built-in function to dynamically return the name of the query currently being executed, forcing users to type the query name manually.
Before editing your M code, ensure you have access to the Advanced Editor in Power Query and have saved a backup of your current query steps.
Use an External Parameter or Named Range
Since there is no built-in M function to fetch the current query name dynamically, the most reliable workaround is to store the query name in a parameter or an Excel named range, and then reference that value inside your M script.
By defining the query name externally, you separate the configuration from the logic. If the query name needs to change, you only update it in one place (the parameter or named range), and the M script will automatically use the updated value.
In your Excel workbook, type your desired query name into an empty cell. Select that cell, click into the Name Box (next to the formula bar), and type a name like 'ConfigQueryName', then press Enter.
Navigate to the 'Data' tab on the Excel ribbon and click 'From Table/Range'. This will open the Power Query Editor and load your named range as a new query.
In the Power Query Editor, right-click the specific cell containing your query name text and select 'Drill Down'. This converts the query from a table into a single scalar text value.
Open the Advanced Editor for your main query. Wherever you previously hardcoded the query name, replace the text string with the name of the scalar query you created in the previous step.

Streamline Your Data Analysis with WPS Office
While Power Query M scripting is heavily specific to Microsoft Excel, WPS Office provides a powerful, free, and lightweight alternative for your everyday data analysis and spreadsheet needs. Enjoy high compatibility with Excel files and a familiar interface without the heavy subscription costs.

Frequently Asked Questions
Can I use Record.FieldNames to find the current query name?
No, Power Query does not expose the current environment's executing query name directly through standard record functions during evaluation. You must pass the name explicitly via variables or parameters.
How do I create a parameter directly in Power Query?
In the Power Query Editor, go to the 'Home' tab, click 'Manage Parameters', and select 'New Parameter'. You can then give it a name (e.g., CurrentQuery), set the type to 'Text', and enter your query name as the Current Value.
Why is hardcoding query names in M code problematic?
Hardcoding query names makes your scripts brittle. If a user renames the query in the Queries pane, the hardcoded text string inside the Advanced Editor will not automatically update, causing subsequent transformation steps to break.




