Forum Discussion
Is it possible to create a retry loop for refresh failure raised by IDbCommand interface?
hi
I have faced this problem earlier also so for this I have Implemented a Wait and Retry Mechanism for Data Refresh in Power BI:
To handle data refresh failures due to data source unavailability, I developed a function using M code in Power BI that incorporates a wait-and-retry mechanism. This solution ensures that the data refresh process can attempt multiple retries before finally failing, which increases the robustness of your data import process.
code:
let
get_data = (counter as number) =>
let
output =
try
let
//your source link
Source = Excel.Workbook(Web.Contents("URL_TO_YOUR_FILE.xlsx"), null, true),
Data =
let
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data]
in
Sheet1_Sheet
in
Data
otherwise if counter < 4 then
Function.InvokeAfter(() => @get_data(counter + 1), #duration(0,0,0,20))
else Error.Record("Dataset refresh failure after multiple attempts", "File not found error", "Additional details need to check")
in
output
in
get_data
Explanation:
- Function Definition: get_data is a recursive function that takes a counter as an argument.
- Try Block: Attempts to load the data from the specified Excel file or any source.
- Catch Block:
- If an error occurs and the counter is less than 4, the function waits for 20 seconds before retrying.
- If the counter reaches 4, it records an error message indicating a refresh failure.
Usage:
To use this function, replace "URL_TO_YOUR_FILE.xlsx" with the actual URL of your source. You can then call this function with an initial counter value of 0.