Function.InvokeAfter is a Power Query M function that invokes a specified function after a duration delay has passed. The function returns the result of the invoked function.
Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365
Function.InvokeAfter( function as function, delay as duration, ) as any
Returns the result of invoking
function after duration
delay has passed.
To really understand how this works, let’s see the Function.InvokeAfter function in action.
Picture this: You’re working with an API that has a restriction, allowing only one request every ten seconds. Now, if you’re making multiple requests, this could pose a significant challenge. This is where Function.InvokeAfter steps in. It provides an easy way to ensure Power Query limits the speed in which it sends requests.
Let’s assume we’re calling an API and the process would look something like this:
List.Generate( () => [ response = Web.Contents("SomeURL", "0"), page = response[page]? ], each [page] <> null, each [ response = Function.InvokeAfter( Web.Contents( "SomeURL", page ), #duration( 0, 0, 0, 10 ) ), page = response[page]? ], each [response][data] )
In the code above, we’re using Function.InvokeAfter to ensure we maintain a 10-second gap between each API call. We ensure that the duration is set at 10 seconds by using the #duration in the following way: #duration(0,0,0,10). This instructs the function to make an API call, waits for 10 seconds, and then proceeds to make the next call.
It’s a simple, powerful way to help manage the timing of function calls, especially when you’re dealing with restricted APIs.
Other functions related to Function.InvokeAfter are: