
Imagine you're a data analyst at a mid-sized manufacturing company. Your company's production data — orders, inventory, shipments, defect logs — all lives in a SQL Server database. Every week, you manually export CSVs, clean them up in Excel, and build reports. It takes hours. Meanwhile, someone in IT keeps reminding you that "all that data is already in the database, you just need to connect to it." They're right, and today we're going to do exactly that.
Power Query has a first-class connector for SQL Server and most other relational databases. But connecting to a database is fundamentally different from opening a CSV file. A file sits passively on your hard drive. A database is a live system with security rules, user permissions, and thousands — sometimes billions — of rows. That means you need to handle credentials properly, and you need to understand how to ask the database only for exactly what you need, rather than pulling the entire thing into your computer's memory.
By the end of this lesson, you'll know how to establish a connection to SQL Server from Power Query, navigate the available tables, write a native SQL query to pull exactly the data you want, and manage credentials so your connections are secure and maintainable. These skills transfer almost directly to other relational databases like PostgreSQL, MySQL, and Azure SQL Database.
What you'll learn:
SELECT * FROM TableName is helpful)When you connect to a CSV or Excel file, Power Query reads the entire file and loads it into a temporary workspace. The file doesn't care who reads it or how often. A relational database is a different creature entirely.
A relational database is a structured system that stores data in tables, enforces relationships between those tables, and controls who can access what through a system of logins and permissions. SQL Server is Microsoft's enterprise relational database — the same engine that powers everything from small business applications to Fortune 500 ERP systems.
When Power Query connects to SQL Server, it's not reading a file. It's opening a network connection, authenticating with the server, sending a query (a request for data written in SQL), and receiving the results. This means several things matter that didn't matter with files:
Understanding these differences will help you avoid the most common pitfalls, especially the one where a colleague complains that "Power Query is so slow" because they loaded a 50-million-row table when they only needed last month's records.
Let's walk through the connection process from scratch.
Open Power BI Desktop and click Get Data in the Home ribbon. A dialog box will appear with a search bar and a categorized list of data sources. Type "SQL" in the search bar, select SQL Server, and click Connect.
A connection dialog appears with two fields: Server and Database.
PROD-SQL-01, 192.168.1.50, or a fully qualified domain name like sqlserver.yourcompany.com. If your SQL Server is running on a named instance (a way of hosting multiple SQL Server installations on one machine), the format is ServerName\InstanceName, for example PROD-SQL-01\REPORTING.ProductionDB.Leave the Data Connectivity mode set to Import for now (we'll discuss DirectQuery another time), and click OK.
In Excel, click the Data tab in the ribbon. In the Get & Transform Data group, click Get Data → From Database → From SQL Server Database. You'll see the same Server and Database fields.
After clicking OK, Power Query will prompt you for credentials (we'll cover that in detail shortly). Once authenticated, you'll see the Navigator panel — a tree view of everything your user account can access in that database.
In the Navigator, you'll typically see folders for Tables, Views, and sometimes Stored Procedures and Functions. Expanding a table shows you a preview of its data on the right side.
For our ProductionDB scenario, imagine you can see tables like:
OrdersOrderLineItemsProductsCustomersShipmentLogDefectReportsYou could click Orders and hit Load to pull the whole table in. But if Orders has five years of data and you only need the last 90 days, loading the entire thing is wasteful and slow. This is where native queries become your best friend.
A native query is SQL code that you write and send directly to the database, instead of letting Power Query import a table and then filter it afterward. The database executes your SQL, and only the resulting rows travel across the network to Power Query. This is dramatically more efficient.
Go back to the Get Data → SQL Server Database dialog. Enter your Server and Database as before. Before clicking OK, expand the Advanced options section at the bottom of the dialog.
You'll see a text box labeled SQL statement (optional, requires database). This is where you write your native query. Note: the Database field becomes required when you use this option.
Let's write our first native query. For the manufacturing scenario, we want orders from the last 90 days:
SELECT
o.OrderID,
o.OrderDate,
o.CustomerID,
c.CustomerName,
o.TotalAmount,
o.Status
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
WHERE o.OrderDate >= DATEADD(day, -90, GETDATE())
AND o.Status <> 'Cancelled'
ORDER BY o.OrderDate DESC
This query does several things that would otherwise require multiple Power Query transformation steps:
Orders and Customers)DATEADD, a SQL Server functionAll of this happens on the SQL Server before a single byte comes to Power Query. Enter this query and click OK.
Important: When you use a native SQL query, Power Query cannot modify or optimize the SQL you've written. It will send exactly what you type to the server. This means you're responsible for writing correct, safe SQL. Always test your query in a SQL tool (like SQL Server Management Studio or Azure Data Studio) before pasting it into Power Query.
There's a practical middle path: use the Navigator to load a table, build your transformations in the Power Query Editor using the graphical interface, and then let Power Query's query folding mechanism handle pushing filters back to the database. We'll cover this next.
Query folding is one of the most important concepts for database connections in Power Query, and it's one of the least understood.
When you import a table and then apply filters, Power Query tries to translate your transformation steps back into SQL and send them to the database as part of the original query. This translation process is called query folding. When it succeeds, your filter runs on the server. When it fails, Power Query pulls all the data first and filters it locally.
Here's how to check if your steps are folding: In the Power Query Editor, right-click on a step in the Applied Steps panel on the right side. If you see the option View Native Query, that step is folding — Power Query is generating SQL behind the scenes. If that option is grayed out, the step is being processed locally.
Some transformations can't be translated back to SQL. Common culprits include:
Once any step breaks query folding, all subsequent steps also run locally. This is why the order of your steps matters. Put filters and column selections early (these fold well) and complex transformations later.
Pro tip: After connecting to a SQL Server table, immediately apply your row filters (date ranges, status fields, etc.) and select only the columns you need using Choose Columns. These steps typically fold, reducing the data volume before any local processing begins.
This is where many beginners get tripped up. Power Query needs to authenticate with SQL Server every time it refreshes data, so it stores credentials. Understanding how this works will save you hours of troubleshooting.
When Power Query prompts you for credentials after connecting to SQL Server, you'll see a dialog with several options. The two main ones are:
1. Windows Authentication
This uses your current Windows login to authenticate with the database. If your organization has set up SQL Server to trust Windows user accounts (this is called "Windows Authentication" or "integrated security"), your credentials are passed automatically. You don't type a username or password — SQL Server trusts that if Windows says you're DOMAIN\jsmith, then you are.
This is the preferred method in enterprise environments because:
To use it, in the credential dialog, select Windows and leave the credential fields blank (or pre-filled with your Windows credentials).
2. Database Authentication (SQL Server Authentication) This uses a username and password created specifically in SQL Server, independent of Windows. You'll see this in cloud databases, external vendor systems, or older SQL Server setups.
In the credential dialog, select Database and enter the SQL Server login name and password. For example, a service account might be powerquery_reader with a specific password.
Warning: Be careful about where these files live. If you use SQL authentication and someone else opens your Power BI file or Excel workbook, they'll be prompted for credentials — but if the file contains sensitive data and is shared via a network share without proper controls, that's a risk. Always think about who can access the file, not just the database.
Power Query stores credentials per data source, not per query or file. This is a crucial distinction. The credential is tied to the combination of server address and database.
In Power BI Desktop, go to File → Options and settings → Data source settings. You'll see a list of every data source you've ever connected to. You can edit credentials, delete them, or change the privacy level here.
In Excel, go to Data → Queries & Connections, then in the Power Query Editor, go to File → Options and settings → Data source settings — same interface.
If your database password changes (or if you initially entered the wrong credentials), you'll need to update them. The error you'll see looks something like: "We couldn't connect to your data. Details: Microsoft SQL: Login failed for user 'jsmith'."
To fix it:
Tip: If you're setting up a Power BI report that will be published to the Power BI Service and refreshed on a schedule, you'll need to reconfigure credentials in the Power BI Service as well, through a Data Gateway. The credentials stored in your Desktop file don't automatically transfer to the cloud.
When you connect to multiple data sources — say, a SQL Server database and an Excel file — Power Query asks you to set Privacy Levels. These tell Power Query whether data from one source can be combined with data from another.
For most internal corporate data scenarios, set both sources to Organizational. Setting one source to Private will prevent query folding across sources and usually cause frustrating performance issues without any actual security benefit in a controlled environment.
For this exercise, you'll build a practical data connection using what we've covered. If you have access to a SQL Server, use it. If not, download the AdventureWorks sample database — it's a free Microsoft sample database that you can restore to a local SQL Server Express instance (also free) or access through several publicly available cloud instances.
Scenario: You're building a weekly sales summary report. You need orders from the AdventureWorks database, specifically from the Sales.SalesOrderHeader and Sales.Customer tables, filtered to the year 2013.
Step 1: Connect
Open Power BI Desktop. Click Get Data → SQL Server Database. Enter your server name and AdventureWorks2019 as the database. Expand Advanced options.
Step 2: Write the native query In the SQL statement box, enter:
SELECT
soh.SalesOrderID,
soh.OrderDate,
soh.TotalDue,
soh.Status,
soh.OnlineOrderFlag,
c.CustomerID,
soh.ShipToAddressID
FROM Sales.SalesOrderHeader soh
INNER JOIN Sales.Customer c ON soh.CustomerID = c.CustomerID
WHERE YEAR(soh.OrderDate) = 2013
ORDER BY soh.OrderDate
Click OK and authenticate with Windows Authentication when prompted.
Step 3: Verify in the Editor
Click Transform Data to open the Power Query Editor. You should see roughly 3,915 rows (the 2013 orders in AdventureWorks). Notice the column types — check that OrderDate was recognized as a Date/Time type and TotalDue as a decimal number. If not, right-click the column header and set the type manually.
Step 4: Check credential storage Close and save the Power BI file. Reopen it. Does it refresh without prompting you for a password? If you used Windows Authentication, it should. Go to File → Options and settings → Data source settings to confirm your credential is saved.
Step 5: Modify the query
Now imagine requirements change and you also need orders from 2014. In the Power Query Editor, right-click your query and select Advanced Editor. You'll see the M code that Power Query generated. Find the native query text inside Value.NativeQuery() and change = 2013 to >= 2013. Click Done and verify the row count increases.
"I can see the server but not the database I need"
Your user account may not have been granted access to that specific database. Contact your database administrator and ask them to grant db_datareader role access to your Windows login for the required database.
"My native query runs fine in SSMS but fails in Power Query"
Two common reasons: First, Power Query doesn't support multi-statement SQL (multiple statements separated by semicolons). Your query must be a single SELECT statement. Second, some SQL Server features like temporary tables (#TempTable) aren't supported in native queries. Rewrite using CTEs (Common Table Expressions) instead:
WITH RecentOrders AS (
SELECT * FROM Orders WHERE OrderDate >= DATEADD(day, -90, GETDATE())
)
SELECT ro.*, c.CustomerName
FROM RecentOrders ro
JOIN Customers c ON ro.CustomerID = c.CustomerID
"Refresh works on my machine but fails when published to Power BI Service" The Power BI Service can't reach your on-premises SQL Server directly. You need to install and configure an On-premises Data Gateway — a piece of software that sits on your network and acts as a secure bridge between the Power BI cloud service and your internal database.
"I keep getting prompted for credentials even after saving them"
This usually happens when the server address in the connection doesn't exactly match the stored credential. For example, if you initially connected to PROD-SQL-01 and now the query uses PROD-SQL-01.yourcompany.com, Power Query treats these as different sources. Standardize on one format — fully qualified domain names are most reliable.
"My date filter in the native query doesn't update automatically"
If you hardcoded a date like WHERE OrderDate >= '2024-01-01', that won't change on its own. Use relative date functions like DATEADD(day, -90, GETDATE()) in SQL, or use Power Query parameters to pass dynamic values into your native query — a technique worth exploring as you advance.
You now have the foundational skills to connect Power Query to SQL Server like a professional. Here's what you've learned:
Where to go next:
The skills you've built here don't just apply to one report. Every database-backed analysis you build from here will benefit from knowing how authentication works, how to write efficient native queries, and how to diagnose credential problems when they arise. That's foundational competence — and that's what separates a good analyst from a great one.
Learning Path: Power Query Essentials