Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Call a REST API multiple times using Power Query

Hi everybody, i have a Rest Api call that have the limited of 31 days for call. This is the advanced editor: let body = "{""from"":""2021-10-01T00:00:00Z"",""to"":""2021-10-31T23:59:59Z""}", D...
  • mattww's avatar
    4 years ago

    Hi Anonymous 

     

    I work with something similar, without getting into tons of detail, here's some pointers to get you started and let me know if anything doesn't make sense or you want me to go through and part in more detail

     

    If I've understood you right, I think it would be best to walk back from todays date (or a predefined date if you prefer) in increments.

     

    1) Convert your code above into a function, where you can pass the start and end dates

    Excuse that the escaped quotes might not be quite right

     

    (startDate as text, endDate as text) =>

    let
    body = "{""from"":""" & startDate &""",""to"":""" & endDate & """"}",
    Data= Web.Contents("***"),
    DataRecord = Json.Document(Data)
    in
    DataRecord

     

    2) Use a numbers table to generate a list of Start and End dates as required, invoke the above function

     

    let
    // Getting results for the last 365 days, in increments of 30 days
    Source = List.Numbers(0, 365,30),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "EndDate", each Date.AddDays(Date.From(DateTime.LocalNow()),[Column1]*-1)),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "StartDate", each Date.AddDays([EndDate],-29)),
    #"Invoked Custom Function" = Table.AddColumn(#"Added Custom1", "fnGetData", each fnGetData([StartDate], [EndDate]))
    in
    #"Invoked Custom Function"

    3) Expand the results and get rid of any unneccesary columns

     

    Hope that helps, let me know if you need any more info

     

    Matt

     

    If this post helps then please consider Accept it as the solution to help the other members find it more quickly.