Skip to content
Carmel Eve By Carmel Eve Software Engineer II · 10 min read
Testing AI Changes to Power BI Semantic Models

TLDR; Before you let an AI assistant change a Power BI semantic model, record regression tests and a benchmark from the unchanged model. If the change should move the numbers, work out the new values (without using the new DAX!) and make sure all changes are explained. In my worked example, the tests showed that a performance rewrite, which took a visual from 13.7 s to 1.8 s, didn't change a single value.

Hello again! In my last post, Power BI Modeling MCP Server in VS Code: Dos and Don'ts, I shared practical tips for working with Power BI semantic models through the Power BI Modeling MCP server in VS Code. I said there that the biggest lesson wasn't really about the server at all: before you let an AI assistant change anything, make sure you can prove what changed (true as a general rule, but especially important when working with complex DAX).

So, today I'm going to go through the dev loop I ended up with, and then show it working on a real performance problem. As before, this is aimed at Power BI developers who are curious about AI tooling, rather than AI people who are curious about Power BI.

A dev loop for testing AI changes to Power BI models

An assistant that's confidently wrong some of the time is fine, if you have something other than its word to check against. So, before I let the assistant write anything to a model, I now put three things in place. The final step is making the change itself.

Diagram of the dev loop. Baseline: 1, record regression tests from the unchanged model; 2, take a labelled benchmark run. Change: 3, derive expected values without the new DAX; 4, make the change, run everything and explain every value that moved. The verified results then become the baseline for the next change.

1. Regression tests for every measure

First, record what the model does now, for every measure the report uses, before anything changes.

The best hour you can spend to refine your own data strategy and leverage the latest capabilities on Azure to accelerate your road map.

There are lots of ways to test a semantic model - here's what I did. My tests didn't run in the report, and they didn't go through the MCP server either. Each one was a DAX query sent straight to the deployed semantic model, with its results checked against values recorded earlier. I used Gherkin feature files, run with Behave, which send the queries through the Power BI REST API's executeQueries endpoint (James has written about testing Power BI reports with the ExecuteQueries REST API if you want the details). Because they run outside the assistant, they're repeatable, and anyone can re-run them.

A test looks something like this:

Feature: Product Comparison

    Background:
        Given I am connecting to the 'Product Performance' Power BI Dataset
        And I add a filter for 'Date' where 'Year' is '2024'
        And I add a filter for 'Region' where 'Region' is 'Europe'

    Scenario: Product comparison table
        Given I am summarizing 'Product' and the 'Product Code' column
        When I calculate the following measures over the table
            | measure        |
            | Total Sales    |
            | Product Median |
        Then the table returned should contain the following results to 4 decimal places
            | Product[Product Code]:string | [Total Sales]:double | [Product Median]:double |
            | P-101                        | 24310.0              | 18250.5                 |
            | P-117                        | 21980.0              | 18250.5                 |

Each scenario becomes one DAX query, and the table at the bottom holds the recorded values.

To make them much more useful, I:

  • Mirrored each visual in a query, grouping by the same columns and calculating the same measures, with one test file per report page.
  • Set every filter in every query, giving a value for every slicer on the page, even where I wanted the slicer's default - and also tested "nothing selected" scenarios.
  • Ran each query across a range of filter values, covering a range of partitions of the data: different years, different types of item.
  • Compared to 4 decimal places. More than that just made the tests brittle.
  • Got them green against the unchanged model, and committed them on their own.

These values are recorded, not verified. A recorded value means "unchanged", not "correct". But that's exactly what you need while the assistant is making changes, because any value that moves without being expected is a regression. Regression tests have other uses too. You can back out a change and show that the report is back where it started (I had to do exactly that for a whole feature, and I was very glad of them), and you'll notice when the upstream data changes underneath you.

