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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.