Sub topic 9
Generating DAX Measures with Copilot
Copilot can draft DAX measures from a plain-language description, and it can explain measures you already have. This chapter covers how to ask for a measure, what a good result looks like, the errors that appear most often, and how to confirm that a measure is correct before you keep it.
Prerequisites
This chapter assumes you already know the following:
- Basic DAX: the difference between a measure and a calculated column, and what filter context is.
- A star-schema semantic model with a fact table (here
Sales) and dimension tables (hereProductandDate). - How to create a measure in Power BI Desktop or in the Power BI service.
- How to prepare the model and write prompts, as covered in the earlier chapters on model readiness and prompt writing.
What Copilot does with a measure request
Copilot reads the metadata of your semantic model: table names, column names, existing measures, relationships, descriptions, and AI instructions. It does not read your row data to decide which columns to use. It then produces a DAX expression and a short explanation of what the expression calculates.
Two consequences follow.
- The quality of the result depends on how clearly the model describes itself. A column named
Amt2gives Copilot far less to work with thanSalesAmount. - Copilot proposes. It does not know your business rules. "Sales" might mean gross sales, net sales, or sales after returns, and Copilot will choose one unless you say which.
You can ask for a measure in two places:
- In the Copilot pane in Power BI Desktop, with a request such as "Write a measure that...".
- In the quick measure experience or DAX query view, where Copilot can suggest and explain DAX (the next chapter covers DAX query view in detail).
Start with base measures
Most useful measures build on a small set of base measures. Create these yourself first, so Copilot can reference them instead of rewriting the same logic each time.
Total Sales = SUM ( Sales[SalesAmount] )Once [Total Sales] exists, a request such as "average order value" can reuse it. This keeps one definition of sales in the model. If the definition changes later, you change it in one place.
A base measure also reduces a common failure. When no base measure exists, Copilot may write SUM ( Sales[SalesAmount] ) inline inside each new measure. The results match today, but the logic is duplicated and can drift.
Write the request
A measure request needs three things: the business definition, the inputs, and the edge cases. Compare these two prompts.
Weak:
Give me average order value.Better:
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.The better prompt states the formula in business terms, so Copilot does not have to guess whether "order" means an order line or a whole order. It also asks for the edge case, so the result does not show an error when a filter leaves no orders.
A result for that prompt could look like this:
Average Order Value =
DIVIDE ( [Total Sales], DISTINCTCOUNT ( Sales[OrderID] ) )This is correct for several reasons:
- It reuses
[Total Sales]. DISTINCTCOUNTcounts each order once, even when an order has many lines.DIVIDEreturns a blank when the denominator is zero, instead of an error. A blank is the normal "no data" result in a visual.
If you want a different result for the zero case, DIVIDE takes an optional third argument. For example, DIVIDE ( [Total Sales], DISTINCTCOUNT ( Sales[OrderID] ), 0 ) returns 0 instead of a blank. Decide which you want and say so in the prompt.
What to include in a measure prompt
| Element | Example wording | Why it helps |
|---|---|---|
| Result you want | "a measure that returns..." | Prevents a calculated column or a query in place of a measure |
| Formula in business terms | "total sales divided by distinct orders" | Removes ambiguity about which columns to use |
| Existing measures to reuse | "use [Total Sales]" | Keeps one definition of each number |
| Filter or time logic | "for the same period last year" | States the filter context |
| Edge cases | "handle no orders" | Avoids errors and surprising blanks |
Time intelligence measures
Time intelligence is where generated DAX most often fails quietly. A year-over-year comparison is a good example.
Sales LY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
Sales YoY % =
DIVIDE ( [Total Sales] - [Sales LY], [Sales LY] )This pair works only when the model has a proper date table:
- The
Datetable has one row per day with no gaps, covering every date in the fact table. - The
Date[Date]column is unique. - The table is marked as the date table in the model.
Saleshas an active relationship toDateon the date column.
If any of these is missing, SAMEPERIODLASTYEAR can return blanks or wrong values, and nothing in the formula will tell you why. Copilot cannot fix a model problem by writing different DAX. If the first result looks wrong, check the date table before you rewrite the prompt.
Notice also that Sales YoY % uses [Sales LY] as its denominator. When there is no prior-year data, DIVIDE returns a blank instead of a divide-by-zero error.
Ask Copilot to explain a measure
Explanation is often more valuable than generation. When you inherit a model, select a measure and ask Copilot what it calculates. A good explanation names:
- the base columns or measures the expression uses,
- the filters it applies or removes,
- what happens in edge cases.
Use the explanation as a reading aid, then check it against the formula yourself. For example, if you ask about this measure:
Sales All Regions =
CALCULATE ( [Total Sales], REMOVEFILTERS ( Region[RegionName] ) )A correct explanation says that the measure ignores any filter on RegionName, so a visual split by region shows the same total on every row. If the explanation says "sales for the selected region", it is wrong, and you should not trust the rest of it either.
Common errors in generated DAX
The following problems come up repeatedly. Look for them in every result.
- Wrong column or table. Copilot picks a similarly named column, such as
OrderDateinstead ofShipDate. Fix: name the column in the prompt, or add a description to the column in the model. - Wrong aggregation.
COUNTorCOUNTROWSis used whereDISTINCTCOUNTis needed, which inflates counts when one order has many lines. - Duplicated logic. The expression repeats a calculation that already exists as a measure.
- Missing date table. Time intelligence functions are applied to a column that is not a marked date table (see above).
- Unhandled division. The
/operator is used whereDIVIDEis safer. - Row context confusion. A measure uses a column reference outside an aggregator, such as
Sales[SalesAmount] * 2. A measure needs an aggregation likeSUMX ( Sales, Sales[SalesAmount] * 2 ). - Business definition mismatch. The DAX runs and the numbers look plausible, but "active customer" means something different from your definition. No syntax check detects this.
Iterate with follow-up prompts
You do not have to get the measure right in one request. Follow up with short, specific corrections:
- "Use
[Total Sales]instead of summingSalesAmountagain." - "Count orders by
OrderID, not by rows." - "Return 0 instead of blank when there are no orders."
- "Explain what this measure does when a filter is applied to
Product[Category]."
Each follow-up should change one thing. If you change several things at once and the result breaks, you cannot tell which change caused it.
Test a measure before you keep it
Do not add a measure to the model until you have tested it. A safe way is to define the measure in a query, so it exists only for that run, and compare the output with a visual or a known total. DAX query view supports this:
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] DESCThe DEFINE MEASURE block creates the measure for this query only. Nothing changes in the model. Check these points in the result:
- The sum of
Total Salesacross categories equals the grand total in a trusted report. - For one category, calculate the average order value by hand from a small export and compare.
- Try a category with no orders, if you have one, to confirm the blank behavior.
When the numbers match, add the measure to the model. In Desktop, DAX query view offers an option to update the model with the defined measure. You can also create the measure manually with the same expression.
Good practice for measures you keep
- Give each measure a clear name and a description. Copilot uses both when it answers later questions.
- Set a format string (currency, percent) so visuals display the value correctly.
- Put related measures in a display folder.
- Keep one definition per business concept. If two measures calculate "sales" differently, Copilot may pick either one.
- Review every generated measure as you would a colleague's code before it goes into a shared model.
Summary of the chapter
- Create base measures first and tell Copilot to reuse them.
- State the business formula, the inputs, and the edge cases in each prompt.
- Time intelligence depends on a marked date table, and Copilot cannot repair a missing one.
- Generated DAX can be valid and still wrong, so test it against a known total.
- Use
DEFINE MEASUREin a query to try a measure without changing the model.
Exercise
You ask Copilot for "average order value" and receive Average Order Value = [Total Sales] / COUNTROWS ( Sales ). The sales table has one row per order line. Name two problems with this measure and write a corrected version.
Show answer
First, COUNTROWS ( Sales ) counts order lines, not orders, so any order with several lines is counted several times and the average is too low. Second, the / operator returns an error or infinity when there are no rows, instead of a blank. The corrected version is: Average Order Value = DIVIDE ( [Total Sales], DISTINCTCOUNT ( Sales[OrderID] ) ). It counts each order once and returns a blank when the denominator is zero.
Exercise
Copilot returns Sales LY = CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ), but the measure shows blanks for every month. The DAX looks right. What do you check in the model, and why?
Show answer
Check the date table. It must contain one row for every day with no gaps, have a unique Date column, be marked as the model's date table, and have an active relationship from Sales to Date. If any of these is missing, SAMEPERIODLASTYEAR cannot shift the filter correctly and returns blanks or wrong values. Rewriting the prompt does not fix a model problem.
Exercise
Why is it better to test a new measure with DEFINE MEASURE in DAX query view than to add it to the model straight away?
Show answer
DEFINE MEASURE creates the measure only for that query, so the model stays unchanged. You can compare the output with a known total or a visual, find mistakes, and only then add the measure. A wrong measure added directly to a shared model could be used in reports before anyone notices the error.