When a change added a new option (say, a slicer that hadn't existed before), I added its default setting to every existing test. The existing suite then became the test for the default behaviour, and not a single expected value had to change.

2. A benchmark notebook

Tests tell you the numbers haven't changed, not that the report is still usable. So, second, I set up a benchmark and took a "before" run. For the benchmark, I:

  • Timed the queries that are actually in the report: the visuals' queries, with one or two representative measures for each kind of calculation. That way, you're measuring how the report performs, not how fast the tests run.
  • Labelled every run with the model state (e.g. "before the financial year change"). The results were append-only and committed alongside the model, so the history travels with it.
  • Repeated each query and compared medians, so I could judge any change against the noise between repeats.
  • Included some control queries that the change doesn't touch. If those slow down too, it's the environment (a busy day, a different network), not the change. Without them, it's easy to chase a slow-down that doesn't exist.
  • Always timed through the same client. The MCP server's execution metrics are brilliant for working out why something is slow in Desktop. But for before-and-after comparisons, the numbers can only be compared if they come through the same harness every time. Mine sent its queries through the same executeQueries endpoint as the tests.
  • Reported what didn't improve, as well as what did. The queries that didn't speed up show you where to look next.

A Jupyter notebook (in Python) worked well for this. It can render the history as charts (total time per run, time per query, and a faster/slower heatmap against the baseline), and the outputs can be committed for a reviewer to read.

Heatmap from the benchmark notebook showing typical seconds per query for each benchmarked run, with each value's ratio to the baseline. Most sales rank queries got between 2 and 16 times faster after the performance rewrite, while a few simpler order measures got slightly slower.

For more on measuring Power BI performance, Jess has written about performance optimisation tools for Power BI, and I covered some testing tips in Optimising DAX: Model Design Comparisons and Testing Tips.

We help teams build Power BI reports and semantic models that people trust, understand and actually use. Explore our Power BI consulting services.

3. Expected values derived outside the DAX change

Third, when you're making a change that should move numbers, work out what the new numbers should be without using the new DAX. (James makes the wider case for checking AI output against something independent in How to trust your AI-assisted data analysis.)

Expected values read from the changed model are circular: all they prove is that the model agrees with itself.

Find a way to make the unchanged model produce the new answer. Imagine a report that shows sales by financial year, where the financial year currently starts in July, and a change that moves the start to April. You don't need the new financial year column to know what "FY2025" should contain under the new rule: it's just 1 April 2024 to 31 March 2025. So ask the unchanged model for exactly those dates, and read the existing measures:

DEFINE
    -- Under the new rule, FY2025 runs from 1 April 2024 to 31 March 2025
    VAR NewFY2025 =
        DATESBETWEEN ( 'Date'[Date], DATE ( 2024, 4, 1 ), DATE ( 2025, 3, 31 ) )

EVALUATE
SUMMARIZECOLUMNS (
    'Region'[Region],
    NewFY2025,
    "Sales", [Total Sales],
    "Orders", [Order Count],
    "Average Order Value", [Average Order Value]
)

This works because those measures just aggregate over whatever dates are in the filter. They don't know anything about financial years, so giving them the new year's dates produces exactly what the changed model should show for FY2025. None of the new DAX is used.

Measures that do know about financial years, such as year-to-date or prior-year comparisons, need their own route. For year-to-date at the end of June 2024, for example, the expected value is just [Total Sales] over 1 April to 30 June 2024, which you can again ask the unchanged model for directly.

Your change will need its own version of this. A few rules apply whatever it is:

  • The rule is an input, values are derived. The new start month comes from the requirement. Only the numbers are worked out.
  • Check the boundaries. Off-by-one mistakes hide in the days either side of the cut-over (31 March and 1 April here), so make sure the tests include them.
  • Prove the method on today's rules first. Run the same derivation for the current behaviour and check it reproduces the current model exactly. Then break it on purpose, and check that it notices. Mine reproduced 27,233 values with 0 differences, and two deliberately broken copies were caught with 7 and 22 differences.
Microsoft Fabric Weekly is a summary of the week's top news to help you build on the Microsoft Fabric Platform.

I'd always try deriving in DAX, against the unchanged model, first. The engine has its quirks (there's an example in the aside below), and when the expected values come from the same engine, they share those quirks. Some changes can't be derived that way, though, such as a brand new calculation with nothing in the existing model to build on. For those, you can fall back to deriving the values outside Power BI, for example in Python from the raw data. That works, but DAX can catch you out there, with blanks versus zeros, or functions that don't quite follow the textbook formula. So proving the method matters even more.

E.g. Watch out for DAX functions that don't behave like the maths. Take the numbers 1 to 7, and ask for the 0.66666666 percentile:

