Search code examples
excelpowerbipowerqueryexcel-2016excel-web-query

Excel power query gives DataSource.Error web.content failed to get content from api url (404):Not Found


My excel uses data from an api url, I run the query every 10 mins, but if the api has a lot of traffic and returns a 404 error, I get a pop up error "DataSource.Error web.content failed to get content from api url (404);Not Found" and the whole spread sheet isn't working until I click okay, what I need to do is to let the query to keep trying the api url until it gets data to return instead of throwing an error. here's the query that I run:

let
    Source = Json.Document(Web.Contents("api url")),
    #"Converted to Table" = Record.ToTable(Source),
    #"Removed Top Rows" = Table.Skip(#"Converted to Table",9),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Top Rows",6)
in
    #"Removed Bottom Rows"

I tried to add MaunalStatusHandling but it didn't solve the problem as I kept getting error messages. I hope that someone can show me a way to solve this

thanks in advance for your help


Solution

  • I figured out a way to solve the problem, I'll post it in case someone else has a similar problem

    let
        api_fnName = let
        Value.WaitFor = (producer as function, interval as function, optional count as number) as any =>
            let
                list = List.Generate(
                    () => {0, null},
                    (state) => state{0} <> null and (count = null or state{0} < count),
                    (state) => if state{1} <> null
                        then {null, state{1}}
                        else {1 + state{0}, Function.InvokeAfter(() => producer(state{0}), interval(state{0}))},
                    (state) => state{1})
            in
                List.Last(list),
        Web.ContentsCustomRetry = (url as text, optional options as record) => Value.WaitFor(
            (i) =>
                let
                    options2 = if options = null then [] else options,
                    options3 = if i=0 then options2 else options2 & [IsRetry=true],
                    result = Web.Contents(url, options3 & [ManualStatusHandling={500}]),
                    buffered = Binary.Buffer(result), /* avoid risk of double request */
                    status = if buffered = null then 0 else Value.Metadata(result)[Response.Status],
                    actualResult = if status = 500 then null else buffered
                in
                    actualResult,
            (i) => #duration(0, 0, 0, i*0.1))
    in
        Json.Document(Web.ContentsCustomRetry("api url")),
        results = api_fnName[results],
        results1 = results{0},
        data = results1[data],
        #"Converted to Table" = Record.ToTable(data),
        #"Kept Range of Rows" = Table.Range(#"Converted to Table",4,1)
    in
        #"Kept Range of Rows"