Sub topic 13
Power Query and Source-Side Columns as Alternatives
This chapter assumes you know the difference between row context and filter context, and that a calculated column is evaluated at refresh time and stored in the model. It also assumes you can open Power Query Editor from Power BI Desktop and read basic M syntax. The chapter covers two other places where a column can be created, and when one of them is a better choice than a DAX calculated column.
Three places to build a column
A column that exists in your report can be created in three layers.
- The data source. A SQL view, a warehouse table, or a column in the source system. The logic runs before Power BI sees the data.
- Power Query. A custom column added as a transformation step in Power Query Editor, written in the M language. The logic runs during refresh, as the data is loaded into the model.
- The model. A DAX calculated column. The logic runs after the data has been loaded, using the model's tables and relationships.
All three produce a column that is stored in the model once the refresh finishes. At query time, a report cannot tell where the column came from. The differences are in where the logic runs, what it can see, how it is maintained, and how well it compresses.
What Power Query can do that DAX cannot
Power Query is built for shaping data. Several jobs are easier, or only possible, there.
- Cleaning text. Trimming, changing case, splitting by delimiter, and replacing values are single steps in Power Query.
- Changing data types before load. Converting text to dates or numbers early avoids type problems in the model.
- Reshaping. Unpivoting, merging queries, and grouping rows change the table's structure. A calculated column can only add a column to an existing table.
- Query folding. When the source supports it, Power Query translates steps into a single native query (for example SQL), so the source does the work and sends back only the result.
Here is a Power Query custom column that mirrors the Price Band column from earlier in this tutorial.
let
Source = Sql.Database("myserver", "SalesDb"),
Product = Source{[Schema = "dbo", Item = "Product"]}[Data],
AddedBand = Table.AddColumn(
Product,
"Price Band",
each if [List Price] >= 100 then "High" else "Standard",
type text
)
in
AddedBandIn M, each creates a function that is applied to every row, and [List Price] refers to the column in the current row. This is the same idea as row context in DAX, written differently.
The step above can fold. The engine can turn the if expression into a SQL CASE expression, so the database computes the band. You can check whether a step folds by right-clicking it in Applied Steps. If View Native Query is available, the step folds. If it is greyed out, the work happens in the Power Query engine on the machine doing the refresh.
What a calculated column can do that Power Query cannot
A DAX calculated column is still the right tool in some cases.
- It can use relationships.
RELATEDfollows a relationship that is already defined in the model. In Power Query you would need a merge, which is a join you design and maintain yourself. - It can use model-level logic. Columns that depend on other calculated columns, or on aggregations across the finished model, are easier to write in DAX.
- It works on data you cannot reshape. With a live connection or a composite model, you may not have access to the query layer for a table.
Compare the two ways of adding a category to the Sales table:
Product Category = RELATED(Product[Category])In Power Query you would merge Sales with Product on the product key and expand the Category column. That works, but it duplicates a text column on the largest table and adds a join to the refresh. The DAX version reads the value from a relationship that the model already has. For a dimension attribute like this, the usual advice is neither. Keep Category on the Product table and filter Sales through the relationship. You only need the column on Sales when something specific requires it.
Comparing the options
| Question | Source-side column | Power Query column | DAX calculated column |
|---|---|---|---|
| Where does the logic run? | In the source database | In the refresh pipeline, possibly folded to the source | In the model engine, after load |
| Can it use model relationships? | No | No, needs a merge | Yes, with `RELATED` |
| Can other tools reuse it? | Yes, any consumer of the source | No, only this dataset | No, only this dataset |
| Compression | Same as any loaded column | Same as any loaded column | Often worse, because the engine compresses it after other columns |
| Visible to the person maintaining it | Database team | Anyone who opens the query | Anyone who opens the model |
| Typical use | Shared business rules, heavy computation | Cleaning, typing, reshaping, simple row logic | Logic that needs relationships or model data |
On compression: columns that come from the source or from Power Query are loaded with the rest of the table, and the engine picks an encoding for them as part of that load. Calculated columns are computed after the load and are stored differently, so they can be less well compressed. The effect depends on the data, and it is worth measuring on your own model rather than assuming.
Where should the logic live?
A practical order of preference follows from how far upstream you can push the work.
- Source first, when the rule is shared. If several reports need the same definition of "active customer", put it in a view or warehouse table once. Every consumer gets the same answer and the logic has one owner.
- Power Query next, for dataset-specific shaping. Cleaning, typing, and simple row-level logic belong here. Prefer steps that fold.
- DAX calculated column last, when the model is required. Use it when you need
RELATED, or when the logic depends on something only the model knows. - Measure when the value must respond to filters. None of the three column options can do this. A column is fixed at refresh time. If the number must change when a user picks a slicer value, it is a measure.
Edge cases and common errors
- Folding breaks silently. Some steps, such as adding an index column or using certain M functions, stop query folding. Every step after the break runs locally. A column you add early, before the break, can fold. The same column added after it cannot. Place folding-friendly steps first.
- Refresh time moves, it does not disappear. Pushing a column into Power Query or the source does not make the computation free. It moves the cost to the refresh pipeline or the database. A heavy column on a large table can slow a refresh wherever you build it.
- Duplicated logic drifts. If the same rule exists in a SQL view and in a Power Query step, the two can disagree after someone edits one of them. Keep each rule in one place.
- Changing the source shape breaks the query. A Power Query step refers to columns by name. If the source renames a column, the refresh fails with an error that the column was not found. A DAX column that references the same model column breaks in a similar way, but at the model level.
- Ratios stay wrong in every layer. Moving
Margin % (column)to Power Query or the source does not fix the summing problem. A stored ratio is still one value per row. Build the ratio as a measure, as inDIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount])). - A column is not a substitute for a measure. The
Red Salespattern,CALCULATE([Total Sales], Product[Color] = "Red"), depends on filter context at query time. No upstream column can reproduce that behavior across all slicer selections.
Worked decision
Suppose a retail model needs three new fields.
- Customer full name, built from first name and last name. This is simple text logic with no model dependency, so create it in Power Query. If several reports need it, ask the database team to add it to the source view.
- Sales region on each sale, taken from the
Storetable. It depends on a relationship, so a DAX calculated column withRELATED(Store[Region])fits. Check first whether users can slice byStore[Region]directly, which would make the column unnecessary. - Profit margin. The value must be correct at any level of filtering. Create a measure.
Exercise
Your team has a Product table loaded from a SQL Server view. You need a text column Size Group that depends only on the Size column in the same row. Two other reports use the same view and need the same definition. Where should you create it, and why? Then name one reason you would choose a DAX calculated column instead.
Show answer
Create it in the SQL view. The logic uses only values in the same row and no model data, and because three reports need the same definition, putting it in the source gives them one shared definition with a single owner. Power Query would be the second choice if you cannot change the view, preferably with a step that folds. You would choose a DAX calculated column if the value needed data from a related table through RELATED, or if you cannot edit either the source or the query layer.
Exercise
A colleague moves Margin % (column) from a DAX calculated column into a Power Query custom column and says the problem with summed ratios is now fixed. Is that correct? What should they build instead?
Show answer
No. The column is still one ratio per row, so summing or averaging it still weights every row equally and gives the wrong overall margin. Moving it upstream changes where it is computed, not what it is. They should build a measure such as Margin % = DIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount])), which divides the totals in the current filter context.