Sub topic 14
Making the folder path easy to change
This chapter assumes you can already connect to a folder, filter the file list, and use Combine & Transform Data. You should also be comfortable opening the Power Query editor and the Advanced Editor. Here you will replace the hard-coded folder path with a parameter. That lets you point the same query at a different folder without editing code.
Why a hard-coded path causes trouble
A folder query starts with a line like this:
Source = Folder.Files("C:\Data\Sales")The path is a text value buried inside the query. That causes problems in a few common situations:
- A colleague opens your workbook, and their data lives at
D:\Reports\Salesor on a different network share. - You keep one folder per year and want to switch from 2025 to 2026.
- You move the workbook from a test folder to the production folder.
Each case means opening the editor, finding the right step, and retyping the path. A parameter moves that value to one named place that is easy to find and edit.
What a parameter is
A parameter is a named value stored as its own query. Other queries can refer to it by name. It is a query that returns a single value, such as a piece of text, instead of a table.
You may remember that Combine & Transform Data creates a query named after the folder, such as Parameter1. That query holds the sample file and is not a path parameter. The one you create in this chapter is separate, and it holds the folder path.
Creating a folder path parameter
- On the Data tab, choose Get Data, then Launch Power Query Editor.
- In the editor, on the Home tab, choose Manage Parameters, then New Parameter.
- Set Name to
FolderPath. - Set Type to Text.
- Set Suggested Values to Any value.
- Set Current Value to the full path, for example
C:\Data\Sales. - Choose OK.
A new query named FolderPath appears in the Queries pane. Its icon differs from a table query. Its preview shows only the text you entered.
Do not add a trailing backslash unless you need one. Folder.Files accepts both forms, but a consistent habit helps when you later build paths by joining text.
Using the parameter in the query
Open the query that connects to the folder. Select the Source step and look at the formula bar. It reads:
= Folder.Files("C:\Data\Sales")Replace the quoted text with the parameter name, without quotes:
= Folder.Files(FolderPath)The preview should look the same as before, because the parameter still holds the same path. The query now reads its location from FolderPath.
The complete query, with the filter and column selection from earlier chapters, looks like this:
let
Source = Folder.Files(FolderPath),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Kept = Table.SelectColumns(CsvOnly, {"Name", "Content"})
in
KeptQuotes matter here. "FolderPath" in quotes is a literal piece of text, and Folder.Files would try to open a folder with that name. Without quotes, Power Query looks up the query called FolderPath and uses its value.
Changing the path later
There are two places to change the value.
- In the Power Query editor, select the
FolderPathquery, then edit the Current Value in Manage Parameters. This is the method to use if the editor is already open. - In Excel, on the Data tab, choose Queries & Connections. Right-click the
FolderPathquery and choose Edit. This opens the editor focused on the parameter.
After changing the value, refresh the main query. The new folder's files are read and combined.
Parameterizing a query that Excel generated
If you used Combine & Transform Data, the generated queries include a main query for the folder. Its Source step contains the hard-coded path in the same form. You can edit that step in the same way:
- Select the main combined query in the Queries pane.
- Select the Source step.
- Replace the quoted path in the formula bar with
FolderPath.
You do not need to change the sample file query, the function query, or the helper parameter. They work from the file list that the main query passes along. Only the main query needs to know where the folder is.
After the edit, check the Applied Steps list for errors. A red message on a later step usually means the new folder's files differ from the old ones. The chapter on why files must share a structure covers that.
Keeping the parameter in a cell
Some users prefer to keep the path in a worksheet cell instead of in the editor. This is practical when the people who change the path never open Power Query. The approach reads a one-cell table and uses its value as the path.
- In Excel, type a label such as
Folder pathin cell A1 and the path in cell B1. - Select B1 and choose Insert, then Table. Turn off "My table has headers" is not needed. Instead, use a two-cell range: put the header
Pathin A1 and the folder in A2, then select A1:A2 and create a table with headers. Name the tableSettingson the Table Design tab. - Choose Data, then From Table/Range with the table selected. A query named
Settingsopens in the editor. - Close the editor with Close & Load To, and choose Only Create Connection.
Now create a new query that returns the path as a text value. In the editor, choose Home, New Source, Other Sources, Blank Query, and open the Advanced Editor. Replace the contents with this code:
let
Source = Excel.CurrentWorkbook(){[Name = "Settings"]}[Content],
PathValue = Source{0}[Path]
in
PathValueName this query FolderPath. It returns the text in the first row of the Path column. Use FolderPath in Folder.Files exactly as before. To change folders, type a new path in the worksheet cell and refresh.
Because Excel.CurrentWorkbook reads the table from the open workbook, this works with any path typed into the cell, including a path on a network drive.
Building a path from parts
A single parameter can also be combined with other text. For example, you may keep a root folder in FolderPath and add a year folder:
let
Root = FolderPath,
YearFolder = Root & "\2026",
Source = Folder.Files(YearFolder)
in
SourceHere & joins two pieces of text. If the root ends in a backslash, the joined text would contain two backslashes, which is not a valid path in most cases. Pick one rule, such as "the root never ends with a backslash", and apply it everywhere.
For a second parameter, repeat the creation steps with a name such as YearName and a Text type. Then build the path with FolderPath & "\" & YearName.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Expression.Error: The name 'FolderPath' wasn't recognized` | The parameter is missing, renamed, or spelled with different capitalization | Check the Queries pane. M names are case sensitive. |
| `DataSource.Error: Could not find a part of the path` | The path has a typo or the folder does not exist for this user | Open the path in File Explorer to confirm it. |
| The preview shows folder names as text instead of a file list | The formula has quotes around `FolderPath` | Remove the quotes. |
| The query returns no rows | The folder is empty, or the filter removes every file | Check the file list before the filter step. |
| Refresh asks for credentials | The new folder is a different data source | Enter credentials, or use Data Source Settings to edit permissions. |
| The path from the cell has no effect after editing | The workbook was not refreshed | Choose Data, then Refresh All. |
Changing the path may also trigger a privacy-level prompt on first refresh. Power Query treats each distinct source as separate and may ask how to combine them. For local or shared folders you control, choose the Organizational or Private level.
Quick check
After you make the change, confirm that it works:
- Create a second folder with one or two sample files that match your layout.
- Change the
FolderPathvalue to the second folder. - Refresh the query and confirm that the loaded table now shows the rows from the second folder.
- Change the value back and refresh again to confirm the original data returns.
Exercise
Your query reads Folder.Files("C:\Data\Sales"). You created a text parameter named FolderPath. Write the new Source step, and explain what happens if you write Folder.Files("FolderPath") instead.
Show answer
The Source step is Source = Folder.Files(FolderPath). With no quotes, Power Query looks up the query named FolderPath and uses its text value. With quotes, "FolderPath" is just a text literal, so Folder.Files looks for a folder literally named FolderPath. That usually fails with an error that the path could not be found.
Exercise
A root folder is stored in FolderPath as C:\Data and you want to read the subfolder C:\Data\2026. Write an M expression that builds this path from the parameter, and say which mistake would produce a double backslash.
Show answer
Use Folder.Files(FolderPath & "\2026"). The text joins to C:\Data\2026. A double backslash appears if the root value already ends in a backslash, for example C:\Data\, because the joined text becomes C:\Data\\2026. Choose one convention, such as no trailing backslash on the root, and keep to it.