In this Pragmatic Works training video, Mitchell Pearson explores how to replicate lag and lead functionality in DAX using the OFFSET function. Drawing from his SQL background, Mitchell demonstrates how these window functions help analysts compare prior or next rows of data, making day-over-day or period-over-period analysis easier and more efficient.
Mitchell shares that window functions such as lag and lead were among his favorites while working in SQL Server. These functions allow developers to reference values from previous or subsequent rows within a dataset. This is especially useful in scenarios like stock analysis, where gaps (weekends, holidays) can complicate row-to-row comparisons.
The demonstration uses a table of historical stock data for Microsoft and Vanguard Total Index Fund. Each row includes daily open, close, high, and low prices. The goal: compare a day’s closing price with the previous day’s close to analyze trends.
OFFSET in DAX to move backward or forward across rows.Mitchell demonstrates how to build a calculated column using OFFSET to return the previous closing price.
-1 moves back one row (lag). A positive number moves forward (lead).Date, Stock, and Close Price in the virtual table.The OFFSET function essentially returns a table expression. Mitchell shows how to retrieve the desired value (the prior day’s closing price):
MINX or MAXX to return the single column value, since only one row is retrieved after partitioning and ordering.Once the calculation is applied, users can confirm that the “Previous Close” column correctly aligns each day’s record with its prior trading day close. For example, June 3rd’s record accurately reflects June 2nd’s close.
Mitchell notes that this result can also be achieved without window functions in DAX by:
However, OFFSET provides a cleaner and more intuitive solution for those familiar with SQL-style window functions.
OFFSET function in DAX enables lag and lead operations similar to SQL Server.Don't forget to check out the Pragmatic Works' on-demand learning platform for more insightful content and training sessions on DAX and other Microsoft applications. Be sure to subscribe to the Pragmatic Works YouTube channel to stay up-to-date on the latest tips and tricks.