To run a SQL query in Power BI, you connect to your database with a supported connector (for example SQL Server, PostgreSQL or MySQL), and then either pick tables in the Navigator or switch to Advanced options and paste your own query into the SQL statement box. Power BI sends that statement to the database and imports or models the result, so the query runs once at refresh time rather than every time you click a visual. The whole process takes a few minutes once you have the connection details, and everything happens inside Power BI Desktop before you publish.
What You'll Need
- Power BI Desktop installed on a Windows PC. Publishing and scheduled refresh need a Power BI account, but building the query does not.
- The exact server name, database name and, for some servers, port number, given to you by whoever administers the database.
- Credentials that can read the tables you need. Ask for a read-only account if one exists, since a reporting query should never need write access.
- The database client driver installed on your machine if the connector requires one (for example a current SQL Server or PostgreSQL driver).
- A working knowledge of your table and column names, or access to a schema listing, so you can write a query that actually runs.
Step-by-Step
- Open Power BI Desktop and start a connection. On the Home ribbon, click Get data, then More. In the connector list, choose the database type you are querying, such as SQL Server database, PostgreSQL database or MySQL database, and click Connect. Menu wording can differ slightly between versions and regions, so look for the closest match if a label has changed.
- Enter the server and database details. Type the server name (and instance name, if your admin gave one, in the form server\instance). For SQL Server, switch to the Database tab if you want to connect straight to a specific database rather than browse the whole server. Choose the data connectivity mode: Import copies the result into the model; DirectQuery runs the query against the source each time data is refreshed on screen. DirectQuery is only offered for some connectors and has practical limits.
- Authenticate. Pick the authentication method your database expects, commonly Windows, Database (username and password), or Microsoft account for cloud databases. Click Connect. If a sign-in or consent prompt appears, complete it.
- Skip the table picker and open the query editor. In the Navigator window, instead of ticking tables, click the small Advanced options arrow at the bottom (or the Transform Data button). In the SQL statement box that appears, paste your query. Only a single SELECT statement belongs here; multiple statements separated by semicolons generally fail. Click OK.
- Write a query that returns tidy columns. A good starting shape is
SELECT CustomerName, OrderDate, TotalAmount FROM Sales.Orders WHERE OrderDate >= '2024-01-01'. Alias calculated columns with clear names, avoidSELECT *on wide tables, and do not include a trailing semicolon if the connector objects to it. Give each column a simple type that Power BI can recognise. - Check the results in Power Query Editor. The preview shows the columns and rows returned. Confirm the row count looks plausible and the column types are right (dates as dates, numbers as numbers). Use the Data Type dropdown on each column header to correct anything misread. Rename query steps or the query itself in the Query pane so the model stays readable.
- Load the data. Click Close & Apply on the Home tab. The query runs and the result lands in your data model as a table, ready for relationships, measures and visuals.
- Control when the query runs. Power BI runs the statement on load and again on every refresh. If the query is slow, limit rows with a WHERE clause or use parameters instead of hard-coded dates. To make the query adjust at refresh, create a parameter (Manage Parameters in Power Query Editor) and inject it into the statement.
- Keep credentials safe when publishing. After you publish to the Power BI service, open the dataset settings and set data source credentials and, if available, a gateway for on-premises databases. Without that step, scheduled refresh fails even though everything worked locally.
Common Mistakes
The most frequent problem is pasting a query that the server cannot parse because of a trailing semicolon, a comment marker, or a second statement after the first. Keep the statement to one SELECT. Another common error is leaving SELECT * in place: the model picks up dozens of unused columns, bloating file size and slowing refreshes.
People also forget that the query text is stored in the file and reused unchanged, so a hard-coded date such as '2024-01-01' quietly becomes stale. Use a parameter, or a relative date filter, instead of editing the query by hand each month. Finally, many publishers forget to set credentials in the service, then wonder why the browser refresh works but the scheduled one fails.
Tips
Write and test the query in your database's own tool first, where error messages are clearer, then paste the working version into Power BI. Keep the query narrow: filter at the source rather than importing everything and filtering in Power Query, because filtering early means less data leaves the server.
Name your query something descriptive such as Sales by Region, and add a comment at the top of the SQL statement with its purpose and owner. If several reports need the same query, publish it once as a shared dataset and connect reports to that, rather than duplicating the statement in every file. For DirectQuery, test the query's response time against realistic data volumes before committing to that mode.
Troubleshooting
The connector cannot reach the server. Check the server name spelling and any instance name, confirm your machine is on the network or VPN the database expects, and ask the admin whether the port is open. A timeout usually points to a firewall rather than a wrong password.
Login failed. Re-enter credentials, confirm the authentication method matches what the server expects, and check the account has read permission on the specific tables. Test the same credentials in the database's own client to see whether the problem is in Power BI or the account.
The query is rejected. Remove trailing semicolons, check that every column exists with the spelling and case the server uses, and confirm you have not included another statement. If it works in the database tool but not here, look for features specific to that tool, such as temporary tables created earlier in the session.
Results look wrong. Check the data type of each column in Power Query Editor, and inspect the applied steps. A step that changes the query or filters rows after import may be hiding records. Compare the row count in Power BI against the same query in the database tool.
Scheduled refresh fails after publishing. Open the dataset settings in the service, re-enter the data source credentials, and check whether an on-premises data gateway is required and running. If a gateway is needed and none is installed, the local machine cannot be used as a substitute.
Frequently Asked Questions
Can I run any SQL statement in Power BI?
No. The SQL statement box expects a query that returns a result set, normally a single SELECT. Statements that modify data, such as INSERT, UPDATE, DELETE or CREATE, are not the purpose of the connector and should not be run there.
Does the query run every time I click a visual?
In Import mode, no. It runs when you load or refresh, and visuals read the stored copy. In DirectQuery, the query (or a version folded from it) can be sent to the source when visuals need data, so response depends on the database.
Can I edit the query after loading it?
Yes. Open Transform Data to launch Power Query Editor, select the query and edit the SQL statement in the source step. Changing the statement can break later steps if column names change, so review the applied steps afterwards.
Can I combine a SQL query with other sources?
Yes. Append or merge it with other queries inside Power Query Editor, as long as the column types line up. Watch for privacy level settings that can block combining sources.
Do I need a gateway for an on-premises database?
Yes, for scheduled refresh in the Power BI service when the database sits on your own network. Desktop work does not need one, but the service cannot reach that server without a gateway installed and configured.
Is native SQL faster than picking tables?
It can be, because you control filtering and column selection at the source. It can also be slower if the query is poorly written or the server is strained, so test both approaches against realistic data.