Skip to content
Advertisement

Is there a way in SQL or Power BI where I can define jobs and figure out the time it takes to do a job and then time in between jobs?

In the table below, there are job activities and unique identifiers under lodnum. I’m trying to figure out a way to get the time difference in between a job. A job such as pallet move has two entries. As you can see in rows 1 and 2, that is a pallet move for pallet A. Imagine the first entry as picking up the pallet, and the second entry as setting it down. Rows 3 and 4 are a separate job that are moving the pallet somewhere else. Rows 7 and 9 are a different job as well, sometimes thet skip a few rows. I’d like to figure out the time it takes each user to perform each job. As well as the time in between each job for a user.

How could I do this in SQL or Power BI?

Advertisement

Answer

Some assumptions:

  • Job start and ends always on the same day? Important for the caculation.
  • Unique combination of activity / usr_id / year / month / lodnum / wh_id defines ‘one’ job

Please let us know if this is correct?

Possible solution:

First, I built a CTE (Common Table Expression) which adds two columns:

  1. [Row] time column defining the order of the jobs, based on the combination of unique columns. Results in 1 and 2: start job and end job.
  2. [Key] creating a key based on combination of unique columns, for the join later.

Second, I join two versions of the CTE: one with the filter on [Row] = 1 (start job) which joins the second one on [Key] and [Row] = 2. Then caculates the difference in seconds.

Solution 2 – Using LEAD (without join, better performance):

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement