How to Create a Power BI Report from SQL Query
Pulling data directly from a SQL database is one of the most powerful features of Power BI, but sometimes you don't need an entire table - you just need a specific slice of data. Instead of importing massive tables and wrestling with filters in the Power Query Editor, you can use a custom SQL query to pull the exact data you need from the very start. This article will walk you through exactly how to create a Power BI report based on a SQL query, giving you more control and improving your report's performance.
Why Use a Custom SQL Query in Power BI?
While Power BI's standard interface for connecting to databases is fantastic, writing your own SQL query offers several key advantages, especially as your data needs become more complex.
- Performance improvement: When you connect to a large database table, Power BI tries to pull in a massive amount of data, which can slow down your refresh times. A SQL query lets you grab only the specific columns and rows you need for your report, significantly reducing the amount of data transferred.
- Pre-aggregation and Complex Logic: You can perform complex calculations, joins, unions, and aggregations directly in your SQL query. This offloads the heavy lifting from Power BI to your database server, which is usually optimized for these kinds of tasks. It's often easier to write a complex
JOINorGROUP BYclause in SQL than it is to replicate that logic using the Power Query interface. - Data Shaping at the Source: Want to rename columns, calculate a new field, or filter out irrelevant information before it ever touches Power BI? A SQL query allows you to shape the data exactly as you need it, creating a clean, report-ready table from the outset.
- Leverage Existing Queries: If your organization already has a library of trusted, vetted SQL queries for reporting, you can simply paste them into Power BI. This saves time and ensures consistency across different reports and platforms.
What You'll Need to Get Started
Before you begin, make sure you have the following pieces of information ready. Gathering these upfront will make the process much smoother.
- Power BI Desktop: You'll need the free Power BI Desktop application installed on your computer.
- Access to a SQL Database: This guide works for most common SQL databases, including SQL Server, MySQL, PostgreSQL, Oracle, and others supported by Power BI.
- Database Credentials: You’ll need the server name (or IP address), the database name, and your login credentials (username and password or other authentication method).
- Your SQL Query: Have the SQL query you want to run written and ready. For this tutorial, we'll use a sample query, but you can substitute your own.
Step-by-Step: Connecting to a SQL Database with a Query
With your prerequisites in hand, let's connect Power BI to your database and execute the query.
Free PDF · the crash course
AI Agents for Marketing Crash Course
Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.
1. Open Power BI and Select Your Data Source
First, launch Power BI Desktop. In the Home tab of the ribbon, click on Get Data. This will open a list of common data sources. Since we're connecting to a SQL database, you'll want to select the appropriate one. For this example, we'll use SQL Server database. If you're using a different database like PostgreSQL or MySQL, you can find it by clicking Get Data and then More... to search for your specific connector.
2. Enter Your Server and Database Information
A dialog box will appear asking for your server and database information.
- Server: Enter the name or IP address of your database server.
- Database (optional): You can enter the specific name of the database you want to query. If you leave this blank, you'll be able to select it in a later step.
Now, before you click "OK," look for the Advanced options dropdown. This is where you'll be able to enter your custom query.
You’ll also see a field for Data Connectivity mode with two primary choices:
- Import: This is the default and most common mode. Power BI copies the data from your database and stores it within the Power BI file (.pbix). This allows for faster performance when interacting with visuals, but you need to schedule data refreshes to keep your report up to date.
- DirectQuery: This mode creates a live connection to your database. Power BI sends queries to the source every time a user interacts with a visual. It's ideal for very large datasets or when you need real-time data, but it can be slower depending on the performance of your database.
For most reports, Import is the best choice. Let's proceed with that.
3. Input Your Custom SQL Statement
Click on Advanced options to reveal the SQL statement (optional) text box. This is where you'll paste your query.
Let’s use a simple example. Imagine we have a sales database with tables for sales, products, and customers. We want to pull a summary of sales performance by product category for the last quarter.
Here's a sample query you can copy and paste into the box:
SELECT
c.CategoryName,
SUM(s.SaleAmount) AS TotalSales,
COUNT(s.OrderID) AS NumberOfOrders
FROM Sales AS s
JOIN Products AS p ON s.ProductID = p.ProductID
JOIN Categories AS c ON p.CategoryID = c.CategoryID
WHERE s.OrderDate >= '2023-10-01' AND s.OrderDate <= '2023-12-31'
GROUP BY c.CategoryName
ORDER BY TotalSales DESC,After pasting your query into the text box, click OK.
4. Authenticate and Preview Your Data
Next, Power BI will ask for your credentials to access the database. Choose the correct authentication method (e.g., Windows Authentication or Database credentials with a username and password) and enter your login information. Once successfully connected, Power BI executes the query against your database.
A Navigator window will appear, showing you a preview of the data returned by your query. It will display a table named "Query1" by default. If it looks correct, your next step is to either Load it or Transform Data.
- Load: This loads the data directly into your Power BI data model, making it immediately available for building visuals.
- Transform Data: This opens the Power Query Editor, where you can perform additional cleanup tasks like changing data types or splitting columns. It's generally a good practice to click Transform Data to quickly check that data types (like numbers, dates, and text) have been correctly identified.
Since our SQL query already did the heavy lifting, we can likely just click Load.
Building Your Report from the Query Data
Once your data is loaded, you'll see your table (still likely named "Query1") appear in the Fields pane on the right side of the Power BI window. You can easily rename this by right-clicking on it and choosing Rename to give it a more descriptive name, like "Quarterly Sales Summary".
Now, all the columns from your SQL query — CategoryName, TotalSales, and NumberOfOrders — are available as fields. You can drag and drop these onto the report canvas to start building visuals.
For example:
- Select the Clustered column chart visual from the Visualizations pane.
- Drag
CategoryNamefrom the Fields pane to the X-axis field in the Visualizations pane. - Drag
TotalSalesto the Y-axis field.
Just like that, you have a chart showing your total sales by product category, all based on the precise data you pulled with your custom SQL query. You can continue adding more visuals, such as a card to show the grand total of sales or a table to display the detailed numbers.
Free PDF · the crash course
AI Agents for Marketing Crash Course
Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.
Tips for Working with SQL Queries in Power BI
To take your skills to the next level, here are a few best practices to keep in mind.
- Use Parameters for Dynamic Reports: Instead of hardcoding values like dates in your query, you can use Power Query parameters. This allows you to create filters in your report that dynamically change the query sent to the database. For example, you could create
StartDateandEndDateparameters and then reference them in your SQLWHEREclause. - Prioritize Performance: Your query should be as efficient as possible. Only
SELECTthe columns you truly need. Avoid usingSELECT *. Ensure yourWHEREclauses are specific enough to limit the result set. Heavy data manipulation should happen in the SQL query on the database, not in Power Query, as databases are optimized for this work. - Understand Query Folding: "Query folding" is a Power BI process where it tries to combine the steps you take in the Power Query Editor (like filtering or sorting) into a single, comprehensive SQL statement. When you use your own custom SQL query, this can sometimes prevent query folding on subsequent steps. If you plan to do a lot of additional transformation in Power Query, it may be better to connect to the table directly and let Power Query build the statement for you.
Final Thoughts
Creating a Power BI report from a direct SQL query is a highly effective way to gain speed, precision, and control over your data. By shaping the data at the source, you can build leaner, faster, and more maintainable reports, letting you focus on creating compelling visualizations and uncovering insights.
For many teams, especially in marketing and sales, building reports doesn't have to involve writing SQL or navigating complex tools like Power BI. Often, the challenge is simply getting all your data from sources like Google Analytics, Shopify, Salesforce, and Facebook Ads into one place. That's why we built Graphed . We automate the entire data connection and reporting process, allowing you to connect your key platforms and simply ask for the dashboards and reports you need in plain English - no query writing required.
Related Articles
Facebook Ads for Real Estate Agents: The Complete 2026 Strategy Guide
Master Facebook ads for real estate agents in 2026. Learn targeting, ad formats, budgets, and creative best practices to generate more leads.
Facebook Ads for Movers: The Complete 2026 Strategy Guide
Learn how to run Facebook ads for movers that actually generate booked jobs—not just clicks. Budget, targeting, funnel strategy, and creative that converts.
Facebook Ads for Web Designers: The Complete 2026 Strategy Guide
Learn how to use Facebook ads to attract high-value web design clients in 2026. A complete 7-step system for agencies and freelancers.