Solving “Query Timeout” by adjusting the CommandTimeout parameter in M-code.

The error “Query Timeout Expired” or “Microsoft SQL: Execution Timeout Expired” occurs when a database query initiated by Excel or Power Query takes longer to complete on the server than the client driver’s pre-configured time limit allows. By default, Power Query sets a command timeout window (typically 10 minutes, or 30 seconds for specific legacy OLE DB/ODBC drivers). When complex calculations, large table scans, or server locks delay the result set, the connection closes abruptly. You can solve this by explicitly defining the CommandTimeout parameter inside your Power Query M-code.

Quick Answer

To fix a query timeout in Excel Power Query, open the Advanced Editor for your query and add CommandTimeout = #duration(0, 0, 30, 0) inside the options record of your Sql.Database function. This increases the maximum allowed execution time to 30 minutes, preventing Excel from aborting long-running queries while the database processes the request.


Connection Timeout vs. Command Timeout

Before changing your configuration, determine which timeout is failing:

  • Connection Timeout: The time limit Excel allows to find the server host, complete the network handshake, and authenticate credentials. This is usually set to 15 or 30 seconds. If this fails, the server is unreachable or offline.
  • Command Timeout: The time limit Excel grants the database engine to parse, execute, and return the data after authentication succeeds. If your query runs for several minutes and then terminates with a timeout error, you are dealing with a command timeout.

For an overview of how database drivers manage session limits, see Managing SQL Query Stability: Fixing Timeouts and Resource Exhaustion..


Step-by-Step Fixes

Method 1: Update CommandTimeout Using the Power Query UI

If you do not want to edit code manually, update the parameter through Power Query’s standard connection window.

  1. In Excel, open the Data tab and select Queries & Connections.
  2. Double-click the affected query to launch the Power Query Editor.
  3. In the Applied Steps pane on the right side of the window, click the gear icon next to the Source step.
  4. In the SQL Server Database dialog box, click to expand Advanced options.
  5. Locate the field labeled Command timeout in minutes (optional).
  6. Enter an expanded threshold, such as 30 or 60.
  7. Click OK, then click Close & Load on the Home ribbon.

Method 2: Adjust the CommandTimeout Parameter in M-Code

If you built your query using a custom native SQL statement or connected via automated templates, the gear icon next to the Source step may not be visible. You must define the duration directly in the Advanced Editor.

  1. Open the query in the Power Query Editor.
  2. On the Home tab, click Advanced Editor.
  3. Locate the Source step, which typically looks like this: Source = Sql.Database("db-server.corp.local", "WarehouseDB")
  4. Add the options record using the #duration(days, hours, minutes, seconds) function: Source = Sql.Database("db-server.corp.local", "WarehouseDB", [CommandTimeout = #duration(0, 0, 45, 0)])
  5. If your query already includes an options record (such as [Query="..."]), add CommandTimeout alongside it, separated by a comma: Source = Sql.Database("db-server.corp.local", "WarehouseDB", [ Query = "SELECT * FROM Sales.FactTransactions WHERE FiscalYear = 2025", CommandTimeout = #duration(0, 1, 0, 0) ])
  6. Click Done and refresh the data.

What If Extending the Timeout Fails?

If you raise the timeout limit to 30 or 60 minutes and the query still times out, simply increasing the clock will not solve the underlying issue. The delay points to deeper structural or database problems:

Check for Query Folding Breaks

When Power Query cannot translate transformations into native SQL, it may request massive unindexed tables and process calculations locally on your workstation. This dramatically increases processing time and leads to timeout drops. To verify whether your transformations are executing on the server, see Troubleshooting “Query Folding” breaks: Why your SQL is running slow in Excel..

Check for Database Locks and Deadlocks

If a query that usually completes in seconds suddenly times out, another transaction on the database server likely has an exclusive lock on the required rows or tables. If the database engine resolves this competition by terminating your connection, consult Solving “Transaction (Process ID) was deadlocked on lock resources.”.

Analyze Underlying SQL Views

When pulling data from nested views that join multiple massive tables without indexes, execution plans can stall. For targeted view optimization techniques, refer to Handling “Timeout Expired” errors in large-scale SQL views..