This error occurs when Power Query or Excel attempts to read, transform, or overwrite a local file that an active operating system thread has locked. Windows enforces file-locking rules: if another application, or a hidden background instance of Excel, holds an exclusive open handle on a source CSV, XLSX, or text file, Power Query cannot access it during data refresh.
Quick Answer
Close any software currently accessing the source file, including hidden background instances of Excel in Task Manager. If the source file is stored in a synced directory like OneDrive or Dropbox, pause file synchronization during refresh. If the query reads from the active workbook itself, separate the data into an external file or use local Excel Tables.
What Causes the File Lock
Operating systems prevent data corruption by placing write-locks or shared-read locks on active files. When Power Query refreshes, its background mashup engine (safe_m_engine.exe or Microsoft.Mashup.Container.exe) requests read access. If another program opened the file without allowing shared reads, the operating system denies the container’s request.
The four most frequent sources of file locks are:
- Self-referencing workbooks: Power Query is set up to read an Excel file from disk via
Excel.Workbook(File.Contents("...")), but that exact file is currently open in your active Excel session. - Orphaned Excel processes: Excel was previously closed, but a hung background process (
EXCEL.EXE) still retains a lock on the file. - Cloud sync engines: Services like OneDrive, SharePoint Sync, Google Drive, or Dropbox are uploading a modified version of the source file at the exact moment Power Query tries to pull data.
- Antivirus scanning or preview panes: Windows Explorer’s Preview Pane or endpoint security scanners are scanning the file handle.
Progressive Diagnostic and Repair Steps
1. Check for Orphaned Processes
Even if Excel appears closed on your desktop, a crash or poorly terminated macro can leave an active instance running in the background.
- Press
Ctrl + Shift + Escto open Task Manager. - Select the Details tab.
- Look for any instances of
EXCEL.EXEorMicrosoft.Mashup.Container.exe. - Right-click the process and choose End Task.
- Reopen your reporting workbook and refresh the query.
2. Resolve Self-Referencing Workbook Locks
A frequent mistake occurs when building a query that imports data from the very workbook running the query using File.Contents. When you refresh the query, Excel attempts to connect to the saved copy on disk while holding an open read/write handle on it.
- The Fix: If the query must pull from the active workbook, do not use
File.Contents(). Instead, format your source data as a structured Excel table and import it directly into Power Query using:
This reads the in-memory data stream without querying the disk-bound file handle.Excel.CurrentWorkbook(){[Name="YourTableName"]}[Content]
3. Handle Cloud Sync and File Conflicts
When source files live inside a synchronized folder (OneDrive, SharePoint Sync, or Box), the sync engine locks files while uploading changes.
- Check the system tray icon for your cloud drive. If it indicates syncing activity, wait until the status changes to “Up to date.”
- If your query uses
Folder.Files()to pull from a directory, hidden temporary lock files will trigger this error. For full details on stripping these temporary files before processing, see Fixing errors in Folder.Files when a temp file (~$) is open in the folder. - If lock issues persist on shared network folders, see “The file is locked for editing”: How to kill “Ghost” owner files (~$filename).
4. Turn Off the Windows Explorer Preview Pane
Windows Explorer locks files while generating visual previews.
- Open File Explorer.
- Go to the View tab.
- Toggle off Preview pane and Details pane.
- Attempt the Power Query refresh again.
5. Identify the Locking Handle with Sysinternals Process Explorer
If the locking application remains unknown:
- Download Microsoft’s free Process Explorer (part of Sysinternals).
- Run the program as Administrator.
- Press
Ctrl + Fand paste the full file name of your source data. - Click Search. The bottom pane will show the exact process name and PID holding the handle.
- Close or terminate that application.
What to Do Next
If eliminating locks restores connectivity on single queries but errors recur whenever you run a full model update, your queries may be competing for the same file in parallel. Switch from global refresh to sequential execution by reviewing Using the “Refresh All” button vs. Individual Query Refresh: Conflict resolution.