How to Edit SQL Queries Directly in Excel Without Power Query M
Question details
The user wants to write and execute standard SQL queries directly in Excel to handle data transformations instead of relying on Power Query's native M language.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Importing and transforming data from a SQL Server database into Excel.
- Observed behavior
- By default, Excel's Power Query Editor uses the M language for data transformations (like joins and grouping) after the initial import, rather than executing native database-side SQL.
Ensure you have your SQL Server connection details ready, including the server name and database name, and verify that you have the necessary read permissions to execute custom SQL queries on the database.
Use Advanced Options to Input Native SQL Queries During Import
Specify a native SQL statement when initially setting up the database connection so that operations like filtering and joining occur on the server before the data enters Power Query.
By providing a native SQL query during the initial connection setup, you force the database engine to process your joins, groupings, and filters. This completely bypasses the need to write those specific transformations using Power Query's M language later.
Navigate to the Data tab on the Excel ribbon, click on 'Get Data', hover over 'From Database', and select 'From SQL Server Database'.
In the SQL Server database dialog box, enter your Server name and the Database name you wish to query.
Click the 'Advanced options' dropdown arrow at the bottom of the dialog box to reveal additional settings.
Type or paste your native SQL query into the 'SQL statement (optional)' text box, then click 'OK' to connect and load your pre-transformed data.

Modify the SQL Statement of an Existing Query
Edit the Source step in the Power Query Editor to update or append a native SQL query to an existing SQL Server connection.
Manage Your Spreadsheet Data with WPS Office
Tired of dealing with complex Power Query M language syntax and heavy Excel processes? WPS Office offers a streamlined, lightweight, and highly compatible spreadsheet experience. Easily manage, filter, and analyze your data without the steep learning curve of advanced query languages.
- 1. Download and Install: Visit the official WPS website to download the free WPS Office installer and follow the on-screen instructions to install it on your device.
- 2. Open Your Spreadsheets: Launch WPS Spreadsheets and seamlessly open your existing .xlsx or .csv files without losing any formatting or data.
- 3. Analyze Data Easily: Use built-in data tools, pivot tables, and intuitive filtering options to manipulate your data without writing complex code.

Frequently Asked Questions
Why does Excel use M language instead of SQL after data is imported?
Once data is loaded into the Power Query engine, it relies on its native M formula language to record and execute transformation steps consistently, regardless of whether the original data source was a SQL database, a flat file, or a web API.
Can I convert an existing Power Query M script back into a native SQL query?
While Excel features 'Query Folding' which translates some basic M steps back into SQL behind the scenes for performance, you cannot directly export complex M transformations as raw SQL text. You must manually rewrite your M steps into a SQL statement.
Does using native SQL in Excel break query folding?
Yes, providing a custom native SQL statement in the advanced options typically breaks query folding. This means any subsequent transformations you add in the Power Query Editor will be processed locally on your computer rather than on the database server.




