Stop 03 · SQL for Power BITopics · 8/9

Writing a Native SQL Query in Power BI

Part of the Power BI Roadmap

In short

Sometimes the cleanest way to get exactly the rows you need is to just write the SQL. Power BI lets you paste a query straight into the database connector, but it comes with one important trade-off.

The SQL Server and other database connectors let you paste a SQL statement under advanced options. It is handy for complex logic, but steps you add after it in Power Query usually stop folding back to the database.

The jaadu passes a handwritten order slip through a small kitchen hatch to a busy cook surrounded by steaming pots.

Think of it like this

Handing the chef a written order

Normally you point at dishes on the menu and the waiter writes the order for you (Power Query folding). With a native query you hand the kitchen your own handwritten order. The kitchen cooks it exactly, but the waiter can no longer tweak it for you. Anything extra you ask for afterwards is done at your table, not in the kitchen.

Watch it happen

Navigator tables vs native SQL

Pick tables in the Navigator

  • Power Query writes the SQL for you
  • Later steps usually fold to the server
  • Easy for others to read and edit
  • Works with incremental refresh

Paste a SQL statement

  • Full control over joins and logic
  • Good for complex queries you already trust
  • Steps added afterwards often stop folding
  • Logic is hidden inside one step

Prefer tables or a database view. Use a native query when the logic is genuinely complex and cannot live in a view, and add as few Power Query steps after it as possible.

The big ideas

What you need to know

Advanced options

In the SQL Server dialog, expand Advanced options and paste your statement into the SQL statement box.

Folding often stops

After a native query, extra steps usually run inside Power BI instead of on the server, which is slower on big tables.

Approval prompt

Power BI asks you to approve each new native query the first time it runs, as a safety check.

A view is often better

If the query is reused, save it as a view in the database and connect to the view like a table.

See it in code

Walk through the code

m
1let2    Source = Sql.Database(3        "sql-prod-01",4        "ShopDB",5        [Query = "6            SELECT o.OrderID, o.OrderDate, c.City, o.Amount7            FROM dbo.Orders AS o8            INNER JOIN dbo.Customers AS c9                ON c.CustomerID = o.CustomerID10            WHERE o.Status = 'Delivered'11              AND o.OrderDate >= '2026-01-01'12        "]13    )14in15    Source

1/3Same Sql.Database call as usual: server and database.

This is what Power BI stores when you paste SQL into Advanced options. The whole statement runs on the server.

Save yourself the pain

Common mistakes

Oops

Pasting SELECT * FROM a huge table and then filtering in Power Query.

Do this instead

Put the filters and the column list inside the SQL itself, because steps after a native query may not fold.

Oops

Ending the native query with ORDER BY to make the table look neat.

Do this instead

Leave sorting out. The model does not keep row order, and sorting wastes server time.

Your mission

Native query vs folding

~30 min

Quick check

You add a Filter Rows step after a native SQL query and View Native Query is greyed out. What does that usually mean?

Go deeper

  1. 1Load a table using a short SELECT with a WHERE clause.
  2. 2Compare it with picking the same table in the Navigator and filtering in Power Query.
  3. 3Prefer a database view over long pasted SQL when the logic is shared.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge