Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
paritysupport
New Member

Update many excel files Power Query Data sources

Hello,

 

We have created a script using standard pivots tables and excel to update many excel files at once with a new DB connection with the script below (maybe this will be helpful to some!). This lets us update a folder of pre-built excel reports at once with a specified Server and DB name, keeping their views they are tied to.

 

We are updating our standard pivots to take advantage of Power Query & using dashboards. Our goal is to convert this script below to work with updating the power query datasources, rather than the default excel connections in the same manner. (Drop excel files with power queries into a folder, define the new datasource settings (Server Name & DB) , and update all files)  The original person who wrote this script has moved on several years ago - so any help is appreciated if you can point us in the right direction! 

 

 

public void UpdateConnectionStrings(List<FileInfo> pExcelFiles, string pOldServerName, string pOldDatabaseName, string pNewServerName, string pNewDatabaseName)

    {

      Microsoft.Office.Interop.Excel.Application application = null;

      try

      {

        // Create a new application

        application = new Microsoft.Office.Interop.Excel.Application();

        foreach (FileInfo excelFile in pExcelFiles)

        {

          // Open the file

          application.Workbooks.Open(excelFile.FullName);

          // Get the query tables from the worksheets

          var sheets = application.Sheets.OfType<Worksheet>();

          var queryTables = sheets.SelectMany(s => GetQueryTables(s));

          // Change the connection string for any query tables

          foreach (var queryTable in queryTables)

          {

            queryTable.Connection = queryTable.Connection.Replace(pOldServerName, pNewServerName);

            queryTable.Connection = queryTable.Connection.Replace(pOldDatabaseName, pNewDatabaseName);

          }

          // Get the pivot table data from the workbooks

          var workbooks = application.Workbooks.Cast<Workbook>();

          var pivotCaches = workbooks.SelectMany(w => GetPivotCaches(w));

          // Change the connection string for any pivot tables

          foreach (var pivotCache in pivotCaches)

          {

            pivotCache.Connection = pivotCache.Connection.Replace(pOldServerName, pNewServerName);

            pivotCache.Connection = pivotCache.Connection.Replace(pOldDatabaseName, pNewDatabaseName);

          }

          foreach (var workbook in workbooks)

          {

            workbook.Save();

            workbook.Close();

          }

        }

      }

      catch (Exception e)

      {

        throw new Exception(e.Message);

      }

      finally

      {

        // Make sure we quit the application

        if (application != null)

        {

          application.Quit();

        }

      }

    }

2 REPLIES 2
Jimmy801
Community Champion
Community Champion

Hello @paritysupport 

 

interesting that such a program is written in Java instead of VBA. 

the VBA-objects of Excel are used within the java-code. The problem is that there are no objects for Power Query and so I'm afraid there is no possibility create a automatism. 

The only way I can think of is to create a table in your Excel-file, where you maintain parameters for you Queries. Then you use the programm to update this table and update the queries.


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

JirkaZ
Solution Specialist
Solution Specialist

@paritysupport I don't know if it's still actual but this kind of thing you could only do with the already published reports via the API:

https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/updatedatasourcesingroup

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors