daxpowerbi-desktopcalculated-columns

Using OFFSET to get the value from a previous row so that I can calculate the difference


I'm creating a report that will show that change in a particular column between one row and the one previous.

When I use the following DAX formula, nothing is shown in the column

PreviousWeightedRevenue = 
    CALCULATE(
        SUM('table'[WeightedRevenue]),
        OFFSET(-1, ORDERBY('table'[EndOfSnapshotWeek], ASC), PARTITIONBY('table'[UniqueKey])
    )
)

UniqueKey is what identifies a set of rows that are linked.

EndOfSnapshotWeek - Each row represents a weekly snapshot not daily

Each UniqueKey can have one or more EndOfSnapshotWeek

The result of the Calculated Column - PreviousWeightedRevenue is nothing.

I will then extend the DAX to calculate the difference between PreviousWeightedRevenue and WeightedRevenue but I need to get this bit working first

The datatype of the EndOfSnapshotWeek column is date.

Here's some sample data

CREATE TABLE SnapshotData (
    EndOfSnapshotWeek DATE,
    UniqueKey NVARCHAR(255),
    WeightedRevenue DECIMAL(10, 2)
);

INSERT INTO SnapshotData (EndOfSnapshotWeek, UniqueKey, WeightedRevenue)
VALUES 
    ('2024-12-08', '//MN7uW5Z7sCvyvLHKXNow==', 3757.66),
    ('2024-12-15', '//MN7uW5Z7sCvyvLHKXNow==', 3015.66),
    ('2024-11-17', '//pMJnFmt78KfQPi0B9oLA==', 7800),
    ('2024-11-24', '//pMJnFmt78KfQPi0B9oLA==', 7120),
    ('2024-12-01', '//pMJnFmt78KfQPi0B9oLA==', 650),
    ('2024-12-08', '//pMJnFmt78KfQPi0B9oLA==', 6500),
    ('2024-12-15', '//pMJnFmt78KfQPi0B9oLA==', 6900),
    ('2024-11-17', '//PsALLVxKitgrfvHWbnDA==', 12540),
    ('2024-11-24', '//PsALLVxKitgrfvHWbnDA==', 11540),
    ('2024-12-01', '//PsALLVxKitgrfvHWbnDA==', 19540),
    ('2024-12-08', '//PsALLVxKitgrfvHWbnDA==', 13340),
    ('2024-12-15', '//PsALLVxKitgrfvHWbnDA==', 17740);

Expected Output

enter image description here


Solution

  • you can try this

    MEASURE =
    VAR _date =
        MAXX (
            FILTER (
                ALL ( 'Table' ),
                'Table'[uniquekey] = MAX ( 'Table'[uniquekey] )
                    && 'Table'[EndOfSnapshotWeek] < MAX ( 'Table'[EndOfSnapshotWeek] )
            ),
            'Table'[EndOfSnapshotWeek]
        )
    RETURN
        SUMX (
            FILTER (
                ALL ( 'Table' ),
                'Table'[uniquekey] = MAX ( 'Table'[uniquekey] )
                    && 'Table'[EndOfSnapshotWeek] = _date
            ),
            'Table'[WeightedRevenue]
        )
    

    enter image description here


    if you want to create a column ,you can try this

    Column=
    MAXX (
        FILTER (
            'Table',
            'Table'[UniqueKey] = EARLIER ( 'Table'[UniqueKey] )
                && 'Table'[EndOfSnapshotWeek]
                    = EARLIER ( 'Table'[EndOfSnapshotWeek] ) - 7
        ),
        'Table'[WeightedRevenue]
    )
    

    enter image description here