How to Get DAX Query in Power BI
Power BI is an incredible tool for visualizing data, but sometimes you need to peek under the hood to see how it's actually getting its numbers. Knowing how to grab the DAX query that Power BI generates behind the scenes is a critical skill for debugging, optimizing, and truly mastering your reports. This article will walk you through exactly how to do it using simple, built-in tools and a popular free third-party app.
Why Get the DAX Query in the First Place?
Before jumping into the "how," let's quickly cover the "why." Extracting the DAX query generated by a visual isn't just an academic exercise. It serves several practical purposes:
- Debugging and Troubleshooting: Is a card visual showing the wrong total? A bar chart not applying a filter correctly? By looking at the exact query Power BI is running, you can spot incorrect logic, filter context issues, or problems in your DAX measures that aren't obvious on the surface.
- Performance Optimization: If a report is running slowly, the culprit is often an inefficient DAX query. By capturing the query for your slowest visuals, you can analyze what's causing the slowdown. You might discover overly complex calculations or inefficient filtering patterns that can be rewritten for better performance.
- Learning and Reusability: Seeing how Power BI constructs a query from your visual settings is one of the best ways to learn DAX. It helps you understand how functions like
SUMMARIZECOLUMNSandCALCULATETABLEwork in a real-world context. You can also repurpose these machine-generated queries in other tools or projects. - Using External Tools: Advanced tools like DAX Studio offer powerful features for formatting, analyzing, and executing DAX queries. To use them, you first need to get the query out of Power BI.
Method 1: Using the Performance Analyzer in Power BI Desktop
The easiest and most direct way to get a DAX query for any visual is by using the Performance Analyzer, a tool built directly into Power BI Desktop. It’s designed to log the performance of your report elements, and as a happy byproduct, it shows you the DAX query for each visual.
Here’s how to use it, step-by-step:
Step 1: Open the Performance Analyzer Pane
In Power BI Desktop, navigate to the View tab in the ribbon. In the "Show panes" section, click on Performance Analyzer. A new pane will appear on the right side of your screen.
Step 2: Start Recording
Inside the Performance Analyzer pane, you'll see a green button labeled Start recording. Click it. From this point forward, Power BI will log the performance data for every interaction you have with the report.
Step 3: Interact with Your Report Visuals
Now, interact with your report page. A good way to start is by refreshing the visuals. You can do this by clicking Refresh visuals inside the Performance Analyzer pane. This forces Power BI to re-run the queries for every visible element on the page.
You can also click on a slicer, filter a visual, or cross-highlight a chart. Each one of these actions will trigger new queries, and the Performance Analyzer will log them all individually.
Step 4: Analyze the Results
Once you’ve interacted with your report, you’ll see a list of actions and visual elements in the Performance Analyzer. Each visual on your page will be listed, and you can expand its entry by clicking the small + sign next to its name.
When you expand it, you'll typically see a few metrics:
- DAX query: This is the time it took the Power BI engine to execute the DAX query. This is what we're looking for!
- Visual display: The time it took for the visual to render on your screen after getting the data.
- Other: The time the visual spent waiting for other operations to complete.
Step 5: Copy the DAX Query
Under the performance metrics for your visual, you'll see a link that says Copy query. Click it.
That's it! The full DAX query that Power BI generated and sent to its Analysis Services engine is now on your clipboard. You can paste it into a text editor like VS Code or Notepad++ to analyze it.
The copied query will often look dense and hard to read at first, like this:
// DAX Query DEFINE VAR __DS0Core = SUMMARIZECOLUMNS( ROLLUPADDISSUBTOTAL('Date'[Calendar Year], "IsGrandTotalRowTotal"), ROLLUPADDISSUBTOTAL('Product'[Category], "IsGrandTotalRowTotal"), "Total_Sales", 'Measures'[Total Sales] )
EVALUATE __DS0Core ORDER BY 'Date'[Calendar Year], [IsGrandTotalRowTotal] DESC, 'Product'[Category], [IsGrandTotalRowTotal] DESC
We'll talk about how to make sense of this in a moment.
Method 2: Using DAX Studio for Deeper Analysis
The Performance Analyzer is great for a quick look, but for serious performance tuning and debugging, you’ll want to use an external tool called DAX Studio. It's a free and incredibly powerful tool built by the community for working with DAX.
What is DAX Studio?
DAX Studio connects directly to your Power BI data model and gives you a full-featured environment for writing, executing, and analyzing DAX queries. It offers advanced features like server timing traces, query plans, and a robust query editor that are missing from Power BI itself.
Step 1: Get DAX Studio and Connect It
First, download and install DAX Studio from daxstudio.org. Once installed, follow these steps:
- Open your
.pbixfile in Power BI Desktop. - Launch DAX Studio.
- In the connection dialog, DAX Studio should automatically detect your open Power BI file under the PBI / SSDT Model option. Select it and click Connect.
Step 2: Trace the Queries
Once connected, you'll see your data model's tables and measures on the left. To capture queries from your Power BI report, you need to turn on tracing.
- Go to the Home tab in the DAX Studio ribbon.
- Click the All Queries button. This will start "listening" for any query that your Power BI report sends to the engine.
- Switch back to your Power BI report.
- Interact with it just like you did with the Performance Analyzer — click a slicer, refresh the page, or apply a filter.
- Now, back in DAX Studio, you'll see a new table at the bottom in the All Queries tab. Each query generated by your interactions will be logged as a separate row.
Step 3: View and Format the Query
You can see the query text in the table, but to work with it properly, double-click on any row. This will automatically copy the full DAX query into the main editor window upstairs. From here, you can format, edit, and re-run the query directly within DAX Studio to test changes.
Making Sense of the Query You've Captured
Okay, so you have a blob of DAX query text. Now what? These machine-generated queries can be intimidating. Here are a few tips to break them down.
1. Format Your Code
A query that isn’t indented is nearly impossible to read. The first step is always to format it. DAX Studio has a "Format Query" button in the ribbon. If you're using a text editor, you can paste your query into an online tool like DAX Formatter and it will clean it up for you.
Before Formatting:
`EVALUATE SUMMARIZECOLUMNS('Product'[BrandName], FILTER('Customer', 'Customer'[Occupation] = "Professional"), "TotalSales", [Total Sales])`
After Formatting:
Much easier to read, right?
2. Understand the Key Functions
Queries generated by Power BI visuals often use a few key functions:
SUMMARIZECOLUMNS: This is the workhorse. It's an efficient function that groups your data by the columns you see on your visual's axes or in its legend (e.g.,'Product'[Category]) and calculates the defined aggregated values (e.g., "Total Sales").CALCULATETABLEandFILTER: These functions are used to apply the filters from your slicers and other visuals on the page.EVALUATE: This is simply the outermost statement that tells the DAX engine to execute the query and return the result as a table.
3. Look for Patterns and Potential Problems
As you review the formatted query, start connecting it back to your visual. Does the SUMMARIZECOLUMNS function include the fields you expected? Do the filters match what you’ve selected on the report page? Look for complex iterator functions (like SUMX) over very large tables, as these can often be a source of performance issues. By isolating and understanding the query, you're empowered to think about how you might rewrite a measure to be more efficient.
Final Thoughts
Learning how to extract and read the DAX queries behind your Power BI visuals is a significant step toward becoming a more proficient data analyst. Using the Performance Analyzer or DAX Studio, you can move from just building reports to truly understanding, debugging, and optimizing how they function from the inside out.
While mastering DAX is a powerful skill, it also shows the technical hurdles involved in traditional BI. We believe getting insights from your data shouldn’t require you to become a query language expert. That’s why we built Graphed. Our platform connects to all your sources — from Google Analytics to Salesforce to Shopify — and lets you build dashboards and ask questions using plain English. Instead of hunting through a tool to copy, paste, and format a query to find a problem, you can simply ask what you want to know and get an immediate, real-time visualization, giving you back hours of your week.
Related Articles
How to Enable Data Analysis in Excel
Enable Excel's hidden data analysis tools with our step-by-step guide. Uncover trends, make forecasts, and turn raw numbers into actionable insights today!
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.