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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Gwin
Regular Visitor

Seeking help implementing Dax - DriveTime

Can anyone help please?

 

Using this function from erik_tarnvik :

 

DriveTime =

VAR DT =

    DATEDIFF (

        MAXX (

            FILTER (

                ALLSELECTED ( Driving ),

                Driving[ID User] = MAX ( Driving[ID User] )

                    && Driving[Check Out] < MAX ( Driving[Check In] )

            ),

            Driving[Check Out]

        ),

        MAX ( Driving[Check In] ),

        DAY

    )

VAR DM =

    DATEDIFF (

        MAXX (

            FILTER (

                ALLSELECTED ( Driving ),

                Driving[ID User] = MAX ( Driving[ID User] )

                    && Driving[Check Out] < MAX ( Driving[Check In] )

            ),

            Driving[Check Out]

        ),

        MAX ( Driving[Check In] ),

        MINUTE

    )

RETURN

    IF ( DT = 0, DM, BLANK () )

 

I am getting an error massage:

MdxScript(Model) (5, 5) Calculation error in measure 'Driving'[DriveTime]: In DATEDIFF function, the start date cannot be greater than the end date) 

 

My data is:

ID UserID StoreCheck InCheck OutTime Diff
1A10/18/2017 9:1510/18/2017 9:580:43:12
1B10/18/2017 10:5610/18/2017 11:320:36:00
1C10/18/2017 12:2910/18/2017 13:200:50:24
1D10/18/2017 14:1710/18/2017 15:221:04:48
1W10/19/2017 9:0510/19/2017 9:260:21:36
1X10/19/2017 10:2410/19/2017 11:501:26:24
1Y10/19/2017 12:4810/19/2017 13:450:57:36
1Z10/19/2017 14:4310/19/2017 15:290:46:05

 

Thank you in advance.

Regards

Gwin

1 ACCEPTED SOLUTION
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @Gwin,

 

As the error message prompted, in DATEDIFF function, the start date cannot be greater than the end date. 

Syntax: DATEDIFF(<start_date>, <end_date>, <interval>)

 

So, please make a little modification to your formula:

DriveTime 2 = 
VAR DT =
    DATEDIFF (
        MAX ( Driving[Check In] ),
        MAXX (
            FILTER (
                ALLSELECTED ( Driving ),
                Driving[ID User] = MAX ( Driving[ID User] )
                    && Driving[Check Out] < MAX ( Driving[Check In] )
            ),
            Driving[Check Out]
        ),
        
        DAY
    )
VAR DM =
    DATEDIFF (
        MAX ( Driving[Check In] ),
        MAXX (
            FILTER (
                ALLSELECTED ( Driving ),
                Driving[ID User] = MAX ( Driving[ID User] )
                    && Driving[Check Out] < MAX ( Driving[Check In] )
            ),
            Driving[Check Out]
        ),
        
        MINUTE
    )
RETURN
    IF ( DT = 0, DM, BLANK () )

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @Gwin,

 

As the error message prompted, in DATEDIFF function, the start date cannot be greater than the end date. 

Syntax: DATEDIFF(<start_date>, <end_date>, <interval>)

 

So, please make a little modification to your formula:

DriveTime 2 = 
VAR DT =
    DATEDIFF (
        MAX ( Driving[Check In] ),
        MAXX (
            FILTER (
                ALLSELECTED ( Driving ),
                Driving[ID User] = MAX ( Driving[ID User] )
                    && Driving[Check Out] < MAX ( Driving[Check In] )
            ),
            Driving[Check Out]
        ),
        
        DAY
    )
VAR DM =
    DATEDIFF (
        MAX ( Driving[Check In] ),
        MAXX (
            FILTER (
                ALLSELECTED ( Driving ),
                Driving[ID User] = MAX ( Driving[ID User] )
                    && Driving[Check Out] < MAX ( Driving[Check In] )
            ),
            Driving[Check Out]
        ),
        
        MINUTE
    )
RETURN
    IF ( DT = 0, DM, BLANK () )

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
MattAllington
Community Champion
Community Champion

The error message suggests you have your check in and check out date comparisons back to front. 



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Do you mind running my DAX? the dates are labelled properly. Thanks

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors