Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
mwallman
Creator III
Creator III

Question for data modelling experts [Sample data provided]: How to analyse Start and End where the end time may be next day or on the same day?

Hello data experts,

I have the following example of data where I am unable find out exactly how to model it properly.

The goal

The goal is to analyse how long patient's have walked per day as well as how long they have slept.

The data (sample):

Activity:

   

    

DateCalories BurnedDistance
01-04-20182,9766.82
02-04-20183,0607.89
03-04-20182,5707.65
04-04-20182,5917.8
05-04-20182,6158.03
06-04-20182,70032.41
07-04-20184,38527.64
08-04-20183,00510.73
09-04-20182,53823.55
10-04-20182,68234.78
11-04-20182,69354.89
12-04-20182,63624.04
13-04-20183,32711
14-04-20183,40211.06
15-04-20183,30410.36
16-04-20182,5523.66
17-04-20182,9635.89
18-04-20182,51713.61
19-04-20182,56633.9
20-04-20182,84617.23
21-04-20183,19619.07
22-04-20183,10518.81
23-04-20182,69014.78

Sleep:

  

Start TimeEnd TimeMinutes Asleep
29-04-2018 10:11 pm30-04-2018 6:22 am415
28-04-2018 11:19 pm29-04-2018 7:28 am429
28-04-2018 12:38 am28-04-2018 4:51 am237
25-04-2018 11:09 pm26-04-2018 8:13 am515
24-04-2018 11:55 pm25-04-2018 7:21 am382
23-04-2018 11:33 pm24-04-2018 6:40 am440
22-04-2018 11:48 pm23-04-2018 6:21 am389
21-04-2018 12:50 pm22-04-2018 8:57 am474
19-04-2018 11:09 pm20-04-2018 5:57 am379
18-04-2018 11:25 pm19-04-2018 6:41 am383
17-04-2018 11:40 pm18-04-2018 6:47 am414
16-04-2018 10:53 pm17-04-2018 5:07 am333
16-04-2018 12:46 am16-04-2018 6:27 am306
14-04-2018 11:45 pm15-04-2018 7:29 am404
14-04-2018 12:02 am14-04-2018 7:46 am422
12-04-2018 11:35 pm13-04-2018 7:24 am425
11-04-2018 11:20 pm12-04-2018 6:39 am370
10-04-2018 11:01 pm11-04-2018 7:11 am425
09-04-2018 10:44 pm10-04-2018 6:51 am431
08-04-2018 11:14 pm09-04-2018 6:23 am387
08-04-2018 12:42 am08-04-2018 8:31 am423
06-04-2018 11:56 pm07-04-2018 7:29 am425
05-04-2018 11:30 pm06-04-2018 7:30 am443
05-04-2018 12:04 am05-04-2018 7:00 am356
03-04-2018 11:06 pm04-04-2018 6:22 am392
03-04-2018 12:43 am03-04-2018 6:36 am300
01-04-2018 09:37 pm02-04-2018 8:16 am470
31-03-2018 10:11 pm01-04-2018 10:15 am646

Problem:

The problem I have is the only thing that links the two data sets above is dates.

It is easy to identify steps taken per day in the Activity table. And you can get the date from the End Date in the Sleep table.

But the sleep data is problematic because I am not sure how to figure out the following logic:

  • If the Start Time is Yesterday and End Time is today then sum Time Asleep and record it for the value from the End Date
  • If the Start Time and End Time are in the same date then record it for the value from the End Date
  • If the are multiple entries (i.e. for naps) then also enter it for the value from the End Date.

Anyone know how to transform the Sleep data that works for the above logic?

1 Reply
petter
Partner - Champion III
Partner - Champion III

I think the DayStart() function can be a solution for you. It can take a dayshift parameter into account and if you specify 0.5 which is half a day or 12 hours as a dayshift it might be exactly what you need. From your example data I see that you never sleep past noon and you never start sleeping for the new day until several hours after noon...

[ACTIVITY]:

LOAD

Date(Date#([Date], 'DD-MM-YYYY') ) AS [Date],

[Calories Burned],

[Distance]

FROM [lib://ActivityAndSleep]

(html, utf8 , embedded labels, table is @1);


[SLEEP]:

LOAD

    *,

    Floor(DayStart([Start Time],0,0.5)) AS Date;

LOAD

Timestamp(Timestamp#([Start Time], 'DD-MM-YYYY hh:mm TT') ) AS [Start Time],

Timestamp(Timestamp#([End Time], 'DD-MM-YYYY h:mm TT') ) AS [End Time],

[Minutes Asleep]

FROM [lib://ActivityAndSleep]

(html, utf8 , embedded labels, table is @2);

Note line number 12 where the DayStart() has been used.