I want to calculate the lag time of each task in this table. A lag calculates the time interval between a task is completed and the next task is initiated.
There are cases (CaseNum), a case can have different tasks (TaskNum), a stage (TaskStage) represents the current stage of a task (but it doesn't need to be continuous, we can skip a particular stage to the next one).
rank is a reference column I created to show the order of task stages under each case.
The logic of Lag is quite complicated---
- For S1: InitDate - ApproveDate
- For S2: InitDate - CompletedDate of the last completed task at S1 stage. (If there isn't any tasks at S1 stage, it will be InitDate - ApproveDate)
- For S3: InitDate - CompletedDate of the last completed task at S2. (If there isn't any tasks at S2 stage, it will be InitDate - CompletedDate of the last completed task at S1, If there isn't any tasks at S1 stage, it will be InitDate - ApproveDate)
- For S4: InitDate - CompletedDate of the last completed task at S3. (same
logic: If there isn't any tasks at S3 stage, it will be InitDate -
CompletedDate of the last completed task at S2, If there isn't any tasks at S2 stage, it will be InitDate - CompletedDate of the last completed task at S1, If there isn't any tasks at S1 stage, it will be InitDate - ApproveDate) - If the lag is negative, keep it as zero.
The background color in the table indicates pairs of dates I use to calculate the lag. Hope it can help with understand the logic. THX!!!
