Katie Brown
Katie Brown

Reputation: 15

Power Query keeps failing refreshes

I have a macro that refreshes a series of power query connections (different queries of the same source, need to process the data in a very different way) but I have had persistent issues with some of the downloads failing ("Download did not complete"). Different downloads fail every time, but it's more common with the downloads later in the queue.

Sub RefreshData()
Call RunBillable
 Application.CalculateUntilAsyncQueriesDone
ThisWorkbook.Save
Call RunNonBillable
 Application.CalculateUntilAsyncQueriesDone
ThisWorkbook.Save
Call RunPCH
 Application.CalculateUntilAsyncQueriesDone
ThisWorkbook.Save
End Sub

Sub RunBillable()
 Application.EnableEvents = False
 On Error GoTo skip
  ThisWorkbook.Connections("Query - Total Billable Hours").Refresh
 Done:
  Exit Sub
 skip:
  On Error GoTo -1
 Application.EnableEvents = True
 End Sub

Sub RunNonBillable()
 Application.EnableEvents = False
 On Error GoTo skip
 ThisWorkbook.Connections("Query - NonBillable Hours Details").Refresh
 Done:
  Exit Sub
 skip:
  On Error GoTo -1
 Application.EnableEvents = True
End Sub

Sub RunPCH()
 Application.EnableEvents = False
 On Error GoTo skip
 ThisWorkbook.Connections("Query - PC HOURS").Refresh
 Done:
  Exit Sub
 skip:
  On Error GoTo -1
 Application.EnableEvents = True
End Sub

I have gotten to to the point where each query is now running in sequence and now on top of each other, but refreshes are still failing. If I go in and manually refresh the queries that fail though, they will update without an issue. Any ideas on how to get them all to download every time?

EDIT: Solution found! Need to use an if statement within a loop checking the number of errors.

 Sub RefreshData()

  RetryRefresh ("Query - Total Billable Hours")
  RetryRefresh ("Query - NonBillable Hours Details")
  RetryRefresh ("Query - PC HOURS")

 End Sub

Sub RetryRefresh(query As String)

 Dim retryCount As Integer
 For retryCount = 1 To 5 ' try refreshing 5 times
    On Error Resume Next
    ThisWorkbook.Connections(query).Refresh
    If Err.Number <> 0 Then
        Debug.Print "Refresh attempt " & retryCount & " failed"
        Err.Clear
    Else
        Exit For
    End If
    ' Add a small delay between retries, required to work
    Application.Wait (Now + TimeValue("00:00:10"))
Next retryCount
If retryCount > 5 Then
    MsgBox "Refresh of " & query & "failed after multiple attempts."
End If
End Sub

Upvotes: 1

Views: 71

Answers (0)

Related Questions