Forum Discussion
Elipo
1 year agoRegular Visitor
Pagination API
I need Help! Power query returns this error: Expression.Error: We cannot convert a value of type List to type Record. Details: Value=[List] Type=[Type] Here is my code for getting paginated 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
kushanNa
1 year agoSuper User
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