Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Code to refresh a dataset where prior refresh failed

Hi everyone,

 

we have a number of "production" datasets, and these seem to fail to refresh occasionally. Uncertain why - maybe network hiccup, PBI service issue, database being backed up on our side, etc.

 

Long and short, I'm trying to write a script that will loop through every dataset in our production workspaces, check whether a) the dataset is set up to refresh, and b) if so did the last refresh work properly. Unfortunately I'm not a PowerShell developer (at all...) so I'm hitting some issues.

 

The main piece of code I have is:

 

$AllDatasets =
ForEach ($workspace in $Workspaces)
    {
    ForEach ($dataset in (Get-PowerBIDataset -Scope Organization -WorkspaceId $workspace.Id))
        {

        # check if dataset is supposed to refresh
        $url = "https://api.powerbi.com/v1.0/myorg/groups/" + $workspace.Id + "/datasets/" + $dataset.ID + "/refreshSchedule"
        $refreshSched = $false
        $refreshSched = (ConvertFrom-Json (Invoke-PowerBIRestMethod -Url $url -Method Get)).enabled


        # check status of last refresh
        $url = "https://api.powerbi.com/v1.0/myorg/groups/" + $workspace.Id + "/datasets/" + $dataset.ID + '/refreshes?$top=1'
        #Write-Host $url
        $last_status = "n/a"
        $last_status = (ConvertFrom-Json (Invoke-PowerBIRestMethod -Url $url -Method Get)).value.status



        [pscustomobject]@{
            WorkspaceName = $workspace.Name
            WorkspaceID = $workspace.Id
            DatasetName = $dataset.Name
            DatasetID = $dataset.Id
            DatasetAuthor = $dataset.ConfiguredBy
            Refresh = $refreshSched
            LastStatus = $last_status
            }
        }
    }

This seems to work pretty well - but it throws errors. For example, the bit of code where it calls the "/refreshSchedule" fails if the dataset isn't set to be refreshed. Ditto for when it calls the last status $top=1 bit.

 

Is there a simple way for me to test whether a dataset is set up to be refreshed, other than calling the /refreshSchedule API and getting an error back? And/or is there a better way to handle the error? I thought I could use the "IsRefreshable" field when querying the dataset itself...but that seems to be set to TRUE even for datasets that aren't scheduled to refresh.

 

Thanks!

Scott

0 REPLIES 0

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.