How often do we revisit a Power BI model when everything is already working? Probably not very often. The numbers are correct, the reports run, and something more urgent always comes up. But over time, small implementation decisions can add up. A few repeated calculations, unnecessary dependencies, or workarounds that made sense at the time can make even a relatively simple model difficult to maintain.

That got me thinking: what actually makes a refactor successful? Is it fewer lines of DAX, a cleaner semantic model, or something we only discover when the next change request arrives? For this week's experiment, I'm revisiting a working retail model and challenging three implementation decisions to see what deserves changing, what should stay, and whether we're genuinely reducing complexity or just moving it elsewhere.

In this blog

  • Using AI to investigate a Power BI model before rewriting DAX
  • Deciding what belongs in SQL, what stays in DAX, and when to reuse logic
  • Why matching totals aren't enough to validate a refactor
  • Making future changes easier through ownership, dependencies, and testing
  • Which refactoring decisions are actually worth implementing?

The Setup: Everything Works, So What's the Problem?

Let's start with a fictional retail business selling electronics, furniture, and accessories. Our Power BI model has four tables: Sales, Products, Customers, and Date. It supports revenue reporting, product performance, and comparisons across different reporting periods.

Nothing is broken. The numbers reconcile, and the reports work as expected. But here's how some of the calculations are currently implemented:

Area Current implementation
Net Sales A SUMX measure calculates revenue after discounts and returns
Time intelligence Separate DAX measures handle MTD, YTD, rolling 12 months, and previous-year comparisons
Measure reuse Similar time-intelligence calculations are repeated across Sales, Quantity, and Orders

Now imagine the business is expanding its reporting. Another analytical product needs the same Net Revenue definition, a new dashboard requires additional time comparisons, and the team needs to maintain an increasing number of measures.

The existing approach still works, but three questions start to emerge:

Three questions to investigate

01

Should Net Revenue remain a calculation inside this semantic model?

02

Which calculations genuinely need DAX's flexibility?

03

Are separate measures still the easiest way to maintain repeated reporting logic?

We don't have a performance problem or incorrect numbers to fix. We have implementation decisions worth questioning before the model becomes harder to maintain. But how do we know which ones actually need changing?

Before Refactoring, Let's Investigate

The usual approach is familiar: copy a few DAX measures into AI and ask it to optimise them. We might get a shorter expression, but we've already assumed that rewriting DAX is the right solution.

A different starting point
The assumption

We've already assumed that rewriting DAX is the right solution.

The question

What if we gave AI the model context first and asked it to investigate before recommending changes?

We don't need to inspect the entire model. Let's focus on Net Sales, the existing time-intelligence calculations, and their dependencies.

First, collect the model context in Power BI:

01

Get the definitions. Open Power BI Desktop's TMDL View and drag the relevant tables into the editor to inspect their measures, calculated columns, and metadata. With a PBIP project saved in TMDL format, we can also inspect the files under definition/tables.

02

Check the relationships. Open Model View and examine how Sales, Products, Customers, and Date are connected. Note the active relationships, cardinality, and filter directions.

03

Trace dependencies. In DAX Query View, run:

DAX QUERY Model dependencies

EVALUATE
INFO.CALCDEPENDENCY()

This identifies dependencies within the semantic model, although it won't reveal every duplicated business rule or external consumer.

Next, give AI access to that context. There are two practical ways to start:

Option A

Without MCP

Create a small investigation package containing the relevant TMDL definitions, relationship details, and dependency query results.

Attach the files or paste their contents into an AI conversation. The important difference is that we're supplying the definitions and dependencies together, rather than isolated DAX expressions.

Option B

With a model-aware assistant

For a more integrated approach, we can use Microsoft's Power BI Modeling MCP Server with VS Code and GitHub Copilot.

  1. Install the Power BI Modeling MCP extension in VS Code and ensure GitHub Copilot Chat is available.
  2. Open the retail model in Power BI Desktop.
  3. In VS Code, open Copilot Chat in Agent mode and connect to the open Power BI Desktop model using the MCP tools.
  4. Verify the connection by asking the assistant to list the model's tables and measures.

Once connected, the assistant can inspect model definitions, relationships, and dependencies through the available tools, rather than relying on manually copied snippets. For this investigation, keep the interaction read-only.

Now we can investigate before rewriting. I'd start with this request:

✦ AI investigation prompt

Read-only

Inspect Net Sales and the time-intelligence measures. Identify repeated calculations, existing reuse mechanisms, and dependencies. Show the supporting definitions and distinguish what the model proves from what requires business or upstream data context. Don't modify anything.

Model context required

For our retail model, this gives us three areas to examine: whether Net Revenue should become a shared definition, which time-intelligence calculations need to remain dynamic, and whether repeated measures justify another reuse mechanism.

We've changed the starting point from β€œCan AI write better DAX?” to β€œDo we need to change this DAX at all?”

Three Refactors, Three Different Decisions

We've inspected the model and identified three areas worth questioning. But reducing DAX isn't the objective. We need to decide which calculations belong in the data platform, which depend on the semantic model, and where reusability actually helps.

A REFACTOR TEST 01

