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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Jayadev
Helper I
Helper I

Calculating difference between dates comparing previous row

Hi

 

I have a table containing patients admission and discharge details. The objective is to find out patients re-admitted after discharge.  If re-admitted in less than 5 days it should be alerted.   I have calculated in excel manually, how to write DAX to achieve this result ?  Just a comparison with previous row will not help,  It should check the same paitent re-admission. In below example there is 1 patient re-admitted after 1 day.

 

Please help to find out a solution.

 

 

The sample data prepared in excel is given in the attached image.

 

readmission.PNG

 

 

2 ACCEPTED SOLUTIONS

@Jayadev

Re Admission =
VAR PreviousDischargeDate =
    CALCULATE (
        MAX ( TableName[DISCHARGEDATE] ),
        FILTER (
            ALLEXCEPT ( TableName, TableName[PatientID] ),
            TableName[Index]
                = EARLIER ( TableName[Index] ) - 1
        )
    )
RETURN
    IF (
        NOT ( ISBLANK ( PreviousDischargeDate ) ),
        IF (
            PreviousDischargeDate < TableName[AdmitDate],
            DATEDIFF ( PreviousDischargeDate, TableName[AdmitDate], DAY )
        )
    )

 

Try this revised formula

 

 


Regards
Zubair

Please try my custom visuals

View solution in original post

Hi,

 

Super solution, thanks a lot,  it works perfect. 

 

Warmest regards Jay, Once again Thanks.

View solution in original post

5 REPLIES 5
Zubair_Muhammad
Community Champion
Community Champion

Hi @Jayadev

 

Try this solution

 

First add an Index Column for each PATIENTID. This will help in further calculations

 

Index =
RANKX (
    FILTER ( TableName, [PatientID] = EARLIER ( [PatientID] ) ),
    [AdmitDate],
    ,
    ASC,
    DENSE
)
Code beutified with Dax Formatter by SQLBI

Now you can get the Re-admission calculated column using this formula

 

Re Admission =
VAR PreviousDischargeDate =
    CALCULATE (
        MAX ( TableName[DISCHARGEDATE] ),
        FILTER (
            ALLEXCEPT ( TableName, TableName[PatientID] ),
            TableName[Index]
                = EARLIER ( TableName[Index] ) - 1
        )
    )
RETURN
    IF (
        NOT ( ISBLANK ( PreviousDischargeDate ) ),
        DATEDIFF ( PreviousDischargeDate, TableName[AdmitDate], DAY )
    )

Regards
Zubair

Please try my custom visuals

Thanks a lot for your valuable inputs.  I am getting the following error.  Please help.

 

In DATEDIFF function, the start date cannot be greater than the end date

 

Regards,

Jay

 

@Jayadev

Re Admission =
VAR PreviousDischargeDate =
    CALCULATE (
        MAX ( TableName[DISCHARGEDATE] ),
        FILTER (
            ALLEXCEPT ( TableName, TableName[PatientID] ),
            TableName[Index]
                = EARLIER ( TableName[Index] ) - 1
        )
    )
RETURN
    IF (
        NOT ( ISBLANK ( PreviousDischargeDate ) ),
        IF (
            PreviousDischargeDate < TableName[AdmitDate],
            DATEDIFF ( PreviousDischargeDate, TableName[AdmitDate], DAY )
        )
    )

 

Try this revised formula

 

 


Regards
Zubair

Please try my custom visuals

Hi,

 

Super solution, thanks a lot,  it works perfect. 

 

Warmest regards Jay, Once again Thanks.

@Jayadev

 

1093.png


Regards
Zubair

Please try my custom visuals

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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