Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Calculating the days between two dates in the same table but different 'rows'

Hello QlikCommunity,

i have already found solutions for a lot of problems i had here but now i'm stuck and hope you can help me.

What I'm trying to do is subtract two dates and create a new field with the result.

e.g. DateCreated - DateDelivered = WaitingTime

So far this is nothing special and could easily be solved in the LOAD script.

My problem is "DateDelivered" has to come from a different DataSet (Row) in the same Table.

Please see this example for better understanding:

PartIDProcessStepIDProcessStepBatchIDDateCreatedDateDeliveredWaitingTime

1

1A1101.01.201217.01.2012-
12B1120.02.201222.02.201234
13C1103.03.201206.03.20129
14D1107.03.201210.03.20121

WaitingTime should be calculated by taking "DateDelivered" of the current DataSet (e.g. PartID = 1; BatchID = 11; ProcessStepID = 2) and subtract it from "DateCreated" of the next DataSet (e.g. PartID = 1; BatchID = 11; ProcessStepID = 3) in the same table.

I want to get the waiting/idle time between one finished step until the beginning of the other.

WaitingTime = DateCreated(ProcessStepIP=3) - DateDelivered (ProcessStepID=2)

I'm really frustated as i have tried to find a solution for about 2 days now. I'm pretty new to QV and am really hoping for you guys to help me 🙂

Thank you very much in advance!

1 Solution

Accepted Solutions
Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

Try

Load

...

If(previous(BatchID)=BatchID and previous(PartID)=PartID, DateCreated-previous(DateDelivered)) as WaitingTime

...

from ...somewhere...

Depending on your source data you may need to use the date# function on the dates to create numeric dates that can be subtracted from each other.


talk is cheap, supply exceeds demand

View solution in original post

5 Replies
Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

Try

Load

...

If(previous(BatchID)=BatchID and previous(PartID)=PartID, DateCreated-previous(DateDelivered)) as WaitingTime

...

from ...somewhere...

Depending on your source data you may need to use the date# function on the dates to create numeric dates that can be subtracted from each other.


talk is cheap, supply exceeds demand
swuehl
MVP
MVP

Maybe like this

RESULT:

LOAD

     PartID,

     ProcessStepID,

     ProcessStep,

     BatchID,

     DateCreated,

     DateDelivered,

     if(peek(PartID) = PartID, DateCreated-peek(DateDelivered)) as WaitingTime

RESIDENT TABLE

ORDER BY PartID, ProcessStepID;

drop table TABLE;

Assuming you alread loaded TABLE with above fields.

Not applicable
Author

That didn't work 😞

I should also mention that the Data is not sorted like in my example. I need to find the Row with the same BatchID and the same PartID but with the ProcessStepID of a previous ProcessStep.

Your example did calculate something but not in the correct way

PartID appears numberous times but with a different BatchID etc.

Not applicable
Author

thank you!!

After i set all my filters in the listboxes i get the correct result!

I just added one more thing:

If(previous(BatchID)=BatchID and previous(PartID)=PartID and previous(ProcessStepID)=(ProcessStepID-1), DateCreated-previous(DateDelivered)) as WaitingTime



Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

You can first load your data and then use a resident load to sort it and calculate the waiting time:

Temp:

LOAD

    PartID,

    ProcessStepID,

    ProcessStep,

    BatchID,

    DateCreated,

    DateDelivered

FROM yoursourcehere;

Result:

Load

    PartID,

    ProcessStepID,

    ProcessStep,

    BatchID,

    DateCreated,

    DateDelivered

    If(previous(BatchID)=BatchID and previous(PartID)=PartID, DateCreated-previous(DateDelivered)) as WaitingTime

Resident Temp

order by PartID, BatchID,ProcessStepID;

Drop table Temp;


talk is cheap, supply exceeds demand