Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
JJ_masgio
Regular Visitor

Calculate expression by previous row

Hi!
Quite new to DAX.
This is my fact table:

Date (dd-mm-yyyy)Value
03-06-2023987
23-06-20231112
02-07-20231234
12-07-20231543

I'm interested in the delta Value across months... but the measure I'm interested in is sush as:
Delta[07] = 1543 - 1112
I mean is the difference between the MAX Value of month 07 and the MAX Value of month 06.
Of course I have a complete Calendar table.
Can you help me in filtering?

Thanks a lot!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @JJ_masgio ,

I created a sample pbix file(see the attachment), please check if that is what you want.

Measure = 
VAR _selyear =
    SELECTEDVALUE ( 'Calendar'[Date].[Year] )
VAR _selmonth =
    SELECTEDVALUE ( 'Calendar'[Date].[MonthNo] )
VAR _curmvalue =
    CALCULATE (
        MAX ( 'Table'[Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            YEAR ( 'Table'[Date] ) = _selyear
                && MONTH ( 'Table'[Date] ) = _selmonth
        )
    )
VAR _premdate =
    EOMONTH ( DATE ( _selyear, _selmonth, 1 ), -1 )
VAR _premvalue =
    CALCULATE (
        MAX ( 'Table'[Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            YEAR ( 'Table'[Date] ) = YEAR ( _premdate )
                && MONTH ( 'Table'[Date] ) = MONTH ( _premdate )
        )
    )
RETURN
    IF (
        ISBLANK ( _premvalue ) || ISBLANK ( _curmvalue ),
        BLANK (),
        _curmvalue - _premvalue
    )
Delta = 
IF (
    ISINSCOPE ( 'Calendar'[Date].[Year] ),
    [Measure],
    SUMX (
        GROUPBY ( 'Calendar', 'Calendar'[Date].[Year], 'Calendar'[Date].[Month] ),
        [Measure]
    )
)

vyiruanmsft_0-1693201957327.png

Best Regards

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @JJ_masgio ,

I created a sample pbix file(see the attachment), please check if that is what you want.

Measure = 
VAR _selyear =
    SELECTEDVALUE ( 'Calendar'[Date].[Year] )
VAR _selmonth =
    SELECTEDVALUE ( 'Calendar'[Date].[MonthNo] )
VAR _curmvalue =
    CALCULATE (
        MAX ( 'Table'[Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            YEAR ( 'Table'[Date] ) = _selyear
                && MONTH ( 'Table'[Date] ) = _selmonth
        )
    )
VAR _premdate =
    EOMONTH ( DATE ( _selyear, _selmonth, 1 ), -1 )
VAR _premvalue =
    CALCULATE (
        MAX ( 'Table'[Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            YEAR ( 'Table'[Date] ) = YEAR ( _premdate )
                && MONTH ( 'Table'[Date] ) = MONTH ( _premdate )
        )
    )
RETURN
    IF (
        ISBLANK ( _premvalue ) || ISBLANK ( _curmvalue ),
        BLANK (),
        _curmvalue - _premvalue
    )
Delta = 
IF (
    ISINSCOPE ( 'Calendar'[Date].[Year] ),
    [Measure],
    SUMX (
        GROUPBY ( 'Calendar', 'Calendar'[Date].[Year], 'Calendar'[Date].[Month] ),
        [Measure]
    )
)

vyiruanmsft_0-1693201957327.png

Best Regards

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.