Power Query automation lets you refresh, transform, and combine data from multiple sources without manually repeating the same steps in Excel or Power BI. You set up a query once, and then a single click — or a scheduled refresh — re-runs the entire pipeline, pulling fresh data and applying all your transformations automatically. This guide walks you through building a reusable query, scheduling refreshes, and handling the common pitfalls that trip people up.
What You'll Need
- Excel 2016 or later (Windows) or Power BI Desktop. The steps below focus on Excel, but the Power Query editor is nearly identical in Power BI.
- A data source you can connect to repeatedly: a folder of CSV files, an Excel workbook, a SQL database, or a web page.
- For scheduled refresh in Power BI: a Power BI Pro or Premium license, and the data source must be reachable from the cloud service.
- For Excel: a Microsoft 365 subscription if you want to use the built-in refresh from a file stored in OneDrive or SharePoint. Standalone Excel 2016/2019 can refresh manually but cannot schedule without third-party tools.
Step-by-Step
1. Build Your Query Once
- Open Excel and go to the Data tab. Click Get Data and choose your source type. For a folder of files, select From File > From Folder. For a database, pick From Database and your engine.
- In the connection dialog, browse to your source. If you chose a folder, Excel shows a preview of all files. Click Combine > Combine and Transform Data to load them as one table.
- The Power Query Editor opens. Here you apply all your steps: remove columns, filter rows, rename headers, split columns, change data types, merge queries, or add custom columns. Every action you take is recorded as a step in the Applied Steps pane on the right.
- When done, click Close & Load. Excel loads the result into a worksheet table. This is your first automated pipeline — the query now exists in the workbook.
2. Test the Refresh
- With your query loaded, right-click the table in the worksheet and choose Refresh. Alternatively, go to Data > Refresh All.
- Add a new file to your source folder, or change a value in the source database. Refresh again. The table updates to reflect the new data. If it does not, check the Common Mistakes section below.
3. Make the Query Parameter-Driven (Optional but Powerful)
- In the Power Query Editor, go to Home > Manage Parameters > New Parameter. Name it something like StartDate and set a default value.
- Use that parameter in your query. For example, if you filter a date column, click the filter arrow, choose Custom Filter, and reference the parameter by typing its name in the value box. The editor will recognise it.
- After closing the editor, you can change the parameter value from Excel by going to Data > Queries & Connections, right-clicking the query, and choosing Edit. This lets you reuse the same query for different time ranges without editing steps.
4. Schedule Refresh in Power BI
- Publish your Power BI report to the service. In Power BI Desktop, click Publish and choose a workspace.
- In the Power BI service (app.powerbi.com), go to your dataset. Click the Schedule refresh icon (the circular arrows) under the dataset name.
- Expand Schedule refresh, toggle it to On, and set the frequency (daily, weekly) and time. You can set multiple times per day if your license allows.
- If your data source requires credentials, click Edit credentials and sign in. The service stores these securely and uses them each refresh.
- Click Apply. The dataset now refreshes automatically on the schedule. You can check the Refresh history link to see past runs and any errors.
5. Schedule Refresh in Excel (Manual or via Power Automate)
- Excel itself has no built-in scheduler. For a manual refresh, just press Ctrl+Alt+F5 or use the Refresh button.
- For automation, save your workbook to OneDrive or SharePoint. Then go to Power Automate (flow.microsoft.com) and create a new flow. Use a trigger like Recurrence (set the frequency) and an action called Refresh Excel file from the Excel Online connector. Select your file and the worksheet. This runs the refresh in the cloud, but note it only works for files that use data sources Power Automate can reach (like SharePoint lists, OneDrive files, or databases with a gateway).
- Alternatively, use a simple VBA macro: open the VBA editor (Alt+F11), insert a module, and add the line
ThisWorkbook.RefreshAll. Then use Windows Task Scheduler to open the workbook and run the macro at a set time. This is a local solution and does not require the cloud.
6. Handle Changing File Names and Structures
- If you pull from a folder, Power Query picks up all files present at refresh time. To avoid errors when a file is missing, add a step that filters the file list. In the editor, after the folder step, click the filter arrow on the Name column and uncheck (Select All) then check only the files you expect. Or use a parameter for the file name and filter to that.
- If a column disappears from a source file, Power Query throws an error. To make the query resilient, use Transform > Rename Columns carefully, and consider using Table.SelectColumns with a list of required names. If a column is missing, the query fails — which is often desirable so you notice the problem, rather than silently producing wrong data.
Common Mistakes
- Hardcoded file paths: If you move the source folder, the query breaks. Use relative paths or parameters for the folder location.
- Forgetting to change data types: Power Query often imports everything as text. If you do not set date and number types, sorting and calculations fail later. Set types in the editor.
- Merging queries without expanding: After a merge, you must click the expand icon (double arrow) on the new column to bring in the fields. Skipping this leaves a column of tables.
- Using the same query name twice: If you duplicate a query and forget to rename it, Excel appends a number, which can confuse references.
- Refreshing while the source file is open: If your source is an Excel file that is currently open, Power Query may fail with a file lock. Close the source file before refreshing.
- Ignoring the privacy levels: If you combine data from different sources (e.g., a folder and a database), Power Query asks about privacy levels. Set them to Organizational or Public appropriately, or you may get a warning that data cannot be combined.
Tips
- Keep the Power Query Editor open while you test. Use Home > Refresh Preview to see how your steps behave with new data before loading back to Excel.
- Name your steps in the Applied Steps pane by right-clicking a step and choosing Rename. This makes the query readable and easier to debug.
- Use View > Formula Bar in the editor to see the M code behind each step. Learning basic M (like
Table.SelectRowsandTable.TransformColumnTypes) helps you fix errors faster. - For large data, disable the Data Load > Enable Load option for intermediate queries. Only the final query should load to the sheet. This speeds up refresh.
- Create a documentation query that lists all your queries and their step counts. This is a manual step but saves time when revisiting a workbook months later.
- In Power BI, set the Data source credentials in the service before scheduling. If the credentials expire or change, the refresh fails silently until you fix them.
Troubleshooting
If your refresh fails, work through these in order.
- Check the error message: In Excel, go to Data > Queries & Connections, right-click the query, and choose Edit. The editor shows the step that failed with a red warning. In Power BI, open the dataset and click Refresh history to see the exact error.
- Verify the source is reachable: Open the source file or navigate to the folder manually. If it is on a network drive, confirm you are connected. If it is a web source, check that the URL still works in a browser.
- Check for renamed columns or changed data types: The most common cause of failure is a source column that was renamed or removed. Look at the error step — it will often say Column not found. Edit the query to match the new structure.
- Update credentials: In Power BI, go to the dataset settings and click Edit credentials. Re-enter your password or re-authenticate. In Excel, go to Data > Get Data > Data Source Settings and update the stored credentials.
- Clear the cache: Sometimes Power Query caches old metadata. In the editor, go to File > Options > Data Load and click Clear Cache. Then refresh again.
- Simplify the query: If the error is unclear, copy the query, paste it as a new query, and delete steps one by one until it works. This isolates the problem step.
- Reinstall or update: If nothing else works, close Excel, restart, and try again. If the issue persists, check for Office updates. Do not reinstall unless you have confirmed the query itself is not the problem.
If a step deletes data — such as removing a column — you can undo it by deleting the step in the Applied Steps pane. This does not delete the source data, only the transformation in your query.
Frequently Asked Questions
Can I schedule a refresh in Excel without Power Automate?
Yes, but only with a local macro and Windows Task Scheduler. The macro runs ThisWorkbook.RefreshAll, and Task Scheduler opens the workbook and triggers the macro at a set time. This requires the computer to be on and Excel to be installed.
Why does my query fail when I add a new file to the folder?
Power Query imports all files in the folder by default. If the new file has a different structure (missing columns, different headers), the combine step fails. Filter the file list in the query to only include the expected files, or ensure all files in the folder share the same schema.
What is the difference between Refresh and Refresh All?
Refresh updates only the selected table or query. Refresh All updates every query in the workbook, including those that are not loaded to a sheet. Use Refresh All when you have dependent queries.
Can I use Power Query to write data back to a database?
No. Power Query is read-only. It connects, transforms, and loads data into Excel or Power BI. To write data back, you need a separate tool like Power Automate, a SQL script, or an Excel VBA macro that uses a database connection.
How do I stop a query from loading to the worksheet?
In the Power Query Editor, right-click the query in the Queries pane and uncheck Enable Load. The query still runs during refresh, and other queries can reference it, but it does not appear as a table in Excel. This is useful for intermediate calculations.
My scheduled refresh in Power BI fails with a gateway error. What should I do?
If your data source is on-premises (like a local SQL database or a network folder), you need an on-premises data gateway installed on a machine that can reach the source. Install the gateway, sign in with your Power BI account, and add the data source in the gateway settings. Then in the dataset settings, select that gateway under Gateway connection.