Previous month’s sales with OFFSET

Fetch the value from the previous row of a visual with the OFFSET window function, ordered by a numeric YearMonth column.

You will learn to

  • Move to a neighboring row with OFFSET
  • Order months by a numeric column, not by their text
  • Explain how OFFSET differs from DATEADD

How it works

`OFFSET` is one of DAX’s **window functions**. Instead of thinking in dates, it thinks in **rows**: “take the row one step before this one, in this order”. That works for months, but equally for weeks, fiscal periods or any sorted list.

`OFFSET(-1, relation, ORDERBY(column))` has three parts:

- **-1** is how far to move: one row back (use 1 for the next row). - The **relation** is the list of rows to move along. `ALLSELECTED('Date'[Month Year], 'Date'[YearMonth])` is every month the visual can show, each with its numeric key. Including the visual’s own column lets OFFSET find the current row and lets its result replace the row’s filter. - `ORDERBY('Date'[YearMonth])` sets the order. Never order by the text `Month Year`: alphabetically, “Apr 2024” comes first.

OFFSET returns a row, so use it as a `CALCULATE` filter.

Practice it hands-on with a real data model and a live report that updates as you type.

Next Month Quantity = CALCULATE([Total Quantity], OFFSET(1, ALLSELECTED('Date'[Month Year], 'Date'[YearMonth]), ORDERBY('Date'[YearMonth])))