Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ The notebook contains samples on how to send messages to [Azure Event Hubs](http
- Send events using a SAS Token
- Send events using a Managed Identity

### [Power BI](./power-bi.ipynb)
### Power BI

The notebook shows how DAX queries can be executed from Azure SQL DB using the `executeQueries` REST endpoint provided by Power BI datasets

- Execute DAX queries in Power BI
- [Execute DAX queries in Power BI](./power-bi.ipynb)
The notebook shows how DAX queries can be executed from Azure SQL DB using the `executeQueries` REST endpoint provided by Power BI semantic models
- [Refresh a semantic model](./power-bi-semantic-model-refresh.ipynb)
The notebook shows how a semantic model can be refreshed from Azure SQL DB & SQL Server using the `refreshes` REST endpoint provided by Power BI semantic models

### [Azure Cognitive Services](./azure-cognitive-services.ipynb)

Expand Down
147 changes: 147 additions & 0 deletions power-bi-semantic-model-refresh.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c81e9b8a",
"metadata": {},
"source": [
"# Refresh Power BI Semantic Model via REST endpoint\n",
"\n",
"Power BI allows the refresh of a published model via the refreshes endpoint.\n",
"\n",
"[Datasets - Refresh Dataset In Group](https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/refresh-dataset-in-group?wt.mc_id=MVP_449122&source=post_page-----1e52165e36ab---------------------------------------)\n",
"\n",
"Authetication can be done via SPN (Microsoft Entra Application)."
]
},
{
"cell_type": "markdown",
"id": "016f5c1e",
"metadata": {},
"source": [
"# Prerequisites\n",
"\n",
"To trigger the refresh, we first need to collect the following information:\n",
"- The tenant ID that hosts the semantic model\n",
"- The client ID of the SPN object\n",
"- The secret of the SPN object\n",
"- The semantic model ID\n",
"- The workspace ID"
]
},
{
"cell_type": "markdown",
"id": "f0addc61",
"metadata": {},
"source": [
"# Get Oauth2 Token for the SPN\n",
"\n",
"We can request for a token for our SPN leveraging **sp_invoke_external_rest_endpoint** and we save it an @access_token variable."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7eeee18b",
"metadata": {
"vscode": {
"languageId": "sql"
}
},
"outputs": [],
"source": [
"\n",
"DECLARE @tenant_id NVARCHAR(100) = N'<redacted>'\n",
" ,@client_id NVARCHAR(100) = N'<redacted>'\n",
" ,@client_secret NVARCHAR(200) = N'<redacted>'\n",
" ,@workspace_id NVARCHAR(100) = N'<redacted>'\n",
" ,@dataset_id NVARCHAR(100) = N'<redacted>'\n",
"\n",
"DECLARE @scope NVARCHAR(200) = \n",
" N'https://analysis.windows.net/powerbi/api/.default';\n",
"\n",
"DECLARE @response NVARCHAR(MAX);\n",
"\n",
"DECLARE @url NVARCHAR(400) = \n",
" N'https://login.microsoftonline.com/' \n",
" + @tenant_id \n",
" + N'/oauth2/v2.0/token';\n",
"\n",
"DECLARE @headers NVARCHAR(MAX) = \n",
" N'{\"Content-Type\":\"application/x-www-form-urlencoded\"}';\n",
"\n",
"DECLARE @body NVARCHAR(MAX) =\n",
" N'client_id=' + @client_id \n",
" + N'&scope=' + @scope \n",
" + N'&client_secret=' + @client_secret \n",
" + N'&grant_type=client_credentials';\n",
" \n",
"EXEC sp_invoke_external_rest_endpoint\n",
" @method = 'POST'\n",
" ,@url = @url\n",
" ,@headers = @headers\n",
" ,@payload = @body\n",
" ,@credential = NULL\n",
" ,@response = @response OUTPUT;\n",
" \n",
"DECLARE @access_token NVARCHAR(MAX) = \n",
" JSON_VALUE(@response, N'$.result.access_token');"
]
},
{
"cell_type": "markdown",
"id": "ae9bc94d",
"metadata": {},
"source": [
"# Refresh the semantic model\n",
"\n",
"Once you have the token stored in the @access_token variable, you can use it to call the Power BI Refresh API."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0dcaf0b0",
"metadata": {
"vscode": {
"languageId": "sql"
}
},
"outputs": [],
"source": [
"DECLARE @refreshUrl NVARCHAR(400) =\n",
" N'https://api.powerbi.com/v1.0/myorg/groups/' \n",
" + @workspace_id \n",
" + N'/datasets/' \n",
" + @dataset_id \n",
" + N'/refreshes';\n",
"\n",
"DECLARE @refreshHeaders NVARCHAR(MAX) =\n",
" N'{\"Authorization\":\"Bearer ' \n",
" + @access_token \n",
" + N'\",\"Content-Type\":\"application/json\"}';\n",
"\n",
"DECLARE @refreshBody NVARCHAR(MAX) = N'{\"notifyOption\": \"NoNotification\"}';\n",
"\n",
"DECLARE @refreshResponse NVARCHAR(MAX);\n",
"\n",
"EXEC sp_invoke_external_rest_endpoint\n",
" @method = 'POST'\n",
" ,@url = @refreshUrl\n",
" ,@headers = @refreshHeaders\n",
" ,@payload = @refreshBody\n",
" ,@credential = NULL\n",
" ,@response = @refreshResponse OUTPUT; \n",
"\n",
"SELECT @refreshResponse;"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}