Purpose: use a dynamic file path for a Power Query or Power BI data source, taken from a cell in the current workbook.
Links to topics on this page
- Scenario – why use dynamic file paths in Excel Power Query?
- Overview of steps to create dynamic Power Query data source
- Name cells and create named ranges in Excel
- Create Power Query objects to get dynamic file source
- Manual option to edit the source file path for a power query data connection
- More help with Power Query
Scenario:
- We have a batch of data files in a folder. Each month, we create a new month’s folder, changing the source file path.
- An Excel workbook has separate Power Query data connections to each file.
- We enter the file path and file names into another worksheet in the workbook, creating dynamic file paths for the Power Query connections.
This page shows how to connect to a csv file. See our related post for linking to an Excel workbook in Power Query.
Overview of steps
- Enter file path and file names into cells in a separate worksheet in the Excel workbook;
- Name the cells (click in the cell, and add a cell label into the name box);
- Create custom objects at the start of the Power Query which define the file paths using the named cell references, using the Advanced Editor;
- Update the file path and file name details in the named cells to refresh the Power Query data source.
Name cells and create named ranges in Excel
To name a cell or range of cells:
- Go to the worksheet where you are going to save the custom file path and file name(s).
- Enter your file path into a cell, repeat for additional file paths.
- Enter your file name into a cell, repeat for additional file names.
- Click in each cell, then click in the ‘Name box’ (top left box above the worksheet grid).
- Enter the name you want to refer to the cell by, then press Enter.
- Your name reference replaces the default cell reference.
- Note, your cursor will return to the highlighted cell.
- To create a named range of cells, select a range of cells, and then name the range (not applicable to this method for creating dynamic file paths in Power Query).
Go to a named cell or range in Excel
To go directly to any named cell or range in an Excel workbook, click in the ‘Name box’, enter the name, and press Enter. The target worksheet is displayed and the named cell or range is highlighted.
Create Power Query objects to get file paths from cells in the current workbook
Note that Power Query doesn’t provide a built-in way to get the path for the current workbook, so another method is required. So, we create custom Power Query objects to link to dynamic file paths and file names.
- First, enter your file path and file name details into a worksheet in your Excel workbook, as described above.
- Open a blank query, open the Advanced Editor window, and
- Enter the three lines of code shown below, under the default ‘let’ section:
The three lines of code explained:
Power Query: create the object to define the FILE PATH
FilePath = Excel.CurrentWorkbook(){[Name=”FilePath”]}[Content]{0}[Column1],
- In our example above:
FilePath=
creates an object named ‘FilePath’;Name="FilePath"
is referring to the cell in the worksheet that we have named ‘FilePath’.
Power Query: create the object to define the FILE NAME
File1 = Excel.CurrentWorkbook(){[Name=”File1″]}[Content]{0}[Column1],
- In the example above:
- ‘File1’ is the name of our custom Power Query object, and is the named cell in the worksheet containing the file path and file name
- Excel.CurrentWorkbook() is the Excel function which points to contents within the workbook.
Power Query: define the DATA SOURCE as a combination of the file path and file name
Source = Csv.Document(File.Contents(FilePath & File1))
- In our example, we’re importing data from a csv file, so we use the Csv.Document() function, see more details from Microsoft help here.
- In the File.Contents() function we add our custom ‘FilePath’ and ‘File1’ objects created above.
We can now set up our Power Query data manipulation steps as usual.
When the source data changes, we can update the file path and file name details in the worksheet, and refresh the Power Query.
Manual option to edit the source file path for a power query data connection
Just in case you don’t fancy using dynamic paths 🙂 it’s pretty easy to manually amend the source reference in a Power Query.
In the Power Query window, click on the settings wheel next to the ‘Source’ step in the Applied Steps pane. You can edit the file path directly, or click on the ‘Browse’ button to navigate to the file.
Alternatively, if you click on the actual step name ‘Source’ (not the settings wheel), you can edit the file path from the step editor box at the top of the window.
Related posts
- Get started with Power Query – connect to data and add an Index column
- Power Query dynamic Excel worksheet name
- Power Query – concatenate text and numeric data
- Power Query IF ELSE and IF AND statements
- Python relative import and dynamic file paths
- Importing data – absolute and relative file paths in R