Forum Discussion
API Loop not working in M Query
Found the bug: in your recursive step, the exit/continue condition and the recursive call both reference the original ApiParamStartDate instead of the newly incremented "newstart" date, so the date never actually progresses:
output =
if ApiParamStartDate <= ApiParamEndDate or loopNum < 10
then appendedData
else @#"GET Activity"(ApiParamStartDate, ApiParamEndDate, loopNum, appendedData)
Two problems here:
1. The recursive call passes ApiParamStartDate (the original, unchanged start date) instead of newstart, so even if it did recurse, day 2 onward would just re-request the same date range as day 1.
2. The condition is inverted/too permissive: "ApiParamStartDate <= ApiParamEndDate" stays true for the entire run (since the start date never advances), so the "or" makes this true almost immediately and the function returns appendedData right away instead of continuing to loop - matching exactly what you're seeing ("works the first time, doesn't call a second time").
Fix both by advancing the date and correcting the exit condition:
output =
if newstart > ApiParamEndDate or loopNum >= 10
then appendedData
else @#"GET Activity"(newstart, ApiParamEndDate, loopNum, appendedData)
Now the function only stops once the incremented date passes your end date (or you hit the 10-call safety cap), and each recursive call moves the window forward by one day via newstart.