Stop 03 · SQL for Power BITopics · 9/9

SQL vs Power Query vs DAX: Where Logic Belongs

Part of the Power BI Roadmap

In short

The same calculation can often be written in SQL, in Power Query or in DAX. Putting it in the right layer is one of the clearest signs of an experienced Power BI developer, and a favourite interview question.

A common rule is to do heavy shaping as far upstream as possible: joins and filters in SQL, cleaning in Power Query, and only dynamic calculations that respond to filters in DAX measures.

The jaadu carries a sack of wheat from a small field to a stone mill and then rolls rotis on a kitchen board, all along one winding path.

Think of it like this

Farm, mill and kitchen

Wheat is harvested and sorted at the farm (SQL in the database), ground into atta at the mill (Power Query), and only in the kitchen is it rolled into rotis to order, for however many guests arrive (DAX measures). You would never grind wheat at the dinner table.

Watch it happen

Where each piece of logic lives

SQL
Power Query
Model
DAX measures

1/4SQL: join Orders to Customers and drop test orders. The database is best at heavy joins and filters.

The big ideas

What you need to know

Push logic upstream

A popular guideline: do transformations as far upstream as possible and as far downstream as necessary.

SQL for heavy lifting

Joins, filters and big aggregations run fastest inside the database.

Power Query for shaping

Cleaning files, unpivoting and combining sources that a database does not hold.

DAX for dynamic maths

Anything that depends on the filters a user picks, like ratios, running totals and time comparisons.

See it in code

Walk through the code

dax
1-- This MUST be DAX: it changes with every slicer and filter.2Sales % of All Cities =3DIVIDE (4    [Total Sales],5    CALCULATE ( [Total Sales], ALL ( Customers[City] ) )6)7 8-- This should NOT be DAX: a fixed per-row label.9-- Do it in SQL or Power Query instead:10-- CASE WHEN Amount >= 5000 THEN 'Large' ELSE 'Small' END

1/2The numerator respects every filter. The denominator removes only the City filter with ALL.

A ratio that depends on the current filters belongs in DAX. A fixed row label belongs further upstream.

Save yourself the pain

Common mistakes

Oops

Writing calculated columns in DAX for every cleaning job, like trimming text or splitting names.

Do this instead

Do row-level cleaning in Power Query or SQL. It compresses better and keeps the model lean.

Oops

Pre-calculating percentages in SQL and then summing them in visuals.

Do this instead

Store the raw amounts and calculate ratios in DAX measures, so they stay correct at every level of a visual.

Your mission

Sort the logic

~20 min

Quick check

Where should a measure showing each city's share of total sales, reacting to a Year slicer, be calculated?

Go deeper

  1. 1Take one report requirement and decide which layer each step belongs in.
  2. 2Move a calculated column that could be done in Power Query and compare model size.
  3. 3Write a one-paragraph rule of thumb you can explain in interviews.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge