Forum Discussion
Convert this code which is a Function into a self looping piece of code for batch
Hi AlanFoley
Sorry, got waylaid by other things.
Download the latest example PBIX
I've looked at your code and the code I posted last week is still pretty much what I would use. Even with your PBIX file I can't test it though because I have no access to the API. So you will have to insert the API url API Token and and make some other modifications to suit your needs.
I've created a new query called Call API and I have tested that the List.Generate function in that works correctly, at least to loop through and create a list of consecutive numbers and then bail out at a predetermined condition.
So I'm satisfied that my code is working and hopefully when you modify it it will do what you need.
In terms of checking for nextpageKey, when the response is received from the API, what you get back is this
which is a Record and you can see that nextPageKey is in it. If there is no more data to retrieve, the API responds with a similar record, just that nextPageKey is missing.
You can check for nextPageKey by checking the API response like this
try #"1stAPICall_JSON"[nextPageKey] otherwise null
If nextPageKey exists this code retrieves it. If it isn't there it gives you null. I've incorporated this into my Call API query
nextPageKey = try Result[nextPageKey] otherwise null,
You've got a section of code that I mentioned last week that I don't know what it is for. You're also testing something called data twice and doing different things for the same value of data. That logic isn't right but I don't know what it is you are trying to do.
appendedData = if currentData is null and data is null then {}
else if data is null then List.Combine({{}, currentData})
else if data is null then List.Combine({data, {}})
else List.Combine({data, currentData})
So the Call API query is using List.Generate a list of the results from the API calls. You can see elements being added to this list in the line
each [Result][entities])
I think with the appendedData and currentData variables you are trying to save the data from the API but I don't think these are needed. All your results will be in the list that List.Generate creates. You can modify the data from there.
Regards
Phil
Thanks Phil
Really apprecaited
I will review and make a go of it
Thanks a mil