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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
TheHans
Helper I
Helper I

Calculate a difference for values of max date with previous date

Hi,

I have a table with IDs, dates and values per ID-Date combination. I would like to calculate the difference of the values compared to the previous date for the same ID.

For example: ID 1 increased from 01.11.2021 to 02.11.2021 by 5. So the result is 5.
If an ID was not there on the previous date, the value would be null.
For example: ID 4 came in on 03.11.2021 with the value of 10. The diff prev. date is null since there is no previous date with this ID.

How do I calculate this new "Diff prev. date" column using DAX`?

Thanks in advance.

 

IDDateValueDiff prev. date
101.11.202110null
201.11.202110null
301.11.202110null
102.11.2021155
202.11.20218-2
302.11.2021100
103.11.2021150
203.11.2021113
303.11.2021100
403.11.202110null
1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@TheHans  you can use a measure like this

 

 

VAR _sum =
    SUM ( 'Table 1'[Value] )
VAR _id =
    MAX ( 'Table 1'[ID] )
VAR _date =
    MAX ( 'Table 1'[Date] )
VAR _immediatelyPrecedingSum =
    CALCULATE (
        SUM ( 'Table 1'[Value] ),
        FILTER (
            ALL ( 'Table 1' ),
            'Table 1'[Date]
                = CALCULATE (
                    MAX ( 'Table 1'[Date] ),
                    'Table 1'[Date] < _date,
                    ALLEXCEPT ( 'Table 1', 'Table 1'[ID] )
                )
                && 'Table 1'[ID] = _id
        )
    )
RETURN
    IF ( _immediatelyPrecedingSum = BLANK (), 0, _sum - _immediatelyPrecedingSum )

 

 

smpa01_0-1637935522738.png

 

If you need a calulated column

 

Column = 
VAR _sum =
    CALCULATE ( SUM ( tbl[Value] ) )
VAR _immediatelyPreceding =
    MAXX (
        FILTER (
            tbl,
            tbl[ID] = EARLIER ( tbl[ID] )
                && tbl[Date] < EARLIER ( tbl[Date] )
        ),
        tbl[Date]
    )
VAR _imediatelyPrecedingSum =
    SUMX (
        FILTER (
            tbl,
            tbl[ID] = EARLIER ( tbl[ID] )
                && tbl[Date] = _immediatelyPreceding
        ),
        tbl[Value]
    )
RETURN
    IF ( _imediatelyPrecedingSum = BLANK (), 0, _sum - _imediatelyPrecedingSum )

 

 

smpa01_1-1637936262566.png

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

4 REPLIES 4
CNENFRNL
Community Champion
Community Champion

 

Diff_CC = 
VAR __prev =
    MAXX(
        TOPN( 1, FILTER( Table1,
                Table1[ID] = EARLIER( Table1[ID] )
                    && Table1[Date] < EARLIER( Table1[Date] ) ),
            Table1[Date]
        ),
        Table1[Value]
    )
RETURN
    IF( NOT ISBLANK( __prev ), Table1[Value] - __prev )

 

Screenshot 2021-11-27 023624.png

 

Diff = 
VAR __prev =
    MAXX(
        TOPN(
            1,
            CALCULATETABLE(
                Table1,
                Table1[Date] < MAX( Table1[Date] ),
                ALLEXCEPT( Table1, Table1[ID] )
            ),
            Table1[Date]
        ),
        [Total]
    )
RETURN
    IF( NOT ISBLANK( __prev ), [Total] - __prev )

 

Screenshot 2021-11-27 023806.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Hi CNENFRNL,

 

thanks a lot. Appreciate your support.

H

smpa01
Super User
Super User

@TheHans  you can use a measure like this

 

 

VAR _sum =
    SUM ( 'Table 1'[Value] )
VAR _id =
    MAX ( 'Table 1'[ID] )
VAR _date =
    MAX ( 'Table 1'[Date] )
VAR _immediatelyPrecedingSum =
    CALCULATE (
        SUM ( 'Table 1'[Value] ),
        FILTER (
            ALL ( 'Table 1' ),
            'Table 1'[Date]
                = CALCULATE (
                    MAX ( 'Table 1'[Date] ),
                    'Table 1'[Date] < _date,
                    ALLEXCEPT ( 'Table 1', 'Table 1'[ID] )
                )
                && 'Table 1'[ID] = _id
        )
    )
RETURN
    IF ( _immediatelyPrecedingSum = BLANK (), 0, _sum - _immediatelyPrecedingSum )

 

 

smpa01_0-1637935522738.png

 

If you need a calulated column

 

Column = 
VAR _sum =
    CALCULATE ( SUM ( tbl[Value] ) )
VAR _immediatelyPreceding =
    MAXX (
        FILTER (
            tbl,
            tbl[ID] = EARLIER ( tbl[ID] )
                && tbl[Date] < EARLIER ( tbl[Date] )
        ),
        tbl[Date]
    )
VAR _imediatelyPrecedingSum =
    SUMX (
        FILTER (
            tbl,
            tbl[ID] = EARLIER ( tbl[ID] )
                && tbl[Date] = _immediatelyPreceding
        ),
        tbl[Value]
    )
RETURN
    IF ( _imediatelyPrecedingSum = BLANK (), 0, _sum - _imediatelyPrecedingSum )

 

 

smpa01_1-1637936262566.png

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi smpa01,

thanks for the solution. Especially for both ways, as measure and calculated column.

H

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.