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
Thank you, the error
We cannot convert a value of type List to type Record.
Details:
Value=[List]
Type=[Type]
is solved but I get an empty table.
Hi Elipo
Thanks for confirming that the original error is resolved.
Since you're now seeing an empty table, that typically indicates:
1. The API didn’t return any actual records for the given date range.
2. The "data" field’s structure varies across pages - for example, it might return a list in some responses and a record or something else in others.
As kushanNa rightly suggested earlier, the use of 'try ... otherwise' helps avoid breaking the query when unexpected structures occur. However, do note that this may drop rows with valuable data if the API returns inconsistent structures, silently skipping over responses that don’t match the expected format. This can lead to the query completing without error but returning an empty or partial table.
To handle this better,
Instead of relying on 'try', consider using a structure-aware approach like the one shared in this similar discussion: Re: Hi Re: [Expression.Error] We cannot convert a ... - Microsoft Fabric Community. This checks each value with 'Value.Is(...)' and then transforms it accordingly based on whether it's a list or a record.
You can adapt this idea to fit into your pagination logic.
Additionally,
->Try calling the API outside Power Query (e.g., Postman) with the same parameters (startDate=2025-01-01, endDate=2025-04-30, limit=100) to confirm whether data is returned.
->You can also temporarily output the raw Pages list in Power Query to inspect the actual content per page.
->Finally, verify that your API key or role has access to the expected data for the given filters.
Hope this helps. Please reach out for further assistance.
If this post helps, then please consider to give a kudos and Accept as the solution to help the other members find it more quickly.
Thank you.