When Should a Calculation Move Upstream?

Let's start with Net Sales. Our retail model calculates revenue after discounts and returns:

DAX Net Sales Β· Current implementation

Net Sales =
SUMX(
    Sales,
    Sales[Quantity] * Sales[UnitPrice]
        - Sales[DiscountAmount]
        - Sales[ReturnAmount]
)

This works. So why move it? Imagine another semantic model is being developed for financial reporting, while a separate analytical application also needs Net Revenue. If each consumer implements the calculation independently, a change to how discounts or returns are treated could produce inconsistent results.

The problem isn't the DAX expression. It's that the business definition is being maintained in multiple places.

If Net Revenue is an agreed, reusable business definition, I'd consider maintaining it in the shared data product:

After β€” Prepare the transaction-level value in SQL

SQL Transaction-level Net Revenue

SELECT
    TransactionID,
    ProductKey,
    CustomerKey,
    SalesDate,
    Quantity * UnitPrice
        - DiscountAmount
        - ReturnAmount AS NetRevenue
FROM sales_transactions;

The semantic model now needs only:

DAX Net Sales Β· Simplified measure

Net Sales =
SUM(Sales[NetRevenue])

But I'd check two things before approving this change: whether other consumers genuinely need the same definition, and whether the upstream data product has clear ownership and change controls.

If neither is true, moving the calculation into SQL may simply create another dependency.

TEST 01 Β· MY DECISION

Move Net Revenue upstream when it is a shared business definition with a clear owner and multiple consumers.

Otherwise, the existing DAX may be perfectly reasonable.

B REFACTOR TEST 02

What Actually Deserves to Stay in DAX?

Now consider calculations whose results depend on what users are analysing. Our Net Revenue definition remains unchanged, but the reporting requirements vary depending on the selected period, filters, and comparison mode.

Requirement Why DAX makes sense
MTD, YTD, rolling 12 months Responds to reporting periods and date context
Previous-year or previous-period comparisons Depends on comparison rules
Revenue contribution % Depends on which filters define the total
Dynamic rankings Changes with product, region, or customer selections
Selected KPI or comparison mode Responds to report interactions

For example, our rolling 12-month calculation can reuse the existing Net Sales measure:

DAX Sales Rolling 12M

Sales Rolling 12M =
VAR LastDate = MAX('Date'[Date])
RETURN
    CALCULATE(
        [Net Sales],
        DATESINPERIOD(
            'Date'[Date],
            LastDate,
            -12,
            MONTH
        )
    )

This becomes more interesting when a report needs to support incomplete months, fiscal calendars, or multiple comparison modes.

We could precalculate specific periods upstream, but that would introduce assumptions about how users should analyse the data. Every new comparison requirement might then need another upstream change.

TEST 02 Β· MY DECISION

Keep time intelligence and other context-dependent analytics in DAX.

Standardise the underlying business definitions, but don't sacrifice reporting flexibility just to reduce DAX.

C REFACTOR TEST 03

Do We Really Need Another Measure?

Even when DAX is the right place for a calculation, we still need to decide how to maintain it. Our retail model has three base measures: Net Sales, Quantity, and Orders. Each needs MTD, YTD, and rolling 12-month calculations.

That's nine explicit measures. Add previous-year comparisons, and the number could double. We have three options:

THREE WAYS TO MANAGE REUSE

β–€ Explicit measures

A small number of calculations with distinct requirements

↻ Calculation groups

The same time transformation applies consistently across several measures

{ } DAX UDFs

Repeated logic benefits from a reusable function with parameters

For example, a calculation group can apply the same YTD transformation to multiple selected measures:

DAX Calculation Group Β· YTD

CALCULATE(
    SELECTEDMEASURE(),
    DATESYTD('Date'[Date])
)

That gives us one shared YTD definition instead of maintaining separate implementations for Net Sales, Quantity, and Orders. But I'd be cautious about introducing it just to eliminate nine measures.

The real question is whether the team repeatedly changes these definitions. If the calculations are stable and easy to understand, explicit measures may require less maintenance than a new abstraction.

I explored the maintenance differences between calculation groups and DAX UDFs in an earlier Analyst in Action experiment. Here, the decision is less about which feature is more capable and more about whether reuse solves an actual maintenance problem.

TEST 03 Β· MY DECISION

Keep explicit measures until repetition creates a meaningful maintenance cost.

Introduce calculation groups or UDFs when there's a clear, recurring pattern worth centralising.

What Have We Actually Changed?

Test Decision What we're trying to improve
A Move a shared revenue definition upstream, conditionally Consistency across consumers
B Retain context-dependent calculations in DAX Reporting flexibility
C Introduce reuse only where justified Maintenance effort

Notice that we haven't necessarily rewritten everything. One decision moves logic, another deliberately retains it, and the third depends on whether the additional abstraction is worthwhile. But architectural reasoning alone doesn't prove that we've improved the model.

What happens when we validate the original and revised implementations against the same reporting requirements?

The Numbers Match. Are We Done?

Let's say our original DAX and revised SQL calculation both return €10,000 in Net Sales. Does that mean the refactor passed? Not necessarily.

