Forum Discussion
Pagination API
- 1 year ago
Hi Elipo
Try this code
let access_token = access_token, // Function to fetch a page of data FetchData = (page as number) as record => try let response = Json.Document(Web.Contents("https://api.qgenda.com/v2/schedule", [ Headers = [ Authorization = "bearer " & access_token, #"Content-Type" = "application/json" ], Query = [ startDate = "2025-01-01", endDate = "2025-04-30", page = Text.From(page), limit = "100" ] ])), safeResponse = if Record.HasFields(response, "data") and (response[data] is list) then response else [data = {}] in safeResponse otherwise [data = {}], // Generator that creates a list of page results Pages = List.Generate( () => [PageNum = 1, Result = FetchData(1)], each Record.HasFields([Result], "data") and List.Count([Result][data]) > 0, each [PageNum = [PageNum] + 1, Result = FetchData([PageNum])], each [Result] ), // Convert each page's 'data' list to a table of records PagesTables = List.Transform( Pages, each Table.FromList(Record.Field(_, "data"), Splitter.SplitByNothing(), {"Record"}) ), // Combine all tables into one CombinedTable = Table.Combine(PagesTables), // Expand fields from the record (adjust fields as per your data structure) #"Expanded Record" = Table.ExpandRecordColumn(CombinedTable, "Record", { "StartDateUTC", "EndDateUTC", "CompName", "TaskName", "StaffFName", "StaffLName" }), // Apply Text.Combine to handle lists of text #"Combined Text" = Table.TransformColumns(#"Expanded Record", { {"CompName", each Text.Combine(List.Transform(_, Text.From)), type text}, {"TaskName", each Text.Combine(List.Transform(_, Text.From)), type text}, {"StaffFName", each Text.Combine(List.Transform(_, Text.From)), type text}, {"StaffLName", each Text.Combine(List.Transform(_, Text.From)), type text} }), // Convert date columns to datetime #"Changed Type" = Table.TransformColumnTypes(#"Combined Text", { {"StartDateUTC", type datetime}, {"EndDateUTC", type datetime} }) in #"Changed Type"this has a similar pages step to above but a public api , i have a feeling you must having this issue in this step
let // Search query searchTerm = "harry potter", // Function to fetch paginated results FetchData = (page as number) as record => try let response = Json.Document(Web.Contents("https://openlibrary.org/search.json", [ Query = [ q = searchTerm, page = Text.From(page) ] ])), safeResponse = if Record.HasFields(response, "docs") and (response[docs] is list) then response else [docs = {}] in safeResponse otherwise [docs = {}], // Generate all pages until no results Pages = List.Generate( () => [PageNum = 1, Result = FetchData(1)], each Record.HasFields([Result], "docs") and List.Count([Result][docs]) > 0, each [PageNum = [PageNum] + 1, Result = FetchData([PageNum])], each [Result] ), // Convert each page to a table PagesTables = List.Transform(Pages, each Table.FromList([docs], Splitter.SplitByNothing(), {"Record"})), // Combine all pages CombinedTable = Table.Combine(PagesTables), // Expand fields of interest Expanded = Table.ExpandRecordColumn(CombinedTable, "Record", {"title", "author_name", "first_publish_year"}) in Expanded
Hi, the provided code is functioning correctly. However, the table returns empty due to restrictions outlined in the REST API documentation.
Hi Elipo ,
Thanks for confirming the code is working correctly. If the table returns empty due to API restrictions, that’s an important limitation to keep in mind.
Could you please share the specific REST API documentation or page where these restrictions are outlined? This would help to better understand the API behavior.
Meanwhile, if any of the suggestions shared here have been helpful, please consider marking the response as a solution. It helps others in the community as well.
Let us know if you need any further assistance.
Thank you.