Sub topic 15
Triggering and inspecting refreshes with the REST API
The Power BI service can refresh a dataset on a schedule, but sometimes you need to refresh on demand: after a nightly ETL job finishes, after a deployment, or during testing. The Power BI REST API lets a script start a refresh, check its result, and change the schedule.
This chapter assumes you have a dataset in Import mode with a working refresh schedule, and that any on-premises source is already mapped to a gateway data source. It also assumes you can run PowerShell and read JSON.
What the API can and cannot do
The REST API controls refreshes, but it does not replace configuration. It does not install gateways, add gateway data sources, or store credentials. If a refresh cannot reach its source from the UI, it cannot reach it from the API either. The API starts the same refresh engine and routes it through the same gateway.
| Task | Method and URL path (after `https://api.powerbi.com/v1.0/myorg/`) |
|---|---|
| Start a refresh | `POST groups/{groupId}/datasets/{datasetId}/refreshes` |
| Read refresh history | `GET groups/{groupId}/datasets/{datasetId}/refreshes` |
| Change the schedule | `PATCH groups/{groupId}/datasets/{datasetId}/refreshSchedule` |
| Cancel an enhanced refresh | `DELETE groups/{groupId}/datasets/{datasetId}/refreshes/{refreshId}` |
For a dataset in My workspace, drop the groups/{groupId}/ part.
Finding the identifiers
You need two GUIDs. Open the dataset's settings page in the service and read the URL. It has the form .../groups/<workspace-id>/datasets/<dataset-id>/details. The first GUID is the workspace (group) ID and the second is the dataset ID.
Authentication and permissions
The caller must be able to write to the dataset. In practice that means the dataset owner, or a Member, Contributor, or Admin of the workspace. If you call the API with your own app registration, the token needs the Dataset.ReadWrite.All scope. The tenant must also allow service principals to use the Power BI APIs if you want to call it without a user.
The MicrosoftPowerBIMgmt module handles sign-in and builds the base URL for you. Install it once with Install-Module MicrosoftPowerBIMgmt -Scope CurrentUser.
Starting a refresh
A POST to the refreshes endpoint queues a refresh. The service answers 202 Accepted immediately. That response only means the request was queued. It does not mean the refresh succeeded.
Connect-PowerBIServiceAccount
$groupId = '<workspace-id>'
$datasetId = '<dataset-id>'
$body = '{"notifyOption":"MailOnFailure"}'
Invoke-PowerBIRestMethod -Method Post -Url "groups/$groupId/datasets/$datasetId/refreshes" -Body $bodyThe notifyOption value controls email to the dataset owner. For a refresh started through the API, the accepted values are MailOnCompletion, MailOnFailure, and NoNotification. If you send no body, the call still works and uses the default notification behavior.
A second refresh sent while one is already running is rejected with an error, typically 400 Bad Request. Do not fire the call in a tight loop.
Reading refresh history
The history call returns the same records you see in the refresh history dialog.
Invoke-PowerBIRestMethod -Method Get -Url "groups/$groupId/datasets/$datasetId/refreshes?`$top=5"The backtick before $top stops PowerShell from treating $top as a variable inside the double-quoted string. The response is JSON text. Pipe it through ConvertFrom-Json to get objects:
$history = Invoke-PowerBIRestMethod -Method Get -Url "groups/$groupId/datasets/$datasetId/refreshes?`$top=5" | ConvertFrom-Json
$history.value | Select-Object refreshType, status, startTime, endTimeThe fields that matter most:
| Field | Meaning |
|---|---|
| `refreshType` | `Scheduled`, `OnDemand` (started in the UI), `ViaApi`, or `ViaEnhancedApi` |
| `status` | `Completed`, `Failed`, `Disabled`, `Cancelled`, or `Unknown` |
| `startTime`, `endTime` | UTC timestamps. `endTime` is absent while the refresh runs. |
| `serviceExceptionJson` | A JSON string with an error code and message when the refresh failed |
| `requestId` | An identifier to quote when you contact support |
Unknown is the status of a refresh that is still in progress. It is not an error. Treat a record with status of Unknown and no endTime as "running."
The asynchronous pattern
Because the POST returns before the work is done, a script that needs the result must poll the history.
A minimal polling script looks like this:
$url = "groups/$groupId/datasets/$datasetId/refreshes"
Invoke-PowerBIRestMethod -Method Post -Url $url -Body '{"notifyOption":"NoNotification"}'
do {
Start-Sleep -Seconds 30
$latest = (Invoke-PowerBIRestMethod -Method Get -Url "$url?`$top=1" | ConvertFrom-Json).value[0]
Write-Host "Status: $($latest.status)"
} while ($latest.status -eq 'Unknown')
if ($latest.status -ne 'Completed') {
Write-Error "Refresh ended as $($latest.status): $($latest.serviceExceptionJson)"
}This script assumes the newest history record is the one it just started. That is true unless someone else starts a refresh at the same moment. For stricter matching, compare startTime with the time you sent the request.
Limits that apply to API refreshes
API refreshes count against the same quota as scheduled refreshes. On shared capacity, a dataset can refresh at most 8 times per day in total, whether the refresh came from the schedule, the UI, or the API. On Premium or Fabric capacity, the daily quota does not apply, but the capacity's memory and concurrency limits still do. Chapter "Refresh frequency limits and timeouts" covers those limits and the refresh timeout.
Changing the schedule through the API
You can also manage the schedule as code. A PATCH to the refreshSchedule endpoint accepts a body like this one, which sets two weekday refreshes and failure email:
{
"value": {
"enabled": true,
"days": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
"times": ["07:00", "13:00"],
"localTimeZoneId": "UTC",
"notifyOption": "MailOnFailure"
}
}Three details cause most mistakes here:
- The settings are wrapped in a
valueobject. Sending the fields at the top level fails. - Times must fall on the hour or half hour, such as
07:00or07:30. - For a schedule,
notifyOptionaccepts onlyMailOnFailureorNoNotification.MailOnCompletionis valid for on-demand API refreshes but not for the schedule.
Only the properties you send are changed, so you can send just {"value":{"enabled":false}} to pause a schedule.
Enhanced refresh
If you add any enhanced-refresh parameter to the POST body, such as type, commitMode, maxParallelism, retryCount, or objects, the service runs an enhanced refresh. This lets you refresh a single table or partition. The service returns a Location header containing a refresh ID. You can use that ID to query the one operation or cancel it with a DELETE. This is also the route for refreshing specific partitions of a table that uses incremental refresh, which a later chapter covers.
Common errors
- 401 Unauthorized: The token has expired or lacks the dataset write scope. Run
Connect-PowerBIServiceAccountagain. - 403 Forbidden: The caller is not a dataset owner or a Contributor-or-higher member of the workspace.
- 404 Not Found: The workspace or dataset ID is wrong, or the dataset is in a different workspace than the one in the URL.
- 400 Bad Request with "refresh already in progress": Wait for the running refresh to finish.
- A Failed record with a gateway or credential message: The API worked. The data source mapping or credentials need attention, as covered in the troubleshooting chapter.
Exercise
Your script posts to the refreshes endpoint and gets back 202 Accepted, so it reports success. The next morning the report still shows old data. What did the script miss, and how should it check the outcome?
Show answer
A 202 only means the refresh request was queued. The refresh itself ran later and probably failed. The script should poll the history endpoint (GET .../refreshes?$top=1) until the status is no longer Unknown, then check that it is Completed. If it is Failed, it should read serviceExceptionJson for the cause, such as a credential or gateway error.
Exercise
A PATCH to the refreshSchedule endpoint returns an error. The body is {"enabled": true, "times": ["07:15"], "notifyOption": "MailOnCompletion"}. Name three problems.
Show answer
The settings are not wrapped in a "value" object. The time 07:15 is not on the hour or half hour. MailOnCompletion is not allowed for a schedule, which accepts only MailOnFailure or NoNotification.