EVALUATE
ROW (
    "PERCENTILEX.INC", FORMAT ( PERCENTILEX.INC ( GENERATESERIES ( 1, 7, 1 ), [Value], 0.66666666 ), "0.00000000" ),
    "Formula by hand", FORMAT ( 1 + 0.66666666 * 6, "0.00000000" )
)

DAX query view showing PERCENTILEX.INC returning 5.00000000, next to 4.99999996 from working out the same percentile by hand.

Work it out by hand (or in Python) and you get 1 + 0.66666666 × 6, which is 4.99999996. The engine returns exactly 5.

That tiny difference matters if the result is compared against a threshold, because 5 and 4.99999996 can land on different sides of it. I built tests from a document that had the hand-worked value in it, and it was only when they failed that I found the difference.

Finally... Make the change, and explain everything that moved!

Before the change goes in, there are two more things I'd do:

  • Write the new tests before the change, and let them fail. They describe the state you want. (If you've come across test-driven development, or TDD, this is the same idea: write the failing tests first, and the change makes them pass.) If the model still disagrees after the change, report it; don't pin the wrong value.
  • Keep the tests and the fixes separate. Partway through, I realised the assistant was reading the test files to check its fixes, and I told it to stop (and then removed the option!). If the fix is designed by looking at the expected values, you end up with a fix that fits the tests, rather than one that meets the requirement. Give it the requirement and let it verify with its own queries. Then run the tests yourself as the independent check.

With all of that in place, the loop itself is simple. Once the assistant's change is deployed, you run both test suites and the benchmark. Every value that moved should be one you expected, at the value you derived. Every failure is one of four things:

  • the environment: throttling (the executeQueries endpoint is rate limited, and on one early run, 13 of the 14 "failures" were just that), an expired sign-in, permissions;
  • a wrong derivation: fix it, and write down what tripped you up;
  • a model defect: report it, and leave the test failing;
  • the comparison itself: an integer against a decimal, or the wrong tolerance.

Also, write down when you deployed. Results taken hours apart can only be compared if you know nothing changed in between!

Once every moved value is explained, the change's results become the new baseline. The passing tests join the regression suite, and the "after" benchmark run becomes the "before" for the next change.

Worked example: speeding up a DAX visual from 13.7 s to 1.8 s

So, let's have a look at the loop in practice. The example I'll use is a performance change, rather than a change in what the model calculates. That makes step 3 simpler: a performance change shouldn't move any numbers, so the expected values are just the ones the model already produces, and the regression tests are the proof that nothing moved. The benchmark does the rest.

Here's the problem. The model itself was small (about 17 MB), but a typical table visual, with a handful of measures for each row, took 13.7 s. Almost all of that (97%) was in the single-threaded formula engine, so the problem was entirely the DAX. (If the formula engine and storage engine are new to you, I've written about them in my Optimising DAX series, starting with the formula engine and storage engine.)

Before anything reached the model, each rewrite was prototyped as a query-scoped measure and compared with the original using == (the last post has the workflow and the comparison query). Across 120 combinations of filters, there were zero differences.

The assistant also benchmarked each rewrite as it went, using the MCP server's execution metrics. Those were quick checks to pick the fastest version. For the real before-and-after comparison, I re-ran the full benchmark notebook after deploying, and compared every query against the baseline.

What fixed it

