Sub topic 10
Drafting and explaining queries in DAX query view
This chapter assumes you can write basic DAX measures and understand filter context. It also assumes a star-schema model with a Sales fact table and Product and Date dimensions, like the ones in earlier chapters. The model should already have a base measure named [Total Sales].
What DAX query view is
DAX query view is a tab in Power BI Desktop, next to Report, Table, and Model views. You write a DAX query, run it, and see the result as a table. Nothing in the report changes when you run a query.
A DAX query differs from a measure in three ways:
- A query starts with
EVALUATEand returns a table. A measure returns a single value. - A query can contain a
DEFINEblock that declares measures, variables, or columns. These exist only while the query runs. - A query has no filter context from a visual. You build the grouping yourself, for example with
SUMMARIZECOLUMNS.
This makes the view a safe place to test DAX. You can try a measure against the model and look at the numbers before you add the measure to the model.
Where Copilot fits
Copilot in DAX query view has a chat pane beside the editor. It supports three jobs:
- Drafting a query from a plain-language request.
- Explaining an existing query, or a DAX function or concept.
- Revising a query after you ask for a change or paste an error.
Copilot reads your model's metadata: table, column, and measure names, plus any descriptions and AI instructions. Weak names and missing descriptions lead to weak queries. The earlier chapters on model preparation apply here as well. Copilot proposes a query and you decide whether to run it, so the review step is yours.
Writing a prompt for a query
A query prompt needs four things: the measure or measures, the grouping columns, any filter, and the sort order. Compare these two prompts.
Show me sales by category.Write a DAX query that returns Total Sales and Average Order Value for each Product[Category], sorted by Total Sales from highest to lowest. Define Average Order Value in the query as Total Sales divided by the distinct count of Sales[OrderID], and return a blank when there are no orders.The first prompt leaves Copilot to guess which measure, which category column, and what "sales" means. The second names the columns, gives the business definition, and states the edge case. Naming [Total Sales] tells Copilot to reuse your existing measure instead of writing a new SUM.
Useful habits:
- Use the exact table and column names as they appear in the model.
- Say whether you want a new measure defined in the query or an existing measure reused.
- Ask for the sort order. Without
ORDER BY, row order is not guaranteed. - Keep one request per prompt, then refine in follow-ups.
Reading the query Copilot returns
For the prompt above, Copilot might return a query like this one.
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] DESCRead it in four parts before you run it.
DEFINE MEASURE Sales[Average Order Value]creates a measure that lives only in this query. TheSales[...]part names the home table the measure would have if you later add it to the model.DIVIDEreturns a blank when the denominator is zero, which satisfies the "no orders" requirement. A plain/would return an error or infinity.SUMMARIZECOLUMNSgroups by'Product'[Category]and adds one column per name and expression pair. The quoted text is the column header in the result.ORDER BY [Total Sales] DESCrefers to the result column namedTotal Sales, not to a model object.
Check the names against the model. Copilot can use a column that looks plausible but does not exist, or pick the wrong table for a column that appears in two places. If you run it and see an error, copy the message into the chat and ask Copilot to fix the query.
Testing a measure before adding it to the model
Run the query and look at the result table. Then compare it with something you already trust. The simplest check is a one-row query that returns grand totals.
EVALUATE
ROW (
"Total Sales", [Total Sales],
"Orders", DISTINCTCOUNT ( Sales[OrderID] ),
"Average Order Value",
DIVIDE ( [Total Sales], DISTINCTCOUNT ( Sales[OrderID] ) )
)Check three things:
- The
Total Salesfigures in the category result add up to the grand total from this query. If a category is missing, a relationship or a blank key may be dropping rows. - The category values match a table or matrix visual in the report that uses the same fields.
- The average order value equals the total divided by the order count, calculated by hand for at least one category.
Once the numbers agree, DAX query view offers an option to update the model with the changes. This adds the DEFINE MEASURE definitions to the model as real measures. Do this only after the checks pass. If you add a measure and then change the query, the model copy does not change until you update it again.
Asking Copilot to explain
Explanations are useful when you inherit a query or when Copilot returns a function you do not recognize. Ask about a specific piece, not the whole query.
Explain what SUMMARIZECOLUMNS does in this query, and why the ORDER BY refers to [Total Sales] rather than to the measure definition.Explain the difference between DISTINCTCOUNT ( Sales[OrderID] ) and COUNTROWS ( Sales ) for this model.A good explanation names the evaluation steps and the effect of filter context. Treat explanations as a second opinion, not as proof. If one says a query "returns all customers," run the query and count the rows. Explanations of function behavior can also be wrong in details such as how blanks are handled, so check anything important against the DAX function documentation.
Refining with follow-up requests
After the first result, you can keep working in the same chat. Short, concrete follow-ups work best:
- "Limit the result to the top five categories by Total Sales."
- "Add a column for Sales YoY % using the existing measure."
- "Filter to the year 2025 using the Date table."
A top-N request is a good place to check the output. Copilot may return something like this.
EVALUATE
TOPN (
5,
SUMMARIZECOLUMNS (
'Product'[Category],
"Total Sales", [Total Sales]
),
[Total Sales], DESC
)
ORDER BY [Total Sales] DESCTOPN selects the rows but does not promise that they come back in order. The final ORDER BY sorts them. If the draft leaves it out, add it yourself or ask Copilot to add it.
The year-over-year measure from the earlier chapter depends on a date table marked as the model's date table. If a follow-up that uses SAMEPERIODLASTYEAR returns blanks, check the date table before you blame the prompt.
Common problems
| Symptom | Likely cause | What to do |
|---|---|---|
| Error naming an unknown column or table | Copilot invented or misspelled a name | Compare the name with the Data pane, then paste the error into the chat |
| Result has one row with the grand total | The grouping column was left out | Ask for the grouping column by exact name |
| Totals differ from a visual | The visual has a slicer or page filter that the query lacks | Add the same filters to the query, or clear them in the report |
| Blank where a number is expected | A relationship is missing or inactive | Check the Model view |
| Rows come back in random order | No `ORDER BY` | Add `ORDER BY` on the result column |
| Copilot defines a new `SUM` instead of using `[Total Sales]` | The prompt did not name the existing measure | Name the measure in the prompt, or add a note in AI instructions |
A repeatable routine
- Write a prompt that names measures, columns, filters, and sort order.
- Read the draft and compare each name with the model.
- Ask for an explanation of any function you cannot explain yourself.
- Run the query and check the totals against a one-row grand total and a visual.
- Revise with one follow-up at a time.
- Update the model only when the numbers match.
Exercise
Copilot returns a query that groups Total Sales by 'Product'[Category] and uses TOPN to keep the top three categories. The results appear in a different order each time you run it. What is missing, and how do you confirm the numbers are correct before adding any measure from the query to the model?
Show answer
The query needs an ORDER BY clause, for example ORDER BY [Total Sales] DESC, because TOPN selects rows but does not guarantee their order. To confirm the numbers, run a ROW query that returns the grand [Total Sales] and check that the three categories do not exceed it. Then compare each category with the same fields in a table visual in the report. Check that any filters on the report page are also in the query. If everything matches, use the update-model option to add the measures. If not, revise the prompt or the DAX first.