Forum Discussion
Convert this code which is a Function into a self looping piece of code for batch
Hi AlanFoley
You can loop using List.Generate, something like this
let
Source = List.Generate(()=>
[Result = Web.Contents("https://www.microsoft.com/en-us/microsoft-365/blog/excel/"), nextPageKey = 2],
each [nextPageKey] <= 10,
each [ Result = Web.Contents("https://www.microsoft.com/en-us/microsoft-365/blog/excel/",
[ RelativePath = Text.From(nextPageKey)]
),
nextPageKey = [nextPageKey] + 1
],
each [Result])
in
Source
In your case it would look more like this
let
Source = List.Generate(()=>
[Result = "", appendedData = {}, nextPageKey = "", loop = 0],
each [nextPageKey] <> null,
each [ Result = Json.Document(Web.Contents("https://domain.com/api/v2",
[ RelativePath = if loop = 0 then "entities?entitySelector=type%28APPLICATION%29&from=now-8d&fields=%2BfirstSeenTms"
else "entities?nextPageKey=" & nextPageKey,
Headers=[Authorization="Api-Token xxxxx"]
]
)),
loop = [loop] + 1,
nextPageKey = try Result[nextPageKey] otherwise null,
currentData = Result[entities],
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})
],
each [Result])
in
Source
I have no way to test this for you as I don't know what website you are working with or have authorisation to it.
Also, there are things like 'data' which aren't defined so I don't know what this is supposed to hold.
In this section of code
you are testing 'data' twice to see if it is null but trying to do 2 different things so that isn't right 🙂
Hopefully this will get you started along the right path. See if you can modify my code in the attached file to work for you and if you can't please post back with more info about what exactly you are doing and supply your PBIX file if you can.
Regards
Phil
Hi Phil
Thank you for your detailed response and for taking the time to assist me
I will need some time to review and understand your code as well as retrofit your suggestions in my PowerBI desktop file
I will provide some feedback as soon as I can
Thanks again
Alan