Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
atulkrsingh
Contributor II
Contributor II

Finding unique time intervals in case of overlap

Hi there,

I'm new to Qlik and having trouble with the following dataset:

Patient IDAppointment DateDoctorStart timeEnd timeAppointment time
11-Jan-20A9:00 AM10:00 AM1:00
21-Jan-20A11:00 AM11:30 AM1:00
31-Jan-20A11:00 AM12:15 PM1:15
41-Jan-20A12:00 PM1:00 PM1:00
51-Jan-20A4:00 PM5:00 PM1:00
61-Jan-20B10:00 AM11:30 AM1:30
71-Jan-20B4:00 PM5:00 PM1:00
81-Jan-20B4:00 PM5:00 PM1:00

 

I need to find out the actual work time for the practitioners,

Here, 

For, Practitioner A - Total Appointment time = 4:45:00;
                                         Actual Work Time = 4:00:00

For, Practitioner B - Total Appointment time = 3:30:00;
                                         Actual Work Time = 2:30:00

 

Required Output:

Appointment DateDoctorActual Work Time
1-Jan-20A4:00:00
1-Jan-20B2:30:00

 

I was thinking of,
                  Actual Work Time =   Aggr(Sum(Interval( Min([Start Date]) - Max([End Date]))   - (Total time of NO Work ))

However, I'm unsure of how to write the query for Total Time of No Work (i.e., when the Practitioner is not working)

Thanks in Advance for your help

 

Best Regards,

Atul 

 

1 Solution

Accepted Solutions
Kushal_Chawda

try below

Data:
LOAD
    "Patient ID",
    "Appointment Date",
    Doctor,
    Time#("Start time",'hh:mm TT') as "Start time",
    Time#("End time",'hh:mm TT') as "End time",
    Time#("Appointment time",'hh:mm') as "Appointment time"
FROM [lib://Qlik]
(html, utf8, embedded labels, table is @1);

New:
Load *,
     rangesum(if(Peek(Doctor)=Doctor and Peek("Appointment Date")="Appointment Date", 
     if("Start time">Peek("End time"),
     "Start time"-Peek("End time"),0))) as No_work_Hours
Resident Data
Order by Doctor,"Appointment Date","Start time";

Drop Table Data;

//Now on frond end create table
Dimensions:
1) Appointment Date
2) Doctor

Expression:
1)Actual Work Time
=time(sum(aggr((max([End time])-min([Start time])) -sum(No_work_Hours),[Appointment Date],Doctor)))
2) No Work Time
=time(sum(No_work_Hours))

View solution in original post

10 Replies
Saravanan_Desingh

Your data doesn't show [Total time of NO Work]. Can you add that data to your sample?

atulkrsingh
Contributor II
Contributor II
Author

Hi,

[Total time of NO Work] is the time when the practitioner is free. It is something I need to calculate and I'm unable to understand the logic for the same.

Ex- in case of practitioner A,

Time of No work = 1 hr( Between 10:00 AM and 11: AM) + 3 hrs (Between 1 PM to 4PM)

So, the Actual Work Hours = Total Time Available - Time of No work

that is, 8 - 4 hours = 4 hours

Kushal_Chawda

for A, total time available isn't 9 (4 Am - 1Pm)>

atulkrsingh
Contributor II
Contributor II
Author

Thanks for your response and correcting me Kush,

It was a wrong entry. It will be 4:00 PM to 5:00 PM. I have corrected it.

Hence, Available time is from  9:00 AM to 5:00 PM (8 hours)

Kushal_Chawda

try below

Data:
LOAD
    "Patient ID",
    "Appointment Date",
    Doctor,
    Time#("Start time",'hh:mm TT') as "Start time",
    Time#("End time",'hh:mm TT') as "End time",
    Time#("Appointment time",'hh:mm') as "Appointment time"
FROM [lib://Qlik]
(html, utf8, embedded labels, table is @1);

New:
Load *,
     rangesum(if(Peek(Doctor)=Doctor and Peek("Appointment Date")="Appointment Date", 
     if("Start time">Peek("End time"),
     "Start time"-Peek("End time"),0))) as No_work_Hours
Resident Data
Order by Doctor,"Appointment Date","Start time";

Drop Table Data;

//Now on frond end create table
Dimensions:
1) Appointment Date
2) Doctor

Expression:
1)Actual Work Time
=time(sum(aggr((max([End time])-min([Start time])) -sum(No_work_Hours),[Appointment Date],Doctor)))
2) No Work Time
=time(sum(No_work_Hours))
atulkrsingh
Contributor II
Contributor II
Author

Thanks Kush! 🙂

atulkrsingh
Contributor II
Contributor II
Author

Appt DateDoctorPatient IDStart timeEnd timeNo Work Time
     1:30
6/16/2020A18:15:009:00:000:00
6/16/2020A29:00:0010:00:000:00
6/16/2020A39:15:009:30:000:00
6/16/2020A410:00:0011:00:000:00
6/16/2020A510:00:0010:30:000:30
6/16/2020A611:00:0012:00:000:00
6/16/2020A713:00:0014:00:001:00
6/16/2020A814:00:0014:30:000:00
6/16/2020A914:00:0014:45:000:00
6/16/2020A1014:30:0015:30:000:00
6/16/2020A1114:30:0015:30:000:00

 

Hi Kush,

While most of the output is correct, I'm getting the wrong output for many such cases (marked in 'red'). Can't figure out why this is happening. Can you please help?

Thanks,

Atul

Kushal_Chawda

How you are getting 30 Minutes no work as he already attended Patient from 10 to 11 AM? should it now be 30 minutes for patient id 6?

atulkrsingh
Contributor II
Contributor II
Author

That is the error. It should have been "0 mins" instead of 30 min for patient id - 5. There are a few anomalies like this else, the logic you gave works fine for other records. "No work time" for patient id - 6 will be zero and is correct