Sub topic 8
Choosing the direction with the axis parameter
Many visual calculation functions take an optional axis parameter. It tells the function which direction to walk through the visual's data. This chapter explains what the parameter accepts and how the choice changes results. It also covers the mistakes that come from leaving it at the default.
This chapter assumes you can create a visual calculation in the editing experience. It also assumes you know the visual's data is laid out as a grid of rows and columns. If either is unfamiliar, review the earlier chapters on creating visual calculations and on the visual matrix.
What the axis parameter controls
A visual calculation does not read the data model. It reads the visual's own grid. Functions such as RUNNINGSUM, PREVIOUS, or NEXT need to know which way to move through that grid. The axis parameter provides that direction.
The parameter is optional. If you leave it out, the function uses the visual's first axis, which is ROWS in most visuals. Writing it explicitly makes the intent visible to the next person who edits the calculation.
The functions covered so far accept it in these positions:
| Function | Signature (optional parts in brackets) |
|---|---|
| `RUNNINGSUM` | `RUNNINGSUM(column [, axis] [, blanks] [, reset])` |
| `MOVINGAVERAGE` | `MOVINGAVERAGE(column, windowSize [, includeCurrent] [, axis] [, blanks] [, reset])` |
| `PREVIOUS` | `PREVIOUS(column [, steps] [, axis] [, reset])` |
| `NEXT` | `NEXT(column [, steps] [, axis] [, reset])` |
| `FIRST` | `FIRST(column [, axis] [, reset])` |
| `LAST` | `LAST(column [, axis] [, reset])` |
To pass axis without the optional arguments before it, leave the earlier positions empty with commas. For example, PREVIOUS([Sales Amount], , COLUMNS) skips steps. Check the signature in the editor's IntelliSense, because the exact positions matter.
The four axis values
The axis parameter accepts four values.
| Value | Direction |
|---|---|
| `ROWS` | Down the rows, from top to bottom, separately within each column |
| `COLUMNS` | Across the columns, from left to right, separately within each row |
| `ROWS COLUMNS` | Down the first column, then continue at the top of the next column |
| `COLUMNS ROWS` | Across the first row, then continue at the left of the next row |
The first two values stay inside one line of the grid. The last two treat the whole grid as one long sequence and differ only in the order they read it.
A worked example
Use a matrix with Year on the rows and Category on the columns, showing Sales Amount:
| Year | Bikes | Helmets |
|---|---|---|
| 2023 | 100 | 20 |
| 2024 | 120 | 30 |
| 2025 | 150 | 25 |
Now add four visual calculations that differ only in the axis.
Running ROWS = RUNNINGSUM([Sales Amount], ROWS)Running COLUMNS = RUNNINGSUM([Sales Amount], COLUMNS)Running ROWS COLUMNS = RUNNINGSUM([Sales Amount], ROWS COLUMNS)Running COLUMNS ROWS = RUNNINGSUM([Sales Amount], COLUMNS ROWS)The results are shown below for each of the four calculations, one table per calculation.
ROWS accumulates down each category. Each category starts over at its own top row.
| Year | Bikes | Helmets |
|---|---|---|
| 2023 | 100 | 20 |
| 2024 | 220 | 50 |
| 2025 | 370 | 75 |
COLUMNS accumulates across each year. Each year starts over at its leftmost cell.
| Year | Bikes | Helmets |
|---|---|---|
| 2023 | 100 | 120 |
| 2024 | 120 | 150 |
| 2025 | 150 | 175 |
ROWS COLUMNS finishes the Bikes column, then carries the total (370) into the top of Helmets.
| Year | Bikes | Helmets |
|---|---|---|
| 2023 | 100 | 390 |
| 2024 | 220 | 420 |
| 2025 | 370 | 445 |
COLUMNS ROWS finishes the 2023 row, then carries the total (120) into the start of 2024.
| Year | Bikes | Helmets |
|---|---|---|
| 2023 | 100 | 120 |
| 2024 | 240 | 270 |
| 2025 | 420 | 445 |
Both combined directions end at 445, the sum of all six cells. They reach it by different routes, so the intermediate cells differ.
Which value to choose
Pick the axis by asking what the sequence means in your report.
- Use
ROWSwhen time or another ordered field runs down the rows and each column is an independent series. This is the common case for a running sum per category. - Use
COLUMNSwhen the ordered field runs across the columns, such as months in a matrix with months as column headers. - Use
ROWS COLUMNSorCOLUMNS ROWSonly when the whole grid is one continuous sequence. This is rare. A typical case is a matrix with years on rows and quarters on columns, where you want one running total that crosses from one year's last quarter into the next year's first quarter.
If your values are independent series, a combined axis is almost always wrong. It adds the end of one series to the start of the next, and the numbers look plausible but mean nothing.
Relative functions follow the same rule
The axis works the same way in PREVIOUS and NEXT. In the example matrix, this calculation compares each year with the year above it, within each category.
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount], 1, ROWS)Switching the last argument to COLUMNS would instead subtract the cell to the left. For 2023, Bikes has no cell to its left, so PREVIOUS returns blank there. Because the subtraction with a blank treats it as zero, the first column simply shows its own value, which is a common surprise.
The same applies at the other end of the sequence. NEXT returns blank for the last cell, and FIRST and LAST always return a cell inside the walked line.
Order comes from the visual
The axis gives the direction, but the order of the cells comes from how the visual is sorted. If you sort the matrix by Sales Amount descending, "previous" means the row above in that sorted order, not the earlier year. A running sum over a visual sorted by value accumulates in that order.
For time-based calculations, sort the ordered field by its natural order, such as date or month number, before building the calculation. If a month field sorts alphabetically, April comes first and your running total is wrong.
Common errors
- Using the default and getting the wrong direction. If you add a field to the columns well later, the visual's layout changes. A calculation that depended on the default may now walk a different line. State the axis explicitly.
- Choosing a combined axis for independent series. Totals bleed from one column into the next.
- Referring to an axis the visual lacks. A table visual or a chart with no legend has no meaningful column dimension. A
COLUMNSwalk there has nothing to travel across, so the result will not be what you expect. Check the visual's fields before choosing the direction. - Sorting by value. The running order changes with the sort, so a running sum may look scrambled.
Exercise
Exercise
A matrix has Month on the rows (sorted January to December) and Region on the columns (North, South). You want a running total of Sales Amount that restarts at the top of each region. Which axis value do you use, and what would ROWS COLUMNS do instead?
Show answer
Use RUNNINGSUM([Sales Amount], ROWS). It walks down the months within each region column. ROWS COLUMNS would finish the North column and carry its December total into the January cell of South, so South's running total would start from North's annual total instead of from zero.
Exercise
In the 2023 to 2025 example, what does PREVIOUS([Sales Amount], 1, COLUMNS) return for the Helmets column in 2024, and what does it return for the Bikes column?
Show answer
For Helmets in 2024 it returns 120, the Bikes value in the same row (the cell to its left). For Bikes it returns blank, because no cell lies to its left.