How to View SQL Query in Power BI
Ever wondered what’s happening behind the scenes inside Power BI when you connect to a SQL database? Each time you filter a column, remove a row, or merge tables using the Power Query editor, Power BI is translating those clicks into instructions. For many data sources, those instructions are turned into a native SQL query that gets sent directly to your database. Seeing that exact SQL query can be incredibly useful for troubleshooting, optimization, or just satisfying your curiosity. This article will show you a few straightforward methods to view the SQL query in Power BI that runs against your data source.
Why Would You Want to View the SQL Query?
Dipping into the SQL code that Power BI generates isn't just an academic exercise. It has practical applications that can help you build better, faster, and more reliable reports. Here are a few common reasons to take a look.
- Troubleshooting and Debugging: Is a filter not behaving as you expect? Are you getting unexpected results after a merge? Viewing the SQL query can immediately show you the
WHEREclause orJOINstatement Power BI constructed, often revealing the source of the problem in seconds. - Performance Optimization: If your data refresh is taking forever, the generated SQL is often the prime suspect. A poorly constructed query can put a massive strain on your database. By inspecting the query, you can see if Power BI is doing something inefficient and then adjust your Power Query steps to generate a more performant query.
- Learning SQL: Power Query is essentially a visual SQL builder. Watching how your clicks and transformations translate into SQL is one of the best ways to learn the language. It’s like having a tutor show you exactly how to write a query to achieve a specific outcome.
- Validation and Documentation: Sometimes you need to prove exactly where your data is coming from for auditing or compliance. Being able to export the exact SQL query provides a clear, documented trail of how the data was pulled and transformed from the source.
Method 1: The "View Native Query" Right-Click Trick
The simplest and quickest way to see the generated SQL is by using the "View Native Query" option in the Power Query Editor. However, there's a catch: this option is only available when your transformation steps support query folding.
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.
What is Query Folding?
Query folding is the process where Power BI translates your transformations (like filtering, sorting, and grouping) into a single SQL statement that can be executed directly by the source database. Think of it like this: instead of pulling an entire 10-million-row table into Power BI and then filtering it, query folding tells the database, "Hey, only send me the 100 rows I actually need."
This is massively more efficient. It speeds up report refreshes, reduces the load on your local machine, and leverages the processing power of your database server.
Most standard transformations on relational databases like SQL Server, PostgreSQL, or MySQL support folding. This includes:
- Filtering rows (
WHERE) - Choosing or removing columns (
SELECT) - Grouping and summarizing (
GROUP BY) - Merging relational tables (
JOIN) - Renaming columns (
AS)
Transformations that typically break query folding include things like applying a custom M function, changing a column data type in a way the database can't understand, or indexing a column.
How to Use "View Native Query"
Let’s walk through the steps. For this example, we’ll connect to a SQL Server database.
- From the Power BI Desktop home ribbon, click Get data and select SQL Server. Connect to your database and select a table.
- Click Transform Data to open the Power Query Editor.
- In the Power Query Editor, perform a few transformations that support query folding. For example, use the column filter to select only certain values, and use the "Choose columns" feature to remove a few fields.
- In the Query Settings pane on the right, you’ll see a list of your Applied Steps. Right-click on the last step in your list.
If query folding is active, you will see an option for View Native Query. Click it!
A new window will appear showcasing the beautifully generated SQL statement. You'll see how your "Filtered Rows" step became a WHERE clause and your "Removed Columns" step refined the SELECT statement.
If "View Native Query" is grayed out, it means query folding has been broken by one of your steps. To find the culprit, start from your last step and right-click on each preceding step until the option becomes enabled again. The step that caused it to be disabled is the one that broke the folding.
Method 2: The Most Reliable Way with Query Diagnostics
Sometimes, the "View Native Query" option isn't available, even if you think query folding should be working. Or maybe you want a more detailed breakdown of every query Power BI sends. For these situations, Query Diagnostics is your best friend.
This tool records all the nitty-gritty details of the communication between Power BI and your data source, including the exact native queries being sent.
How to Use Query Diagnostics
- With your query open in the Power Query Editor, go to the Tools tab.
- Click on Start Diagnostics.
- Now, trigger a data source interaction. The easiest way to do this is to click "Refresh Preview" in the Home ribbon or click on your very last applied step in the Query Settings pane. This forces Power BI to re-run the query against the database.
- Once the data preview has loaded, go back to the Tools tab and click Stop Diagnostics.
- Your original queries disappear for a moment, and two new folders will appear in the Queries pane on the left: "Diagnostics" and "Parameters." The most useful information is tucked away inside the "Diagnostics" tables. Expand your query, and you’ll see separate rows for each step. Each row logs details like the start time, duration, and most importantly, the
Data Source Querybeing run. - Click on the table for the step you are interested in (usually the final one). Find the column named Data Source Query. The cell in that column will contain the full native SQL that was executed by the database.
Here’s an example of what you might see in that column:
select "ProductKey", "ProductAlternateKey", "EnglishProductName", "Color", "StandardCost", "ListPrice", "Size" from "dbo"."DimProduct" where "Color" = 'Red'
Query diagnostics is amazingly powerful because it works even when query folding is broken. It will show you the SQL query for the foldable part of your logic and then you can see how Power BI handled the rest of the steps internally with its M engine.
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.
What About DAX and DirectQuery?
The methods above are for viewing the SQL generated by Power Query to import and transform your data. What about the live queries sent when someone interacts with a report built using DirectQuery mode?
In DirectQuery, no data is imported into Power BI. Instead, every time you click a slicer or filter a chart, Power BI generates a DAX query, translates it on the fly into SQL, and sends it to your database.
To see these queries, you can use the Performance Analyzer.
- In the Power BI Desktop report view, go to the View ribbon and check the box for Performance Analyzer.
- A new pane will open. Click Start recording.
- Now, interact with your report visuals. Click on a bar in a chart, select a value in a slicer, or refresh the entire page.
- The Performance Analyzer will log every action each visual takes, showing you the duration for the DAX query, visual display, and other operations.
- Expand a visual's entry and you’ll see an option to Copy query under the "DAX query" section.
Keep in mind, this gives you the DAX query, not the final SQL. The translation from DAX to SQL is handled internally by Power BI. For most diagnostic purposes, seeing the DAX is enough to understand what's being asked of the data. To see the final translated SQL for DirectQuery reports, you would typically need to use an external tool like SQL Server Profiler, which monitors activity on the database itself.
Final Thoughts
Knowing how to view the underlying SQL in Power BI demystifies the data transformation process and gives you incredible power to debug, optimize, and validate your reports. Whether you use the quick "View Native Query" option or the more detailed Query Diagnostics tool, peeking behind the curtain puts you in much greater control of your BI reports.
While diving into the SQL is powerful, it’s also a time-consuming process that pulls you away from finding actual insights. We built Graphed because we believe getting answers from your data shouldn't require you to become a database detective. Instead of building manual transformation steps in Power Query and troubleshooting the generated SQL, you simply connect your data sources and ask questions in plain English. Graphed automatically generates the visualizations and live dashboards you need, letting you focus on making smart decisions, not debugging queries.
Related Articles
Facebook Ads for Estheticians: The Complete 2026 Strategy Guide
A practical 2026 guide to running Facebook ads for estheticians, from local targeting and offers to creative testing, booking funnels, and performance tracking.
Facebook Ads for Wedding DJs: The Complete 2026 Strategy Guide
Learn how wedding DJs can use Facebook and Instagram ads in 2026 to reach engaged couples, promote event packages, generate better inquiries, and book more dates without wasting budget on boosted posts.
Facebook Ads for Personal Organizers: The Complete 2026 Strategy Guide
Learn how professional organizers can use Facebook ads in 2026 to attract local clients, showcase transformations, retarget website visitors, and turn inquiries into booked organizing projects.