This will give you the total cycle time for each case. If you want the average between cases just wrap it in an average.
PU_SUM(
"CASE_TABLE",
CASE WHEN
(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice submitted for Approval'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
ELSE
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice submitted for Approval'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END)
END)
>
(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
ELSE
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END)
END)
OR
(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
ELSE
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END)
END) IS NULL
OR
(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
ELSE
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END)
END)
>
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
THEN
DATEDIFF(ms,
"ACTIVITY_TABLE"."EVENTTIME",
LEAD(
"ACTIVITY_TABLE"."EVENTTIME",
ORDER BY ("ACTIVITY_TABLE"."EVENTTIME" ASC),
PARTITION BY ("ACTIVITY_TABLE"."CASE KEY")
))
ELSE 0
END
)
Hi @dylan.koury12 Unfortunately there formula does not count the time as expected. I checked for 3 different cases and it does not result with time between the required activities.
It looks like the challenge is with applying the right index order.
Attaching the excel sheet with analysis.
Appreciate your help.
Can you try this? I added some logic to skip activities that have no invoice submitted for approval before them and no invoice approved after them
CASE
WHEN
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice submitted for Approval'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END) IS NULL THEN 0
WHEN LEAD(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Approved'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END) IS NULL AND "ACTIVITY_TABLE"."ACTIVITY (EN)" != 'Invoice Approved' THEN 0
WHEN
(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice submitted for Approval'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
ELSE
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice submitted for Approval'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END)
END)
>
(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
ELSE
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END)
END)
OR
(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
ELSE
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END)
END) IS NULL
OR
(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
ELSE
LAG(CASE WHEN "ACTIVITY_TABLE"."ACTIVITY (EN)" = 'Invoice Rejected'
THEN
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
END)
END)
>
INDEX_ORDER ( "ACTIVITY_TABLE"."ACTIVITY (EN)" ,
ORDER BY ( "ACTIVITY_TABLE"."EVENTTIME" ASC ),
PARTITION BY ( "ACTIVITY_TABLE"."CASE KEY" ) )
THEN
DATEDIFF(ms,
"ACTIVITY_TABLE"."EVENTTIME",
LEAD(
"ACTIVITY_TABLE"."EVENTTIME",
ORDER BY ("ACTIVITY_TABLE"."EVENTTIME" ASC),
PARTITION BY ("ACTIVITY_TABLE"."CASE KEY")
))
ELSE 0
END