To calculate the difference between each row and the previous row within a data table in DAX.
Below is a sample DAX formula that calculates the difference for a column named Value
in a table named DataTable
.
RowDifference =
VAR CurrentValue = SUM(DataTable[Value])
VAR PreviousValue =
CALCULATE(
SUM(DataTable[Value]),
PREVIOUSROW(DataTable[Date]) // Assuming 'Date' is the index for row comparison
)
RETURN
CurrentValue - PreviousValue
Variables Definition:
CurrentValue
: This variable holds the sum of the current row's Value
. Adjust the aggregation method if necessary (e.g., using AVERAGE, MAX, or MIN based on your dataset).PreviousValue
: This uses the CALCULATE
function in conjunction with PREVIOUSROW
(assuming it's a column available for row order, such as a date or sequence number). It fetches the value from the previous row.Return Statement:
CurrentValue
and the PreviousValue
.Date | Value |
---|---|
2023-01-01 | 100 |
2023-01-02 | 150 |
2023-01-03 | 120 |
2023-01-04 | 130 |
NULL
.150 - 100 = 50
120 - 150 = -30
130 - 120 = 10
Date | Value | RowDifference |
---|---|---|
2023-01-01 | 100 | NULL |
2023-01-02 | 150 | 50 |
2023-01-03 | 120 | -30 |
2023-01-04 | 130 | 10 |
This DAX formula successfully computes the row-by-row difference. It is essential to ensure you have a unique ordering column (e.g., Date) to use with functions like PREVIOUSROW
. For further learning on DAX and data manipulation, consider exploring resources available through Enterprise DNA Platform.
This guide explains how to create a DAX formula for calculating the difference between each row and the previous row in a data table, using a column named Value
and ensuring proper ordering with a date index.