Sub topic 8
Choosing the direction with the axis parameter
This chapter assumes you can build a matrix in Power BI Desktop, write a basic visual calculation in the calculation editing experience, and understand rows and columns as the visual lays them out. Most visual calculation functions run along a line of cells. The axis parameter tells them which line.
What the axis controls
A visual calculation sees the visual as a grid. When a function such as RUNNINGSUM or PREVIOUS moves from cell to cell, the axis decides whether it walks down the rows or across the columns.
Take a matrix with Product Category on rows and Year on columns, showing [Sales Amount]. Without an axis, RUNNINGSUM adds values going down each column. That is usually not what you want here, because it adds up unrelated categories.
Axis values
| Axis value | Direction of travel |
|---|---|
| `ROWS` | Top to bottom, within each column. |
| `COLUMNS` | Left to right, within each row. |
| `ROWS COLUMNS` | Top to bottom, then continues in the next column to the right. |
| `COLUMNS ROWS` | Left to right, then continues in the next row down. |
When you omit the axis, the function uses the first axis of the visual, which is ROWS in most cases. Write the axis explicitly whenever the visual has both rows and columns. The calculation then keeps working if someone rearranges the fields.
Running across columns
This calculation accumulates sales across years for each category:
Cumulative by year = RUNNINGSUM([Sales Amount], COLUMNS)The 2024 cell for Bikes holds 2023 plus 2024 for Bikes. Each category is a separate line, so Bikes never mixes with Accessories.
The same idea works for comparisons. This calculation gives the change from the previous year:
Change vs previous year = [Sales Amount] - PREVIOUS([Sales Amount], 1, COLUMNS)The second argument is the number of steps back. The first year has no previous cell, so the result there is blank.
Walking the whole grid
The two-word values treat the grid as one long path. Compare the two ways of numbering the cells of a 3-row, 2-column matrix:
With ROWS COLUMNS, RUNNINGSUM carries the total from the bottom of one column into the top of the next. Use this when you want one continuous total across the whole matrix, such as sales by month where months are nested under years on the columns.
Continuous total = RUNNINGSUM([Sales Amount], ROWS COLUMNS)Common errors
- Results that restart or never accumulate. The function is moving along the wrong axis. Check whether your field is on rows or columns.
- Results that break after rearranging the visual. The calculation relies on the default axis. Set the axis explicitly.
- Unexpected carry-over between groups. Single-word axes stay within one row or column. The two-word values cross boundaries. If totals should not cross a boundary, use a single-word axis or add the reset parameter, which a later chapter covers.
Exercise
A matrix has Region on rows and Quarter on columns. You want each region's sales for a quarter minus the same region's previous quarter. Which expression do you write, and what does the first quarter show?
Show answer
[Sales Amount] - PREVIOUS([Sales Amount], 1, COLUMNS). Quarters run left to right within each region row, so COLUMNS is the correct axis. The first quarter has no previous cell, so PREVIOUS returns blank there.
Exercise
What is the difference between RUNNINGSUM([Sales Amount], ROWS) and RUNNINGSUM([Sales Amount], ROWS COLUMNS) in a matrix with several columns?
Show answer
With ROWS, the total restarts at the top of every column. With ROWS COLUMNS, it continues from the bottom of one column into the top of the next, so it produces one running total across the whole matrix.