Consider two transactions. Both have a quantity of one, a unit price of €100, and no returns. But one transaction has a missing discount value.

TRANSACTION-LEVEL COMPARISON
Transaction Discount Original DAX SQL without NULL handling
A €10 €90 €90
B Missing €100 NULL
Total €190 €90

In DAX, a blank numeric value is treated as zero in this subtraction. In SQL, subtracting NULL produces NULL, and SUM ignores that result.

The business calculation hasn't changed, but the two implementations no longer behave the same way.

We can address this in SQL using COALESCE(DiscountAmount, 0) and applying the same treatment to other nullable amounts where the business rules require it.

But correcting the expression isn't enough. We still need to verify the results. In DAX Query View, we can compare the original and revised measures by reporting month:

DAX QUERY Net Sales Β· Validation

EVALUATE
SUMMARIZECOLUMNS(
    'Date'[YearMonth],
    "Original", [Net Sales Original],
    "Revised", [Net Sales],
    "Difference", [Net Sales Original] - [Net Sales]
)

I'd also compare results by product group and test transactions with missing values, discounts, and returns. These checks help catch differences that a grand total might hide.

For Test C, the equivalent risk is filter context. A reusable YTD calculation might match an explicit measure for Net Sales but behave differently for percentages or other non-additive KPIs.

VALIDATION
WHAT ACTUALLY COUNTS

The principle is simple: a refactor must preserve the intended calculation behavior, not just produce a familiar-looking total.

But even when the numbers reconcile, we still haven't answered the maintenance question. What happens when the business asks us to change the logic again?

The Change Request: Did We Actually Make Maintenance Easier?

A refactor often feels like an improvement when you're the one who built it. You know where the logic lives and why certain decisions were made. But what happens when another developer inherits the solution six months later?

Let's introduce two new requirements: a promotional discount must now be excluded from Net Revenue, and YTD reporting must follow a fiscal year instead of the calendar year. Here's what I'd want in place before the next developer starts changing anything:

MAINTENANCE READINESS 4 AREAS
01 Discoverability

Document shared calculations, their location, and ownership in a searchable data product or semantic model catalogue

02 Change impact

Identify affected measures, reports, and downstream consumers before editing

03 Safe changes

Keep baseline validation queries and expected results alongside the implementation

04 Team accessibility

Use consistent naming and explain non-obvious business rules and design decisions

For the revenue change, the developer should be able to locate the shared SQL definition, identify its consumers, and confirm whether the promotional discount exclusion applies to all of them. If it doesn't, changing the shared definition would be the wrong approach.

For fiscal YTD, they should be able to locate the existing time-intelligence implementation, update the relevant calculation, and validate the result against the fiscal calendar. A shared calculation item may reduce repeated edits, but only if the same fiscal rule applies across those KPIs.

THE REAL MAINTENANCE BENEFIT

That's where the real maintenance benefit becomes visible: not simply having fewer definitions, but knowing exactly what to change, what not to change, and how to verify the result.

We haven't measured developer effort in this fictional exercise. But these are the conditions I'd want to establish before claiming that the refactor made maintenance easier. So, looking back at our three decisions, which changes would I actually approve?

The Verdict: What Actually Became Simpler?

If I were reviewing this model for release, I wouldn't approve three refactors. I'd approve only the changes that solve a problem we've actually established.

REFACTOR RELEASE REVIEW 3 DECISIONS

Decision My verdict What I'd do
Move Net Revenue upstream ◐ Conditional approval Move it only after confirming shared ownership, consistent business rules, and downstream consumers. Otherwise, retain the DAX measure.
Move time intelligence out of DAX Γ— Reject Keep context-dependent calculations in the semantic model and reuse the existing Net Sales measure.
Replace explicit measures with reusable logic β—· Not yet Keep the nine measures until repeated changes or growing complexity justify a calculation group or UDF.

There's a distinction here that I think gets overlooked: identifying an opportunity to refactor isn't the same as establishing a reason to do it.

For Net Revenue, the missing evidence is whether the definition genuinely belongs to a shared data product. For time intelligence, we've established why DAX remains useful. And for the nine explicit measures, we haven't demonstrated a maintenance problem significant enough to justify another abstraction.

That means our most immediate improvements may be less exciting than a rewrite: preserve the existing calculations, make their dependencies visible, establish ownership, and retain validation checks for future changes.

THE FINAL THOUGHT

I'd rather release a model with nine understandable measures and reliable tests than a more sophisticated implementation whose maintenance benefits we've only assumed


Closing Thoughts

One thing I've become increasingly sceptical about is refactoring a Power BI model simply because we can implement something more elegantly.

I've worked with models where a small change meant tracing measures, relationships, and dependencies before touching a single line of DAX. In those situations, the problem was rarely that the original developer hadn't used enough advanced features. It was that too many decisions had accumulated without anyone questioning whether they still made sense.

That's why I'd rather spend time removing unnecessary logic than introducing another layer of reuse. And I wouldn't replace ten understandable measures with a clever abstraction unless there was a recurring maintenance problem worth solving.

For me, the strongest refactor isn't the one that demonstrates what Power BI can do. It's the one that removes something the model never needed in the first place.