Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
LarryBroward
Helper I
Helper I

API Loop not working in M Query

Hi,

 

In the code below I can see the day increment is working the first time thru but it doesn't increment and call the api a second time.

Is there anything obvious that I am doing wrong.

Thanks 

 

 

(ApiParamStartDate as date,ApiParamEndDate as date,optional loop as number,optional data as list) =>

let

newstart = Date.AddDays(ApiParamStartDate,1),

sd = Date.ToText(newstart) & "%2000%3A00",
ed = Date.ToText(newstart) & "%2023%3A59",

Source =

// call a 61 second delay function here
//Function.InvokeAfter(

 

Json.Document(Web.Contents(
"https://cloud.com/api/v4/get_readings/?device_sn=12345&start_date=" &Text.From(sd)&"&end_date="&Text.From(ed)&"&output_format=json&page_num=1&per_page=2000",
[Headers=[Accept="application/json", Authorization="Token xxxxx"]])),

 

//#duration(0,0,1,1))),

 

//loop
loopNum = loop + 1,

currentData = Record.ToList(Source[data]) ,

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) ,

 

output =

if ApiParamStartDate <= ApiParamEndDate or loopNum < 10
then
appendedData

else
@#"GET Activity" (ApiParamStartDate,ApiParamEndDate,loopNum, appendedData)

in output

 

 

 

1 REPLY 1
Divyaraj_Rathod
Helper II
Helper II

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.

Helpful resources

Announcements
Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.