Power BI native query lets you write your own SQL and have Power BI send it to the source database as-is, instead of letting Power Query build the query for you. You use it when you need SQL the graphical editor cannot produce: a stored procedure call, a CTE, a window function, a database-specific hint, or a hand-tuned query that already runs fast. The catch is that native queries are opaque to Power BI — it cannot fold later steps back into them — so you should use them deliberately and keep them as the last step in your query chain. Nothing extra needs installing; you need Power BI Desktop, a database you can reach, and permission to run the SQL you are about to write.
What You'll Need
- Power BI Desktop (any current version). The Power Query Editor is built in.
- A connection to a relational source that accepts SQL text — SQL Server, Azure SQL, Oracle, PostgreSQL, MySQL, Snowflake and similar. Flat files, Excel and most web sources have no native query option.
- Credentials with read permission on the objects your SQL touches.
- The SQL already tested in the source database's own query tool, so you know it runs and returns the shape you expect.
- Knowledge of the source's SQL dialect. A query written for SQL Server will not necessarily run on PostgreSQL.
Step-by-Step
- Test the SQL outside Power BI first. Run it in SQL Server Management Studio, Azure Data Studio, pgAdmin, DBeaver or whatever tool your database uses. Confirm it executes without errors, returns the columns you want, and finishes in a reasonable time. Fixing SQL inside Power Query is slow and frustrating; fixing it in a real SQL editor is fast.
- Open Power BI Desktop and start a new query. On the Home ribbon, choose Get data, then pick your connector — for example SQL Server or PostgreSQL database. Menu labels vary slightly by version, device and region.
- Enter the server and database, then expand Advanced options. This is where the native query box lives. Paste your SQL into the SQL statement field. Leave the other advanced fields alone unless you know you need them.
- Choose your data connectivity mode deliberately. Import loads a copy into the model and refreshes on your schedule. DirectQuery sends queries to the source each time a visual is rendered. Native queries work with both, but DirectQuery means your SQL runs repeatedly, so keep it fast.
- Authenticate. Pick the credential type your organisation uses — Windows, database, or organisational account — and connect. If the dialog warns that you are using a native query, read it: Power BI is telling you it cannot validate or fold the statement.
- Check the preview, then transform. Power Query shows the result set. Rename columns, set data types, remove unused columns and filter rows here. Do all of this after the native query step, never by editing the SQL to do work Power Query could do more visibly.
- Keep the native query as the last step that touches the source. Any step you add after it runs in Power Query's own engine, not in the database. That is fine for shaping, but it means those steps are not folded back to the server.
- Load or close and apply. Choose Close & Apply to bring the table into the model. If you used DirectQuery, test a few visuals to confirm performance is acceptable.
- Set refresh credentials for the published dataset. After publishing to the Power BI service, open the dataset settings and confirm the data source credentials are stored, otherwise scheduled refresh fails silently or with a credential error.
- Document the SQL. Paste the query into a comment, a shared document or the query's description field. Six months from now, nobody will remember why the WHERE clause has that odd date filter.
Common Mistakes
- Using SELECT * in a native query. Column order and names can change at the source, breaking the model. List columns explicitly.
- Putting a semicolon or a trailing ORDER BY at the end. Some connectors wrap your SQL in an outer query, and a trailing semicolon or ORDER BY can cause a syntax error. Remove them and sort in Power Query instead.
- Assuming query folding still works. Once a native query is in the chain, Power BI cannot fold steps before it, and steps after it are not sent to the database. Filters you add later may pull the full result set into memory first.
- Doing heavy joins and aggregations in SQL and then filtering in Power Query. You lose the benefit. Filter inside the SQL where the database can use indexes.
- Forgetting parameters. Hard-coded dates and IDs break next month. Use Power Query parameters and reference them in the SQL where the connector supports it.
- Running a stored procedure that returns multiple result sets. Power BI generally expects one tabular result; extra result sets cause confusion or errors.
Tips
- Write the SQL to return exactly the grain you need — one row per customer, per day, per transaction — so the model stays simple.
- Cast and alias columns in SQL so names are stable and readable before they reach Power Query.
- For DirectQuery, keep the query sargable: avoid functions on indexed columns in WHERE clauses.
- If the SQL is long, keep a copy in source control alongside the report file.
- Use a view in the database when the logic is shared across reports; point the native query at the view and keep the complex SQL in one place.
- Test refresh in the Power BI service, not just in Desktop — gateway and credential issues only appear there.
Troubleshooting
The query errors immediately. Copy the exact SQL back into your database tool. The usual culprits are a trailing semicolon, a dialect mismatch, or a reference to a table your credentials cannot see.
It works in the database tool but not in Power BI. Check for multiple statements, temporary tables and stored procedures. Some connectors only accept a single SELECT statement.
Refresh in the service fails. Open the dataset settings and re-enter credentials. If the source is on-premises, confirm the data gateway is running and the dataset is bound to it.
It is slow. Run the SQL with an execution plan in the database tool. Add or adjust indexes, reduce columns, and push filters into the SQL. For DirectQuery, reduce the number of visuals on a page so fewer queries fire.
Columns disappeared or changed type. The source schema changed. Update the SQL to list columns explicitly and re-set data types in Power Query.
If that did not work, delete the query and rebuild it from the connector, or fall back to selecting tables and views in the Navigator and shaping them in Power Query — you lose the custom SQL but regain folding and simpler maintenance.
Frequently Asked Questions
Does a native query break query folding?
Yes. Power BI cannot fold steps into SQL it did not generate, so anything after the native query runs locally.
Can I use parameters in a native query?
Often yes, by referencing a Power Query parameter inside the SQL text, but support varies by connector. Test it before relying on it.
Can I call a stored procedure?
Some connectors allow it, but Power BI expects a single tabular result. Prefer a view or a plain SELECT where possible.
Is native query available for Excel or CSV files?
No. It only applies to sources that understand SQL, such as relational databases and some cloud warehouses.
Import or DirectQuery with a native query?
Import for anything heavy or infrequent; DirectQuery only when you need near-real-time data and the SQL is fast.
Why does the service refresh fail when Desktop works?
Almost always stored credentials or a gateway binding. Re-enter credentials in the dataset settings.