July 24, 2018 Martin Kratky

Automate Update For Power BI

At one of our clients -that was previously using a solution that includes a limited set of the Acterys functionality- we saw that they had licensed a separate update solution that refreshes the underlying Power Pivot model in an Excel workbook that is used in a Power BI report. This is a requirement that can easily be implemented in a few minutes using a Windows PowerShell script in conjunction with the Windows Task Scheduler. In the following steps I describe the process:

Create The PowerShell Script

  1. Open Windows PowerShell
  2. Copy and paste the following code and replace “Yourpath\yourExcelworkbookname.xlsx” with the details of your workbook:
#Set the file path (can be a network location)
$filePath = "Yourpath\yourExcelworkbookname.xlsx"
#Create the Excel Object
$excelObj = New-Object -Com Excel.Application
#Wait for 10 seconds then update the spreadsheet
Start-Sleep -s 10
#Make Excel visible. Set to $false if you want this done in the background
$excelObj.Visible = $true
$excelObj.DisplayAlerts = $false
#Open the workbook
$workBook = $excelObj.Workbooks.Open($filePath)
#Wait for 10 seconds then update the spreadsheet
Start-Sleep -s 10
#Refresh all data in this workbook
$workBook.RefreshAll()
Start-Sleep -s 10
#Save any changes done by the refresh
$workBook.Save()
$workBook.Close()
#Uncomment this line if you want Excel to close on its own
$excelObj.Quit()
$excelObj = $null
write-host "Finished updating the spreadsheet" -foregroundcolor "green"
Start-Sleep -s 5

This script will open the respective workbook run the refresh for the data models, save and close the workbook. This means that if you have a Power BI report that is using this workbook, you can just refresh the Power BI report and will get the updated data.

Setup The Update Interval

This process can be scheduled so that it runs completely automatic in a specific interval.

  1. Open Windows Task Scheduler:

2. Create a new task:

To configure the interval go to the “Triggers” tab and:

3. Click on new:

Her you can now:

4. Setup the trigger and specify the update interval. For example, every day at 12:54:

The final step is to specify what action you want to run. To do this:

5. Go to the “Actions” tab and choose: “Start a program” and point to the location of your script:

That’s it. This can obviously be configured and refined with all the options that PowerShell gives you respectively be integrated into a standalone app which is available in Acterys for an even easier usability and with additional features. Please don’t hesitate to contact us for any further questions.

×