Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

PowerQuery Rest API Pagination

Hi folks   I am sure I am not the first one with the issue but I havent come across a viable solution for me. I am querying a Web API that returns pages by 200 rows. This API specifically also retu...
  • Anonymous's avatar
    Anonymous
    5 years ago

    Morning everyone!

     

    Thanks for the hints! After some trial & error I got it to work using a function PageRunner incorporating it into the main query. Note that I was lazy enough to defined the MaxPages first (as this is returned on each page) which I then used to limit the number of loops.

     

    PageRunner:

    (InputNumber)=>
    
    let
        
        APIKey = API_Key,
        APPKey= APP_Key,
        Page="&page="&Number.ToText(InputNumber),
        Source = Json.Document(Web.Contents("https://api.SOFTWARE.com/api/v3/deals.json?api_key="&APIKey&"&app_key="&APPKey&Page)),
        #"Converted to Table" = Table.FromRecords({Source})
        in
        #"Converted to Table"

     

    MainQuery:

    let
        MAXPages = let
        
        APIKey = API_Key,
        APPKey= APP_Key,
        Page="&page="&Number.ToText(1),
        Source = Json.Document(Web.Contents("https://api.SOFTWARE.com/api/v3/deals.json?api_key="&APIKey&"&app_key="&APPKey&Page)),
        #"Converted to Table" = Record.ToTable(Source),
        Value = #"Converted to Table"{1}[Value],
        pages1 = Value[pages]
    in
        pages1,
        
        Source = List.Generate(
            ()=> [Page=1, Funct = PageRunner(1)],
            each [Page]<=MAXPages,
            each [Page=[Page]+1, Funct = PageRunner([Page]+1)]),
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
    
    in
        #"Converted to Table"

     

    This works just fine. Any hints for makinf it more simple / efficient is highyl appreciated.