logo
search
Power Query Problems

How to Reference the Current Query Name in Excel Power Query M

Huma Ashraf ChHuma Ashraf Ch Oct 21, 2026 868 views

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.

How to Reference the Current Query Name in Excel Power Query M
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 you start

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.

Solution 1Recommended

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.

1
Create a Named Range in Excel

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.

2
Import the Named Range into Power Query

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.

3
Convert the Query to a Text Value

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.

4
Reference the Value in Your M Code

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.

Use an External Parameter or Named Range
Advanced Implementations: For complex setups with multiple queries, consider creating a centralized Configuration Table in Excel with 'Key' and 'Value' columns, then creating a custom M function to fetch the specific query name based on the key.
Free Microsoft Office alternative

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.

Fully compatible with Microsoft Excel (.xlsx, .xls, .csv) formats.Robust data processing and pivot table features without complex coding.Lightweight installation that runs smoothly on older or budget devices.Familiar user interface ensuring zero learning curve.
microsoft office alternative - wps office

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.