Sub topic 10
Drafting and explaining queries in DAX query view
DAX query view is a tab in Power BI Desktop and the Power BI service where you write and run DAX queries against your semantic model. Copilot works inside it. It can draft a query from a description, explain an existing query, and help fix one that fails. This chapter shows how to use it to test a measure before it goes into the model.
This chapter assumes you can write basic DAX measures, understand filter context, and have a star-schema model with a Total Sales measure. It also assumes Copilot is already enabled, as covered in earlier chapters.
Why use DAX query view
A report visual shows only the result. A query shows the result as a table you can read, compare, and change. You can also define a measure inside the query with DEFINE. That measure exists only while the query runs, so you can try it without changing the model.
This makes DAX query view a good place to check a measure Copilot wrote. You can compare its numbers with a visual and add it to the model only when they match.
The shape of a DAX query
Every query returns a table with EVALUATE. The optional DEFINE block comes first and holds local measures or variables. ORDER BY comes last.
DEFINE
MEASURE Sales[Average Order Value] =
DIVIDE ( [Total Sales], DISTINCTCOUNT ( Sales[OrderID] ) )
EVALUATE
SUMMARIZECOLUMNS (
'Product'[Category],
"Total Sales", [Total Sales],
"Average Order Value", [Average Order Value]
)
ORDER BY [Total Sales] DESCThis query defines Average Order Value for this run only. SUMMARIZECOLUMNS groups by product category and returns both measures for each group. The result is sorted by sales, highest first.
Drafting a query with Copilot
Open DAX query view and open the Copilot pane. Describe the result you want as a table. Name the grouping column, the measures, and the sort order.
Write a measure that returns the average order value: total sales divided by the number of distinct orders. Handle the case where there are no orders.Copilot can return a measure like this one:
Average Order Value =
DIVIDE ( [Total Sales], DISTINCTCOUNT ( Sales[OrderID] ) )DIVIDE returns a blank instead of an error when the denominator is zero. That covers the "no orders" case from the prompt.
To test it, ask Copilot to put the measure in a query grouped by category. Review the query before you run it. Check that the table and column names exist in your model, because Copilot can use a name that looks right but is not there.
Asking Copilot to explain a query
Copilot can also explain a query you did not write. Paste it in and ask what it does. A good explanation covers three things:
- Which columns the query groups by.
- What each measure calculates, and in what filter context.
- How the result is sorted or filtered.
Compare the explanation with the query itself. If Copilot says a query groups by region but the code uses category, the explanation is wrong. Treat explanations as a starting point for your own reading.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Query fails with "column cannot be found" | Copilot used a name that is not in the model | Fix the name, or ask Copilot to use the exact names |
| Totals differ from the visual | The visual has a slicer or filter the query lacks | Add the same filter, or clear it in the visual |
| Blank in every row | The measure depends on a date table that is missing or not marked | Mark the date table, then run again |
| Query runs but is slow | It groups by a high-cardinality column | Group by a smaller column, or filter first |
Exercise
Copilot drafts a query that defines Average Order Value with DEFINE and groups by product category. It runs, but the grand total for Average Order Value is different from the number in your report card. Name two things to check before you add the measure to the model.
Show answer
First, check that the query and the report card use the same filters, because a slicer or page filter on the card changes the result. Second, check the measure logic. Confirm that the order count uses DISTINCTCOUNT on the order ID column. Also remember that an average over a grand total is not the same as the average of the category averages.