You’ve built some queries in a test environment, and now it’s time to start using them for real. How can you change the queries to point to the live source, rather than the test? In this post, we will answer this very question.
As like most things in Excel, there are multiple ways to achieve this:
- Change the location in the query M code
- Use the Data source settings
- Using parameters
Without further ado, let’s get started.
Change the source location in the query
The first applied step in most queries is to identify the source data. Technically, it doesn’t have to be, and it doesn’t have to be called Source either. However, in most cases, it will be. Being first makes the M code easy to find; as it’s always at the start 😉
The screenshot below shows the first step in the query with the M code displayed in the Formula Bar. If the Formula Bar is not visible you can click View -> Formula Bar in the Power Query menu.
We can edit the code directly in the Formula Bar, we just need to provide the new file location as a text string.
We also have the option of editing the code in the Advanced Editor, click Home -> Advanced Editor or View -> Advanced Editor (see the screenshot below).
Thirdly, clicking on the gear icon next to the Source step within the Applied Steps pane will open the source dialog. In this example, an Excel Workbook is used as the source, so click Browse to navigate to the new file location, then click OK.
Depending on the source type, the dialog box will change, but the principles are the same.
We’ve seen the technique of changing the Source already in this Power Query series when we imported from the web.
Use the Data Source settings
Where there are multiple queries all coming from the same source, there is a better way to repoint to the source data; the Data Source settings. In fact, this may be a better way in general.
In the Power Query editor click Home -> Data Source settings
In the Data Source settings window, select the source we want to change, then click the Change Source... button.
This will bring up the same window as clicking the gear icon (as we saw earlier).
However, there is a crucial difference; rather than changing the source for one single query, it changes the source for all the queries using that as the source.
The Data Source settings are available in other locations in Excel and Power Query, but I find this the easiest to remember.
If you know the source data location will be changing, then a good option is to create a Parameter. A cell on the worksheet can contain a text string with the location of the data source. This text string could be the folder path, file name, table name or a SharePoint web address, etc. To repoint the query it is as easy as changing the text string.
We’ve seen many times that Power Query hardcodes text values into the M code, so using parameters is a good flexible approach. As we have already covered Power Query parameters in another post, I won’t repeat the process here, go read the post.
- Import Data
- Data Refresh
- Edit Queries
- Close & Load Options
- Using Parameters
- Basic Transformations
- Combine / Append Queries
- Import All Files in a Folder
- List All the Files in a Folder and File Attributes
- Import Data from the Current Workbook
- Import Data from the Web
- Unpivot Data
- Unstacking Data in a Column
- Lookup Values using Merge
- Change the Source Data Location
- If Statements for Conditional Logic