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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors