Sub topic 3
Thinking in the visual's rows and columns
This chapter assumes you can build tables, matrices, and charts in Power BI Desktop. It also assumes you can write basic measures with DAX (Data Analysis Expressions) and know that a measure is calculated at query time while a column is stored per row. You need no prior knowledge of visual calculations beyond the overview chapters.
Why the layout matters
A measure runs inside the semantic model. Its result depends on filter context, which is the set of filters coming from slicers, row and column headers, and the report. A visual calculation works differently. It runs against the data the visual already holds, and that data is a grid.
Your formulas will make sense once you can picture that grid. Functions such as RUNNINGSUM, PREVIOUS, and COLLAPSE do not ask "which filters apply?" They ask "which cell is before this one, and in which direction?"
The visual matrix
Every visual that supports visual calculations has an underlying visual matrix. This is a grid of cells made from the fields you placed in the field wells. It has three parts:
- Rows come from the fields in the row-like wells. In a matrix visual that is the Rows well. In a column chart it is the field on the X-axis.
- Columns come from the fields that split the data sideways. In a matrix visual that is the Columns well. In a chart it is the Legend field.
- Cells hold the values: the measures and columns in the Values well, plus the results of your visual calculations.
Suppose you build a matrix with Year on rows, Sales Channel on columns, and [Sales Amount] as the value. The visual matrix looks like this:
| Year | Online | Retail | Total |
|---|---|---|---|
| 2023 | 100 | 300 | 400 |
| 2024 | 150 | 320 | 470 |
| 2025 | 210 | 310 | 520 |
| Total | 460 | 930 | 1390 |
A visual calculation adds one more value to each cell. If you add Running sum = RUNNINGSUM([Sales Amount]), you get a second value alongside [Sales Amount] in every cell, as if the grid had a new layer.
Referencing values by name
Inside a visual calculation you refer to a field by its name in square brackets, for example [Sales Amount]. The name points at that field's value in the current cell. It does not point at a table column in the model.
The matrix only holds what the visual holds. A field that is not in the visual is not available to your formula. If you need [Cost] and it is not in the wells, add it to the visual first. The hiding chapter covers how to keep such a field out of sight.
Margin = [Sales Amount] - [Total Cost]This works only if both [Sales Amount] and [Total Cost] are fields in the visual. The result for each cell uses the two values in that same cell, which is a plain cell-to-cell calculation.
Axes: the directions you can walk
Many visual calculation functions move across cells, so they need a direction. That direction is an axis. A visual has two:
- ROWS walks down the grid, from one row header to the next, within a column.
- COLUMNS walks across the grid, from one column header to the next, within a row.
You can also combine them. ROWS COLUMNS walks down each column first and then moves to the next column. COLUMNS ROWS walks across each row first and then moves to the next row.
Using the table above, here is what a running sum does on the Online column:
| Year | `[Sales Amount]` | `RUNNINGSUM([Sales Amount], ROWS)` |
|---|---|---|
| 2023 | 100 | 100 |
| 2024 | 150 | 250 |
| 2025 | 210 | 460 |
Walking along COLUMNS in the 2023 row instead gives 100 for Online, then 400 for Retail. Each value adds to the one before it in the new direction.
Running down = RUNNINGSUM([Sales Amount], ROWS)
Running across = RUNNINGSUM([Sales Amount], COLUMNS)You do not have to set the axis. When you leave it out, the function uses the visual's default axis, which is normally the rows. The axis chapter covers the parameter in detail. For now, remember that the same formula can give different answers if you move a field from Rows to Columns, because the grid's shape changed.
Hierarchies and levels
Fields in a well can form a hierarchy. For example, Year above Quarter on the rows. The visual matrix then has several levels of rows:
- The leaf rows, such as each quarter.
- The parent rows above them, such as each year, which hold subtotals.
- The grand total row at the top.
Each level is part of the grid, and your formula is evaluated for the cells on every level. This is why a result can appear on a subtotal row even though you never asked for one.
A function that walks an axis, such as PREVIOUS, compares cells at the same level. A year compares with a year, and a quarter with a quarter. Functions that cross levels are a separate family. COLLAPSE and COLLAPSEALL move up the hierarchy to a parent or the top. Their use for percent of parent and percent of grand total is covered in a later chapter.
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))Read this as a statement about the grid. For each cell, take its value, take the value one level up the rows, and divide.
What the grid does not contain
The visual matrix is smaller than the model. That has three practical consequences:
- Only visible granularity exists. If the visual shows Year, you cannot reach individual orders from a visual calculation.
- Filters outside the visual are already applied. Slicers, page filters, and report filters have shaped the grid before your formula runs. You cannot remove them from inside the calculation the way
ALLcan in a measure. - Adding or removing a field changes the answers. A running sum over Year gives different values once Quarter is added beneath it, because the rows are now different.
Because the formula depends on the grid, a visual calculation does not move with a field. If you copy a visual calculation to another visual with a different layout, it can silently compute something else, or fail if a referenced field is missing.
A worked reading exercise
Take this formula on the Online and Retail matrix above:
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])Read it as a walk through the grid:
- Start in a cell, such as Online for 2024, which holds 150.
PREVIOUSsteps one cell back along the default axis (rows) to Online for 2023, which holds 100.- The subtraction gives 50.
For the first row, 2023, there is no earlier row. PREVIOUS returns blank there, so the subtraction gives the cell's own value, 100. This is a typical edge effect. Notice that the output depends entirely on the position in the grid. No filter or date logic is involved.
Common mistakes
- Assuming the model is available. Writing
SUM(Sales[Amount])is not how visual calculations work. Use a field that is in the visual. - Changing the layout and not rechecking. Swapping rows and columns, or adding a field, changes what "previous" means.
- Ignoring the total rows. A running sum or an average also evaluates on subtotal and total cells. Check those cells before you trust the result.
- Confusing ROWS with the row-header text.
ROWSis a direction of travel, not a field name.
Exercise
A matrix has Year on rows and Sales Channel (Online, Retail) on columns. Online sales are 100, 150, and 210 for 2023, 2024, and 2025. What does RUNNINGSUM([Sales Amount], ROWS) show for Online in 2025? What would you expect if the same field were on the columns axis and you used the same default axis?
Show answer
It shows 460 (100 + 150 + 210), because the rows axis accumulates down the Online column. If the Year field moved to Columns, the same default (rows) axis would no longer walk across years, so the sum would accumulate over whichever field is now on the rows. The formula is unchanged, but the grid is different, so the values change.