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
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


@ 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!:
The Definitive Guide to Power Query (M)

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

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.