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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Unknown
Frequent Visitor

Difference between two rows

Hello,

I have a table that looks like this (Please note that my date is in the format DD/MM/YYYY):

Unknown_1-1654592686240.png


I want to calculate the difference between two following dates where condition=1.
I managed to create the column in orange with the following DAX code:
datediff = IF( Table[condition]=1, Table[date] - CALCULATE( SUM(Table[date]), FILTER( Table[index] = EARLIER(Table[index])-1 ) ), BLANK() )
But I want to get the values in the yellow column. Can you help me achieve this without having to create a new index column?
Thank you

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Unknown 
You can use

 

 

datediff =
VAR CurrentDate = 'Table'[date]
VAR PreviousTable =
    FILTER (
        'Table',
        'Table'[date] < CurrentDate
            && 'Table'[condition] <> BLANK ()
    )
VAR PreviousDate =
    MAXX ( PreviousTable, 'Table'[date] )
RETURN
    IF (
        'Table'[condition] = 1
            && NOT ISEMPTY ( PreviousTable ),
        DATEDIFF ( PreviousDate, CurrentDate, DAY )
    )

 

 

View solution in original post

1 REPLY 1
tamerj1
Super User
Super User

Hi @Unknown 
You can use

 

 

datediff =
VAR CurrentDate = 'Table'[date]
VAR PreviousTable =
    FILTER (
        'Table',
        'Table'[date] < CurrentDate
            && 'Table'[condition] <> BLANK ()
    )
VAR PreviousDate =
    MAXX ( PreviousTable, 'Table'[date] )
RETURN
    IF (
        'Table'[condition] = 1
            && NOT ISEMPTY ( PreviousTable ),
        DATEDIFF ( PreviousDate, CurrentDate, DAY )
    )

 

 

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors