Change the Power Query Source Based on a Cell Value

Jul 4, 2020 • 4 min read

If you’re like me, you build Power Query solutions in a test environment first. Then, when everything is ready, you move the workbook into its final location. That usually means updating the paths to the source files.

Or perhaps you create workbooks for clients. Rather than giving them complicated instructions for editing a query, wouldn’t it be better if they could change a cell and refresh the data?

We can link Power Query’s source file to worksheet cells using two named ranges and a few short lines of M code. Once that connection is in place, changing the source location no longer requires opening the Power Query Editor.

The Starting Point

For this example, we already have a query that imports data from an Excel file, applies transformations, and loads the result into a worksheet starting at cell B6. The source file’s full path is currently hard-coded in the query.

Above the results, we have two cells containing the source details:

  • Cell C2: The file name, including its extension.
  • Cell C3: The folder path where the file is stored.

At the moment, these cells are just text. They are not connected to Power Query. We’re going to make them control which file the query imports.

Excel worksheet with file name and file path cells outlined above a green results table

This method assumes you already have your data loaded into Power Query. If you want to follow the same example, you can download the example file. Remember to change the file name and folder path to locations that exist on your own PC.

1. Create the Named Ranges

First, we need to give each input cell a name so Power Query can identify it.

  1. Select cell C2, which contains the file name.
  2. Click the Name Box to the left of the formula bar.
  3. Type FileName and press Enter.
  4. Select cell C3, which contains the folder path.
  5. Click the Name Box, type FilePath, and press Enter.

We will combine these two values in Power Query, so they must form a valid full file path when joined together.

Make sure the folder path ends with a backslash, \. It’s an easy detail to miss. Our code will join the folder path and file name exactly as they appear in the cells; it will not add the separator for us.

2. Open the Query’s Advanced Editor

Now we can connect the named ranges to the query.

  1. In Excel, select Data > Queries & Connections if the pane is not already visible.
  2. Double-click the existing query to open the Power Query Editor.
  3. Select View > Advanced Editor.

The Advanced Editor displays the M code behind all the query’s transformations. Near the beginning, the Source step contains the hard-coded location of the current source file.

Editing M code might seem a little daunting, but we only need to add two lines and replace the file path. The existing transformations can stay as they are.

3. Read the Cell Values into Power Query

Immediately after the opening let, add these two lines:

FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
FileName = Excel.CurrentWorkbook(){[Name="FileName"]}[Content]{0}[Column1],

These lines retrieve the text from our two named ranges and assign it to the M steps FilePath and FileName.

How the code works

The expression is short, but each part has a specific job:

  • Excel.CurrentWorkbook() provides access to items in the current workbook, including our named ranges.
  • {[Name="FilePath"]} selects the item named FilePath.
  • [Content] retrieves the data held in that named range.
  • {0} selects its first row. Power Query starts counting at zero, so the first row is row 0.
  • [Column1] retrieves the value from the first column. Our single-cell named range has no header, so Power Query uses the default column name Column1.

The second line follows exactly the same pattern, but reads the FileName named range instead.

Keep the comma at the end of each added line. These steps come before the existing Source step, and the commas separate them.

A useful time-saver is to write the first line, copy it, and change both the step name and the quoted named-range reference for the second line. Just remember to change both occurrences.

4. Replace the Hard-Coded Source Path

In the existing Source step, locate the quoted full file path inside File.Contents(...). Replace that quoted path with:

FilePath & FileName

The file-reading part of the source expression should now look like this:

File.Contents(FilePath & FileName)

The ampersand joins the folder path and file name into one text value. Notice that FilePath and FileName are not in quotation marks here: they refer to the steps we just created, rather than literal text.

Only replace the hard-coded path. Keep the surrounding source function, its other arguments, and all subsequent transformation steps unchanged.

Check the bottom of the Advanced Editor for the message confirming that no syntax errors have been detected. Then click Done, followed by Home > Close & Load.

5. Change the Source Cells and Refresh

Assuming everything has been entered correctly, the query now gets its source location from cells C2 and C3.

To test it, I have another file in the same folder named AnotherSourceFile.xlsx. Because the folder has not changed, I only need to update the file name.

  1. Change C2 to AnotherSourceFile.xlsx.
  2. Leave C3 unchanged if the new file is in the same folder, or update it to the new folder path.
  3. Select Data > Refresh All.
Excel worksheet showing AnotherSourceFile.xlsx in the file name cell and an outlined green results table
After refreshing, the results come from the file specified in the worksheet cells.

The results table updates using the new source file. Changing the cells selects the source; refreshing runs the query with those values. Editing a cell alone does not refresh the results.

A Simpler Way to Hand Over a Workbook

That’s all it takes: two named ranges, two short M steps, and a small change to the source expression.

Instead of asking someone to find and edit a file path inside Power Query, we can give them a much simpler instruction: update the file name or folder path in the worksheet, then refresh.

It makes moving a solution from testing into everyday use much easier. More importantly, anyone using the workbook can point it to the correct source location without needing to understand how Power Query works.

Share

Excel Off The Grid

I help you reduce repetitive Excel work with practical guides to formulas, Power Query, reporting, and automation.

© 2026 Excel Off The Grid
Powered by Convella