Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to Sum within a group

Hi,

I am having hard time computing difference in time for each "Key" within each day. Can someone please suggest how to do this in QlikView? I can do this Excel and Matlab. I am not an expert, but fairly familiar with Expressions and programming in Qlik. I have the following data and we want to compute:

1. Duration = the difference between "Complete Time" minus "Start" time for each "Key within the same "Date"

2. Then sum those "Duration" for each "Key" only within each day

3. Find percentage of total duration each key took across all keys within the same day. Duration of each key divided by (Minimum(Start) time across any key - Max (Complete) time across any key for the same day.

So from below sample, we want answers to be:

On date 6-26-2013, the total duration for Key 5 will be "25 minutes" + "1 minute" = "26 minutes".

Column

- N is a unique number

- Key is identifier

- Date

- Start is starting date and time

- Complete is complete date and time

NKeyDateStartComplete
1116/26/2013 0:006/26/2013 19:066/26/2013 19:06
1216/26/2013 0:006/26/2013 19:076/26/2013 19:33
1326/26/2013 0:006/26/2013 19:086/26/2013 19:34
1436/26/2013 0:006/26/2013 19:056/26/2013 19:31
1546/26/2013 0:006/26/2013 19:056/26/2013 19:30
1656/26/2013 0:006/26/2013 19:116/26/2013 19:36
1756/26/2013 0:006/26/2013 19:096/26/2013 19:10
1816/27/2013 0:006/27/2013 8:556/27/2013 8:55
1916/27/2013 0:006/27/2013 9:186/27/2013 9:43
2026/27/2013 0:006/27/2013 8:286/27/2013 8:28
2126/27/2013 0:006/27/2013 8:496/27/2013 9:14
2276/27/2013 0:006/27/2013 9:146/27/2013 9:39
2316/28/2013 0:006/28/2013 9:056/28/2013 9:30
2416/28/2013 0:006/28/2013 11:146/28/2013 11:39
2526/28/2013 0:006/28/2013 8:546/28/2013 9:19
2636/28/2013 0:006/28/2013 9:346/28/2013 9:59
2736/28/2013 0:006/28/2013 11:276/28/2013 11:52
2846/28/2013 0:006/28/2013 10:116/28/2013 10:36
2976/28/2013 0:006/28/2013 11:256/28/2013 11:50

Thanks,

1 Solution

Accepted Solutions
petter
Partner - Champion III
Partner - Champion III

10 Replies
sunny_talwar

You are trying to do this in the script or front end?

Not applicable
Author

Hi

I was trying this is Expression. I now plan to do this in script

swuehl
MVP
MVP

Maybe like this:

Set TimestampFormat = 'M/DD/YYYY h:mm';

LOAD Key,

  Date,

  interval(sum([Complete Time]-Start)) as Interval

GROUP BY Key, Date;

LOAD N,

    Key,

    Date,

    Start,

    [Complete Time]

FROM

[https://community.qlik.com/thread/171364]

(html, codepage is 1252, embedded labels, table is @1);

maxgro
MVP
MVP

if you want this, theexpression is

sum((Date#(Complete, 'MM/DD/YYYY hh:mm') - Date#(Start, 'MM/DD/YYYY hh:mm')))

formatted in number tab as interval

1.png

sunny_talwar

Try this:

Table:

LOAD *,

  Interval(Complete - Start, 'hh:mm') as Duration;

LOAD N,

     Key,

     Date(TimeStamp#(Date, 'M/DD/YYYY h:mm')) as Date,

     TimeStamp#(Start, 'M/DD/YYYY h:mm') as Start,

     TimeStamp#(Complete, 'M/DD/YYYY h:mm') as Complete

FROM

[https://community.qlik.com/thread/171364]

(html, codepage is 1252, embedded labels, table is @1);

Join(Table)

LOAD Key,

  Date,

  Interval(Sum(Duration), 'hh:mm')  as [Total Duration]

Resident Table

Group By Key, Date;

FinalTable:

LOAD *,

  Duration/[Total Duration] as Percentage

Resident Table;

DROP Table Table;

Output:

Capture.PNG

petter
Partner - Champion III
Partner - Champion III

2015-07-06 #1 How to Sum within a Sub Loop.PNG

Not applicable
Author

If none of above worked for you, maybe your qlik settings are inpropper.

Use the below setting at the begining of the script.

SET TimestampFormat= 'MM/DD/YYYY hh:mm'; //it will ensure that qlik will recognize dates

and in front end use  expression:

=SUM(Timestamp#(Complete)-Timestamp#(Start))

in straight or pivot table to calculate sum of differnces.

Not applicable
Author

Thanks SunIndia

Not applicable
Author

Hi,

I applied the logic provided by petter-s , but it did not work initial. After applying the format you provided, it worked. This is what I do not like about QlikView. It is very very tricky.

By default, the date and time format was:

SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';

this did not work

I changed this to:

SET TimestampFormat= 'MM/DD/YYYY hh:mm';

It worked.

Thanks,

Raghu