Forum Discussion
Looping - API Call
- 6 years ago
Hi Asynchronism ,
pagination/paginate is the keyword you have to use: https://community.powerbi.com/t5/Desktop/how-to-create-a-query-that-paginates/td-p/20047/page/20
or:
ImkeF - got it working, thank you. Ended up doing it by records, not pages, but basically the same result
Here's what I ended up with:
url = "myapi/getlist?&limit=50&offset=0",
Source = ((Web.Contents(url, [Headers=[#"Content-Type"="application/json"]]))),
#"Imported JSON" = Json.Document(Source,65001),
Custom1 = List.Generate( () =>
[Result = #"Imported JSON", Offset = 0],
each [Result][response][count] > ([Offset]), //api returns a total record count as "count" in the response section
each [
Result = Json.Document(Web.Contents(url & "&offset=" & Text.From([Offset] + 50) )),
Offset = [Offset] + 50
]
),
Hi ImkeF,
I've noticed you seem to be the go to person for looping API calls, was hoping you could go over my code and see where I'm going wrong.
This is what I've currently have -
let
ProjectID = "135353", // Type Dalux ProjectID
iterations = 20,
Bookmark1 = "0",
url = "https://field.dalux.com/service/APIv2/FieldRestService.svc/v2.2/Projects/" &
ProjectID &
"/Approvals?key=[API KEY]&bookmark=",
FnGetOnePage=
(url, Bookmark1) as record =>
let
Source = Json.Document(Web.Contents(url & Bookmark1)),
data = try Source[ApprovalsList] otherwise null,
next = try Source[NextBookmark] otherwise null,
res = [Data = data, Next = next]
in
res,
GeneratedList =
List.Generate(
() => [i=0, res = FnGetOnePage(url, Bookmark1)],
each [i] < iterations and [res][Data] <> null,
each [i=[i]+1, res = try FnGetOnePage(url & [res][Next]) otherwise null],
each [res]
)
in
GeneratedList
The image below is the result of the query:
The function is working and is able to call the first batch of results but then it fails to replace Bookmark1 for the Next cursor and terminates the list.
Any suggestions?
Thank you in advanced.
Best regards,
Ricardo.