Event Trigger Power BI Entities With Azure SQL Database
Event Trigger Power BI Entities With Azure SQL Database
Problem Statement
Is it possible to refresh Power BI Dataflow / Dataset on any event occurrence in Azure SQL Database.
Prerequisites
Solution
Note: We would be leveraging Azure SQL Database External REST Endpoints Integration via which we would be triggering an Azure
function and Power BI REST API.
1. In this article, Refresh Power BI Dataflow / Dataset from Azure Data Factory / Synapse Pipeline via Service Principal (AAD Token
Generation)- Part 1, we trigger the Power BI Dataset / Dataflow refresh REST API by generating an AAD Token of the Service
Principal which has access on the needed Power BI Dataset / Dataflow.
2. We would be following a similar route in the current flow; wherein we would be triggering a Managed identity enabled Azure
Function to generate an AAD Token, which we would, in turn, use to trigger the Power BI Dataset / Dataflow Refresh API.
Note: The AAD Token can have an expiration time between 60 – 90 min. So rather than preserving that as a Database Scoped Credential,
we can generate it at run time by triggering the Azure function (in this scenario)
3. Assuming we have a PowerShell Core Runtime Stack Function App set up, update the requirements.psd1 ( present within App files
section of App Function) to leverage Az modules.
Add the below code (for AAD Token creation) in run.ps1 file within Code + Test Section and Save it.
Connect-AzAccount -Identity
C#
5. Once the function is created within the Azure function, login into app.powerbi.com and provide the App function Member access
within the workspace hosting the Power BI Dataset / Dataflow that we need to refresh.
6. Login to the Azure SQL database via which we need to trigger the Power BI refresh and execute the queries in the below provided
sequence.
a)
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<<>>'
b)
CREATE DATABASE SCOPED CREDENTIAL [https://<<AzureFunctionName>>.azurewebsites.net/api/<<FunctionName>>]
WITH IDENTITY = 'HTTPEndpointHeaders', SECRET = '{"x-functions-key":"<<FunctionKey>>"}'
SQL
7. Now for the current use case, the scenario is to trigger a Power BI dataset refresh when any data is inserted within a table in the Azure
SQL Database.
b) Creating a Stored Procedure which we can leverage within the Database Trigger
--To Trigger the Azure Function in order to generate the AAD Token at run time
@url = @Appurl,
@method = 'POST',
@payload = @Apppayload,
@credential = [https://<<AzureFunctionName>>.azurewebsites.net/api/<<FunctionName>>],
@response = @Appresponse output;
(
"Token" NVARCHAR(max)
))
@method = 'POST',
@url = @PowerBIurl,
@payload = @PowerBIpayload,
@headers = @PowerBIheaders,
@response = @PowerBIresponse output;
SELECT @PowerBIresponse,@PowerBIret
Result / Output
Creating an Event to trigger the Database Trigger , thereby generating an AAD Token based on Appfunction to trigger the Power BI
Dataset refresh.
Response
Note: To refresh a dataflow, replace the @PowerBIurl variable within the Stored Procedure with the below value :
N'https://api.powerbi.com/v1.0/myorg/groups/<<WorkspaceID>>/dataflows/<<DataflowID>>/refreshes'
HTTP
Also, the Refresh API call is asynchronous. Hence, we do not know whether the dataset/dataflow refresh has actually succeeded. The
successful execution of the Stored Procedure doesn’t mean that the refresh was a success.