Is there a way to set a max run time or a timeout factor in Power Query so that after a specified period the query would terminate itself regardless of whether it was successfully executed or not?
This is important for me because I have built various queries at my workplace that usually run fine (as in I have been running it daily without issues for months and years) but occasionally, they would hang (likely because of clashing with another process on the server at that moment, when the server was under heavy load) and keep making read requests on the server indefinitely - One time IT have told me they logged more than 7 million reads from my machine into the database within a few hours. In cases, these have caused the servers to crash which leads to extensive downtime.
So I would like to know if there is any setting, or any thing that I can build into the query itself to ensure it terminates after a certain period of time.
I'm proficient with the M Power Query langue.
Thanks.
[Update 1] Thanks Alexis for the suggestion below regarding setting a CommandTimeout value when coding the connection. It's a great pointer.
I'll try it out, do some tests and report back if it conclusively fixed the issue or not.
But in the meantime, I've done a bit of digging into Microsoft's Power Query documentation and found that the CommandTimeout argument itself already has a default value of 10 minutes built into it, so theoretically even if we didn't specify that argument, the query should have terminated itself. But that wasn't the case. It ran for hours.
I wonder if it is a bug with Excel version of Power Query? Because I do use Power Query within Power BI as well, and over there I haven't seen it crashing and hanging yet (admittedly I've been using the Excel version more frequently than I did Power BI).
However, if anyone has any other suggestions on potential fixes for this problem, that would be much appreciated. Thanks.
Using SQL Server Management Studio Connect to MS SQL server via SQL Management Studio. In Object Explorer, right-click on the server name and then select Properties. In the new tab, click on Connections node. In Remote Query Timeout change it to your desired value or specify 0 to set no limit.
How can I prevent the query from timing out? Answer: Open your query in design view. Then right-click in a blank area of the design view (where the tables are displayed) and select Properties from the popup menu. When the "Query Properties" window appears, set the "ODBC Timeout" property to 0.
I think there are two ways to achieve this: First is to open the advanced options when connecting to the data source and enter the time out minutes, like this: Second is to open the advanced editor in Power query editor and change the M query to set the time out manually, like this:
Since the SQL query takes 10 seconds to run and the timeout is 1 minute, it runs successfully. However, if you set the CommandTimeout option to 5 seconds like so: = "select [dbo]. [ForceDelay] (10) as MyColumn", …then the query will take longer that the timeout and you’ll see the following error in the Power Query Editor:
Every time a Power BI Import mode dataset connects to a data source it goes through a Power Query query, and inside the code of that Power Query query will be an M function that connects to a specific type of data source. Most – but not all – of these M functions have the option to set timeouts.
1 Open Query Editor 2 Choose File > Options and settings > Data source settings 3 Select your source and click on Change Source ... 4 Expand Advanced options and enter a Command timeout value
I found a pretty decent answer to this here. Here are the steps posted on that forum:
- Have the Power BI Desktop file open and in Report View
- Click on the arrow for "Edit Queries" (in the External Data section)
- A dropdown will appear - then click "Data Source Settings"
- Data source settings pop-up window will appear
- Click on "Change Source..."
- Another pop-up will appear
- Click on Advanced Options (a drop-down of sorts will appear within the pop-up)
- "Command timeout in minutes (optional)" will be the first option
- Enter a value - I chose to enter 60 minutes but feel free to enter any value
- Then apply the query changes and wait till the refresh is complete
This was written for Power BI, but it works in the Excel query editor as well. In summary,
When I tried this with a connection to a SQL server it added a CommandTimeout
argument to my Source step. You can just use this code instead and skip all the clicking:
= Sql.Database("server", "DB", [CommandTimeout=#duration(0, 0, 15, 0)])
Doing it via the Data source settings may be preferable if you aren't connecting to a SQL server as the parameters might be different. E.g.
= Web.Page(Web.Contents("URL", [Timeout=#duration(0,0,15,0)]))
or
= OData.Feed("http://some.url/service.svc/", null, [Timeout=#duration(0, 0, 15, 0)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With