Sub topic 11
Replacing hard-coded paths with parameters
This chapter assumes you can open Power Query Editor, read the Applied Steps pane, and have already connected to a CSV or Excel file. You should know what a query is and how a step refers to the step before it.
Why hard-coded paths cause trouble
When you connect to a file, Power Query writes the full path into the first step. For example, C:\Data\orders.csv is stored as text inside File.Contents. This works on your machine. It fails when a colleague opens the report, when the file moves, or when you switch from a test folder to a production folder.
A parameter is a named value that queries can read. You define it once and refer to it by name. To change the location, you edit one value and leave the query steps alone.
Create a text parameter
- In Power BI Desktop, select Transform data to open Power Query Editor.
- Select Manage Parameters, then New Parameter.
- Set Name to
FolderPath. - Set Type to Text.
- Set Suggested Values to Any value.
- Enter the Current Value, for example
C:\Data\. Keep the trailing backslash, because the query below joins the file name directly onto it. - Select OK.
The parameter now appears in the Queries pane as its own item. It is not loaded into the report as a table.
Use the parameter in a query
Open the Advanced Editor for your CSV query and replace the quoted path with the parameter name.
let
Source = Csv.Document(File.Contents(FolderPath & "orders.csv"), [Delimiter = ",", Encoding = 65001]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true])
in
PromotedThe & operator joins two pieces of text. If FolderPath is C:\Data\, the result is C:\Data\orders.csv. Every query that reads from this folder can use the same parameter, so one edit moves them all.
Parameters for site URLs
The same idea works for SharePoint Online. Create a text parameter named SiteUrl with a value such as https://contoso.sharepoint.com/sites/Sales. Then pass it to the connector in place of the quoted URL, for example SharePoint.Files(SiteUrl, [ApiVersion = 15]). Moving from a test site to the live site then means changing one value.
Change the value later
You can change a parameter in two places:
- In Power Query Editor, select the parameter in the Queries pane and edit Current Value.
- In Power BI Desktop, choose Transform data, then Edit parameters, and enter a new value. Power BI prompts you to apply changes and refreshes the data.
After the change, refresh the report. If the new location needs different sign-in details, Power BI asks for credentials for that source. The next chapter covers credentials in detail.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Expression.Error: The name 'FolderPath' wasn't recognized` | The parameter name is misspelled. Names are case-sensitive. | Match the name exactly as shown in the Queries pane. |
| `DataSource.Error: Could not find a part of the path` | The value is missing a backslash, or the folder does not exist. | Check the joined path. |
| The query asks for credentials again | The parameter changed the data source. | Enter credentials for the new location. |
Exercise
Your query uses File.Contents(FolderPath & "sales.csv"). The parameter value is D:\Reports, and the refresh fails with a path error. What is wrong, and what are two ways to fix it?
Show answer
The joined path is D:\Reportssales.csv, because the value has no trailing backslash. Fix it by changing the parameter value to D:\Reports\, or by changing the query to FolderPath & "\sales.csv".
Exercise
You publish a report that uses a SiteUrl parameter and want to point it at the production site. Where in Power BI Desktop do you change the value, and what should you expect after you do?
Show answer
Use Transform data, then Edit parameters, and enter the new URL. Power BI applies the change and refreshes the data. It may ask for credentials for the new site.