The three biggest fixes were:

  • Calculate expensive intermediate results once. The slowest measures had iterators nested inside iterators, and rebuilt the same intermediate results separately for every cell in the visual. The rewrite calculates them once, into a table variable, and works everything else out from that. (There's more on why this helps in my post on data materialisation.) One measure went from 1.26 s and 200 MB to 0.25 s and 10 MB.
  • Use variables, and skip work you don't need. Some measures calculated the same value several times over, and did expensive work even for rows that had no value to work with. Storing the value in a variable, and checking for a value before doing anything expensive, took them from about 5 s to 0.8 s.
  • Pass the column, not the measure, into percentile and median functions. 21 measures were passing a measure into PERCENTILEX.INC or MEDIANX when the value had already been calculated in a column added with ADDCOLUMNS, so every item's value was calculated twice.
Typical visual Before After
Duration 13.7 s 1.8 s
Peak memory 1.6 GB 330 MB
Storage engine queries 1,788 570

(That's one measurement of one visual, with a cleared cache, so treat it as indicative.) A refresh that had been running out of memory on a 25.5 GB capacity also ended up peaking at 4.3 GB.

Summary

Here's the loop in one place:

  1. Record regression tests from the unchanged model, with every filter set.
  2. Take a labelled benchmark run, with some control queries, through the same client every time.
  3. Derive the expected values (if any move!) without the new DAX: in DAX against the unchanged model where you can, and prove the method on today's rules either way.
  4. Write the new tests before the change, and explain every value that moved once it's deployed.

And a few things not to do:

  • Don't trust expected values read from the changed model: all they prove is that the model agrees with itself.
  • Don't let the assistant read the tests while it designs the fix.
  • Don't let it work out edge cases by hand when the engine can calculate them for real.

If you want to read more about the testing side, James's posts on testing Power BI reports with the ExecuteQueries REST API and testing Power BI reports using SpecFlow and .NET are a great place to start, and Barry's post on applying Behaviour Driven Development to data and analytics projects explains why Gherkin works so well for this kind of specification. If you're running Behave tests in VS Code, Liam's post on how to step into external code when debugging Python Behave tests is useful too. On the performance side, my Optimising DAX series covers how the engine works and what makes DAX fast or slow, with practical examples at the end.

We help teams build Power BI reports and semantic models that people trust, understand and actually use. Explore our Power BI consulting services.

FAQs

What should I do before letting an AI assistant change a Power BI model? Record regression tests for every measure from the unchanged model, using the queries your visuals send, and take a labelled benchmark run of the same queries. Then any change can be checked against both: nothing should move that you didn't intend, and nothing should get slower without you knowing.
How should I work out expected values for a Power BI model change? Derive them without using the new DAX. For example, for a change that moves the start of the financial year, ask the unchanged model for the new year's date range directly and read the existing measures. Check the method reproduces today's values exactly before you trust it, and write the new tests before the change is made.
How do I use AI to test a Power BI semantic model? Don't rely on the assistant's own checks. Record regression tests from the unchanged model as DAX queries that mirror each visual, and run them outside the assistant, for example with Behave and the Power BI executeQueries REST API. The assistant can help write the queries, but the tests should run independently of it, so anyone can re-run them.
How do I use AI to speed up slow DAX measures? Have the assistant prototype each rewrite as a query-scoped measure with DEFINE MEASURE, compare it with the original using == across every filter combination the report uses, and benchmark both. Only then write it into the model, and run the regression tests and a full benchmark. In the worked example, calculating intermediate results once, using variables, and passing columns rather than measures into PERCENTILEX.INC and MEDIANX took a visual from 13.7 s to 1.8 s.
Why does PERCENTILEX.INC return a different answer from the formula? The engine doesn't always return the exact answer the documented formula gives. For example, the 0.66666666 percentile of the numbers 1 to 7 works out as 4.99999996 by hand, but the engine returns exactly 5. If an edge case matters, run a query rather than working the answer out by hand or in Python.

Carmel Eve

Software Engineer II

Carmel Eve

Carmel is a software engineer and LinkedIn Learning instructor. She worked at endjin from 2016 to 2021, focused on delivering cloud-first solutions to a variety of problems. These included highly performant serverless architectures, web applications, reporting and insight pipelines, and data analytics engines. After a three-year career break spent travelling around the world, she rejoined endjin in 2024.

Carmel has written many blog posts covering a huge range of topics, including deconstructing Rx operators, agile estimation and planning and mental well-being and managing remote working.

Carmel has released two courses on LinkedIn Learning - one on the Az-204 exam (developing solutions for Microsoft Azure) and one on Azure Data Lake. She has also spoken at NDC, APISpecs, and SQLBits, covering a range of topics from reactive big-data processing to secure Azure architectures.

She is passionate about diversity and inclusivity in tech. She spent two years as a STEM ambassador in her local community and taking part in a local mentorship scheme. Through this work she hopes to be a part of positive change in the industry.

Carmel won "Apprentice Engineer of the Year" at the Computing Rising Star Awards 2019.