Forum Discussion
Update Dataset Connections using Power BI APIs
- 1 year ago
Your JSON request body should look like this:
{ "updateDetails": [ { "datasourceType": "Sql", "connectionDetails": { "database": "NAME_OF_DATABASE", "server": "NAME_OF_SERVER.database.windows.net" }, "datasourceId": "XYZ123" } ] }For cloud sources like Azure SQL Database, leave this field empty or remove it. The API does not require a gateway for cloud connections.
PowerShell Script:
# Parameters $workspaceId = "YOUR_WORKSPACE_ID" $datasetId = "YOUR_DATASET_ID" $url = "https://api.powerbi.com/v1.0/myorg/groups/$workspaceId/datasets/$datasetId/Default.UpdateDatasources" # Connection Details $ConnectionDetailsObject = @{ updateDetails = @( @{ datasourceType = "Sql" connectionDetails = @{ database = "NAME_OF_DATABASE" server = "NAME_OF_SERVER.database.windows.net" } } ) } # Convert to JSON and Make the API Call Invoke-PowerBIRestMethod -Url $url -Method Post -Body ($ConnectionDetailsObject | ConvertTo-Json -Depth 10) -ContentType "application/json"If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Your JSON request body should look like this:
{
"updateDetails": [
{
"datasourceType": "Sql",
"connectionDetails": {
"database": "NAME_OF_DATABASE",
"server": "NAME_OF_SERVER.database.windows.net"
},
"datasourceId": "XYZ123"
}
]
}
For cloud sources like Azure SQL Database, leave this field empty or remove it. The API does not require a gateway for cloud connections.
PowerShell Script:
# Parameters
$workspaceId = "YOUR_WORKSPACE_ID"
$datasetId = "YOUR_DATASET_ID"
$url = "https://api.powerbi.com/v1.0/myorg/groups/$workspaceId/datasets/$datasetId/Default.UpdateDatasources"
# Connection Details
$ConnectionDetailsObject = @{
updateDetails = @(
@{
datasourceType = "Sql"
connectionDetails = @{
database = "NAME_OF_DATABASE"
server = "NAME_OF_SERVER.database.windows.net"
}
}
)
}
# Convert to JSON and Make the API Call
Invoke-PowerBIRestMethod -Url $url -Method Post -Body ($ConnectionDetailsObject | ConvertTo-Json -Depth 10) -ContentType "application/json"
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
- LJonesStripe1 year agoRegular Visitor
Hi, thanks for your response. I get the following error message:
Invoke-PowerBIRestMethod:
Line |
6 | Invoke-PowerBIRestMethod -Url $url -Method Post -Body ($Connectio …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| One or more errors occurred. ({
"code": "InvalidRequest",
"message": "The given connection details for selector # are invalid"
})
Invoke-PowerBIRestMethod:
Line |
6 | Invoke-PowerBIRestMethod -Url $url -Method Post -Body ($Connectio …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Encountered errors when invoking the command: {
"code": "InvalidRequest",
"message": "The given connection details for selector # are invalid"
}I have verified by printing out the JSON that the ConnectionDetialsObject is formatted as expected.
- v-nmadadi-msft1 year agoCommunity Support
Hi LJonesStripe ,
Thanks for reaching out to the Microsoft fabric community forum.
Please ensure that:
1) The original data source and the new data source have the exact same schema.
2) You are the dataset owner
3) your token is not expired, Tokens are typically valid for 1 hour. If you receive an authentication error, refresh the token by re-running Connect-PowerBIServiceAccount or obtaining a new token programmatically.Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to give "Kudos"
Thanks and Regards- LJonesStripe1 year agoRegular Visitor
Hi,
I beleive the data sources have the exact same schema. How do I verify this?
I am the dataset owner, In my powershell script I am authenticating using oAuth to the API and authenticating using the same account. Can I just verify that I am using the correct endpoint?
In the GUI I am trying to update this section:
I am using this API: https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/update-datasources-in-group