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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Returning an output within a time range

Hi, please help to write an appropriate DAX code.

 

mish_1703_0-1614621088020.png

 

The issue:

A transaction is a row that contains a Driver Number, Vehicle Number, Load Date and a Load Time.

Upon checking the fact table, if a transaction for the same driver number, same load date, and load time in the time range of an hour (before or after the current load time) is found, then return the previous vehicle number, else return blank.

Thanks!  

 

1 ACCEPTED SOLUTION
Watsky
Solution Sage
Solution Sage

Hey @Anonymous , how about this:

 

Output = 
VAR CALC =
    CALCULATE (
        MAX ( 'Table'[Load Time] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Load Date], 'Table'[Driver Number] ),
            EARLIER ( 'Table'[Load Time] ) - 'Table'[Load Time]
        )
    )
VAR PreviousRow =
    TOPN (
        1,
        FILTER (
            'Table',
            'Table'[Vehicle Number] < EARLIER ( 'Table'[Vehicle Number] )
        ),
        'Table'[Vehicle Number], DESC
    )
VAR PreviousValue =
    MINX ( PreviousRow, 'Table'[Vehicle Number] )
RETURN
    IF (
        ISBLANK ( CALC ),
        "BLANK",
        IF (
            AND (
                DATEDIFF ( 'Table'[Load Time], CALC, MINUTE ) < 60,
                DATEDIFF ( 'Table'[Load Time], CALC, MINUTE ) > -60
            ),
            PreviousValue,
            "BLANK"
        )
    )

MishOutput.png

 

 

 

 

 

 


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up! ?
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up!
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.

Proud to be a Super User!

View solution in original post

2 REPLIES 2
Watsky
Solution Sage
Solution Sage

Hey @Anonymous , how about this:

 

Output = 
VAR CALC =
    CALCULATE (
        MAX ( 'Table'[Load Time] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Load Date], 'Table'[Driver Number] ),
            EARLIER ( 'Table'[Load Time] ) - 'Table'[Load Time]
        )
    )
VAR PreviousRow =
    TOPN (
        1,
        FILTER (
            'Table',
            'Table'[Vehicle Number] < EARLIER ( 'Table'[Vehicle Number] )
        ),
        'Table'[Vehicle Number], DESC
    )
VAR PreviousValue =
    MINX ( PreviousRow, 'Table'[Vehicle Number] )
RETURN
    IF (
        ISBLANK ( CALC ),
        "BLANK",
        IF (
            AND (
                DATEDIFF ( 'Table'[Load Time], CALC, MINUTE ) < 60,
                DATEDIFF ( 'Table'[Load Time], CALC, MINUTE ) > -60
            ),
            PreviousValue,
            "BLANK"
        )
    )

MishOutput.png

 

 

 

 

 

 


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up! ?
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up!
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.

Proud to be a Super User!

Anonymous
Not applicable

Hey @Watsky, Genius! Thank you, it worked as I needed it to. 

Helpful resources

Announcements
November Fabric Update Carousel

Fabric Monthly Update - November 2025

Check out the November 2025 Fabric 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.

Users online (27)