none
Define condition to stop an API Call RRS feed

  • Question

  • Hello,

    I'm trying to get data from an API. Currently my code keeps bringing data even after it supposed to have finished. This is the code I'm using:

    let
        token = "1234",
        RUT = "76647505",
        dv = "1",
        Fecha_desde = "2019-01-01",
        Fecha_hasta = "2019-12-31",
    
    
        LibroDiario = (offset as number) 
        => Json.Document(Web.Contents("https://api.clay.cl//v1/obligaciones/documentos_tributarios/?rut_empresa=" & RUT & "&dv_empresa=" & dv & "&guia_despacho=true&fecha_desde=" & Fecha_desde & "&fecha_hasta=" & Fecha_hasta & "&offset=" & Number.ToText(offset),
            [Headers = [#"Content-Type" = "application/json", #"Accept" = "application/json", #"token" = token] ]))
    in
        LibroDiario

    The offset variable is used to go through the pages, giving me 50 values per page.

    I used the following function to get all the data:

    = List.Generate(()=> [Result = try Get_DTE(0) otherwise null, offset = 0],
    
                            each Record.Field([Result], "status") <> "FALSE",
                            each [Result = try Get_DTE([offset] + 50) otherwise null, offset = [offset] + 50],
                            each [Result])

    This gives me a list of records, and inside each record there's an 'status' field. I would need everything where 'status' = TRUE.

    Here's an image of how it looks, after the 11th item in the list everything is status = FALSE and therefore not needed.


    How can I modify the condition to prevent this problem?

    Thank you.






    • Edited by Oreo_bunny Friday, November 1, 2019 5:51 PM
    Friday, November 1, 2019 5:43 PM

All replies

  • Unfortunately the link to the image doesn't work, but maybe the status-field returns a boolean value. Then you'd have to omit the "": Record.Field([Result], "status") <> FALSE

    What I usually do is to use the counter during development to prevent never ending runs. I can then examine the results quicker and see what's going on.

    You could use the existing "offset" like so:

    = List.Generate(()=> [Result = try Get_DTE(0) otherwise null, offset = 0],
    
                            each Record.Field([Result], "status") <> "FALSE" and [offset] < 500,
                            each [Result = try Get_DTE([offset] + 50) otherwise null, offset = [offset] + 50],
                            each [Result])
    just don't forget to delete this "development help" before releasing the solution ;)


    Imke Feldmann - MVP Data Platform (PowerBI) - http://www.TheBIccountant.com 

    Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to vote it as helpful - Thanks!



    Saturday, November 2, 2019 7:15 AM
    Moderator