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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
arythedj
Helper I
Helper I

Detect Previous Job DropOff Time

Hi there,

Hope you are doing well.

I need help for the below.

arythedj_0-1632195551062.png

Duration (mins) is the column I need to create.

Grouping by Driver ID, the blue highlighted are using [Drop Time] - [Accept Time]

The green highlighted are using [Drop Time] - previous job [Drop Time]

We need to check if the job [Accept Time] is after the previous job [Drop Time]. then use [Drop Time] - [Accept Time]

if the job [Accept Time] is before the previous job [Drop Time]. then use [Drop Time] - previous job [Drop Time]

 

What's the best way to solve this?

I am thinking if I have another column "accept_is_after_previous_job", this will be used as a helper?

arythedj_1-1632195941381.png

So if value is 1, then then use [Drop Time] - [Accept Time]

If value is 0, then use [Drop Time] - previous job [Drop Time]

 

The questions are:

- What's the DAX for "accept_is_after_previous_job" by Driver ID?

- What's the DAX for [Drop Time] - previous job [Drop Time] by Driver ID?

 

Many thanks for the help.

 

Warm regards,

Ary

1 ACCEPTED SOLUTION

Hi @arythedj 

 

First add a new column with below code to rank the rows by Drop time:

Rank =
RANKX (
    FILTER ( 'Table', 'Table'[Driver ID] = EARLIER ( 'Table'[Driver ID] ) ),
    'Table'[Drop Time],
    ,
    ASC,
    DENSE
)

Then use below code to add a Duration (mins) column:

Duration (mins) =
VAR _Drop = 'Table'[Drop Time]
VAR _DropRank = 'Table'[Rank]
VAR _LDrop =
    CALCULATE (
        MAX ( 'Table'[Drop Time] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Rank] = ( _DropRank - 1 )
                && 'Table'[Driver ID] = EARLIER ( 'Table'[Driver ID] )
        )
    )
VAR _AT = 'Table'[Accept Time]
RETURN
    IF (
        _AT > _LDrop,
        DATEDIFF ( _AT, _Drop, MINUTE ),
        DATEDIFF ( _LDrop, _Drop, MINUTE )
    )

 

Output:

VahidDM_0-1632205732560.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos✌️!!

View solution in original post

6 REPLIES 6
VahidDM
Super User
Super User

Hi @arythedj 

 

You need to add a Index [rank] column to your table then use that column to find the previous job.

Can you share your data in a table format to be able to copy paste that?

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos🙏!!

Hi @VahidDM 

 

Below is the data in a table format, is this sufficient?

 

Driver IDAccept TimeDrop Timeaccept_is_after_previous_jobDuration (mins)
45621/09/2021 10:3021/09/2021 10:40110
45621/09/2021 10:4521/09/2021 11:00115
45621/09/2021 10:4621/09/2021 11:10010
45621/09/2021 11:3021/09/2021 11:45115
45621/09/2021 11:3021/09/2021 11:55010
45621/09/2021 11:3221/09/2021 11:5803
41021/09/2021 11:2521/09/2021 11:45120
41021/09/2021 11:3021/09/2021 11:5005
41021/09/2021 11:3121/09/2021 11:5505

 

Ary

Hi @arythedj 

 

First add a new column with below code to rank the rows by Drop time:

Rank =
RANKX (
    FILTER ( 'Table', 'Table'[Driver ID] = EARLIER ( 'Table'[Driver ID] ) ),
    'Table'[Drop Time],
    ,
    ASC,
    DENSE
)

Then use below code to add a Duration (mins) column:

Duration (mins) =
VAR _Drop = 'Table'[Drop Time]
VAR _DropRank = 'Table'[Rank]
VAR _LDrop =
    CALCULATE (
        MAX ( 'Table'[Drop Time] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Rank] = ( _DropRank - 1 )
                && 'Table'[Driver ID] = EARLIER ( 'Table'[Driver ID] )
        )
    )
VAR _AT = 'Table'[Accept Time]
RETURN
    IF (
        _AT > _LDrop,
        DATEDIFF ( _AT, _Drop, MINUTE ),
        DATEDIFF ( _LDrop, _Drop, MINUTE )
    )

 

Output:

VahidDM_0-1632205732560.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos✌️!!

Greg_Deckler
Super User
Super User

@arythedj I recently just published something for this:

Decimal Duration Converter - Microsoft Power BI Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Are you spamming?
You replied 13 seconds after I posted and your answer is irrelevant to my questions.

@arythedj So, if you subtract your two date/time columns and then use the link I sent where you convert that decimal time to minutes that is irrelevant and spamming. Good to know.

Duration (mins) =
  VAR __Value = ([Drop Time] - [Accept Time]) * 1.
RETURN
  TRUNC ( __Value * 24*60 )
  

As for the getting the previous row part of your post, I suppose I could have included the link for MTBF but it was late and I was going to bed. 

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.