Forum Discussion
How do I change the Power Query data source for multiple excel files at once?
- Anonymous1 year ago
Hi anuverma,
Thanks for reaching out to the Microsoft fabric community forum.
Based on your description, you're dealing with multiple Excel files that each connect to a master file via Power Query or external links, and you're now moving that master file to a cloud location. You want to update the path in all the connected files without manually opening each one which is a very valid requirement, especially at scale.
You can go through the response provided by lbendlin and check if your issue can be resolved or you can follow this step where you can automate the update of the data source using PowerShell. This script will open each file silently, update the connection string or Power Query M code, save, and close it:
$folderPath = "C:\Your\Folder\With\ExcelFiles" $oldPath = "C:\Path\To\Old\Master.xlsx" $newPath = "https://yourcloudlocation.com/Path/To/New/Master.xlsx" $excel = New-Object -ComObject Excel.Application $excel.Visible = $false $excel.DisplayAlerts = $false Get-ChildItem -Path $folderPath -Filter *.xlsx | ForEach-Object { $workbook = $excel.Workbooks.Open($_.FullName) foreach ($connection in $workbook.Connections) { if ($connection.Type -eq 6) { # 6 = xlConnectionTypeWORKBOOK $connString = $connection.OLEDBConnection.Connection if ($connString -like "*$oldPath*") { $connection.OLEDBConnection.Connection = $connString -replace [regex]::Escape($oldPath), $newPath } } } # For Power Query / M code changes (if applicable): foreach ($query in $workbook.Queries) { $formula = $query.Formula if ($formula -like "*$oldPath*") { $query.Formula = $formula -replace [regex]::Escape($oldPath), $newPath } } $workbook.Save() $workbook.Close() } $excel.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)Make sure to replace the $folderPath, $oldPath, and $newPath with your actual paths. This script assumes the connection string or Power Query M code contains the old file path, and replaces it with the new one. Also be sure to back up your Excel files before running the script, as it will overwrite them.
I would also take a moment to thank lbendlin, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Best Regards,
Hammad.
Community Support Team
Hi anuverma,
As we haven’t heard back from you, so just following up to our previous message. I'd like to confirm if you've successfully resolved this issue or if you need further help.
If yes, you are welcome to share your workaround and mark it as a solution so that other users can benefit as well. If you find a reply particularly helpful to you, you can also mark it as a solution.
If so, it would be really helpful for the community if you could mark the answer that helped you the most. If you're still looking for guidance, feel free to give us an update, we’re here for you.
Best Regards,
Hammad.
Hi anuverma,
I just wanted to follow up on your thread. If the issue is resolved, it would be great if you could mark the helpful reply as solution so other community members facing similar issues can benefit too.
If not, don’t hesitate to reach out, we’re happy to keep working with you on this.
Best Regards,
Hammad.
- Anonymous1 year agoNot applicable
Hi anuverma,
We noticed there hasn’t been any recent activity on this thread. If your issue is resolved, marking the correct reply as a solution would be a big help to other community members. If you still need support, just reply here and we’ll pick it up from where we left off.Best Regards,
Hammad.