Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Running total

Hi,

Given the below data:

IDHitsDate
11001/01/2014
11001/02/2014
1301/03/2014
2301/01/2014
2201/02/2014
35001/01/2014
3301/02/2014

I would like to monitor the Hits column and flag when it reaches a threshold e.g. 20.

Something like this:

IDHitsDateRunning_TotalGreater than 20?First time breach?
11001/01/201410
11001/02/201420
1301/03/201423YY
2301/01/20143
2201/02/20145
35001/01/201450YY
3301/02/201453Y

I would then like to display the date that the ID breached the threshold e.g.

IDDate
101/03/2014
301/01/2014

What is the best way to approach this?

Thanks

1 Solution

Accepted Solutions
jerem1234
Specialist II
Specialist II

Hello, Please find attached. Used this script:

Data:

Load * Inline [

ID,Hits,Date

1,10,01/01/2014

1,10,01/02/2014

1,3,01/03/2014

2,3,01/01/2014

2,2,01/02/2014

3,50,01/01/2014

3,3,01/02/2014

];

Data2:

NoConcatenate Load *, if(ID=previous(ID), rangesum(Hits, peek('Running_Total')), Hits) as Running_Total,

if(if(ID=previous(ID), rangesum(Hits, peek('Running_Total')), Hits)>20, 'Y') as [Greater than 20?],

if(if(ID=previous(ID), rangesum(Hits, peek('Running_Total')), Hits)>20 and peek('Greater than 20?')<>'Y', 'Y') as [First time breach?]

Resident Data

Order by ID asc, Date asc;

Drop Table Data;

Then for a table for the date, used set analysis in a straight table with ID as dimension and the expression:

only({<[First time breach?]={'Y'}>}Date)

Hope this helps!

View solution in original post

8 Replies
jerem1234
Specialist II
Specialist II

Hello, Please find attached. Used this script:

Data:

Load * Inline [

ID,Hits,Date

1,10,01/01/2014

1,10,01/02/2014

1,3,01/03/2014

2,3,01/01/2014

2,2,01/02/2014

3,50,01/01/2014

3,3,01/02/2014

];

Data2:

NoConcatenate Load *, if(ID=previous(ID), rangesum(Hits, peek('Running_Total')), Hits) as Running_Total,

if(if(ID=previous(ID), rangesum(Hits, peek('Running_Total')), Hits)>20, 'Y') as [Greater than 20?],

if(if(ID=previous(ID), rangesum(Hits, peek('Running_Total')), Hits)>20 and peek('Greater than 20?')<>'Y', 'Y') as [First time breach?]

Resident Data

Order by ID asc, Date asc;

Drop Table Data;

Then for a table for the date, used set analysis in a straight table with ID as dimension and the expression:

only({<[First time breach?]={'Y'}>}Date)

Hope this helps!

MK_QSL
MVP
MVP

T1:

Load

  ID,

  Hits,

  Date(Date#(Date,'MM/DD/YYYY')) as Date

Inline

[

  ID, Hits, Date

  1, 10, 01/01/2014

  1, 10, 01/02/2014

  1, 3, 01/03/2014

  2, 3, 01/01/2014

  2, 2, 01/02/2014

  3, 50, 01/01/2014

  3, 3, 01/02/2014

];

T2:

Load

  *,

  IF(CumulativeHits >= 20, 1) as CumulativeHitsFlag;

Load

  ID,

  Hits,

  Date,

  If(ID = Previous(ID), RangeSum(Peek('CumulativeHits'),Hits),Hits) as CumulativeHits

Resident T1

Order By ID, Date;

Left Join

Load

  ID,

  Date(Min(Date)) as MinDate

Resident T2 Where CumulativeHitsFlag = 1

Group By ID;

Drop Table T1;

Drop Fields CumulativeHitsFlag, CumulativeHits;

datanibbler
Champion
Champion

Hi PortoLad,

use the PEEK() function:

- First sort your table by that ID so you have all records with the same ID in one group, then use sth. like this

>> IF(ID = PEEK(ID, -1), PEEK(Hits, -1) + Hits, Hits) as running_total <<

You just have to take care of the very first record since the PEEK() fct. won't work there,

so you have to build in an additional IF() statement for a line_ID (Recno())

HTH

Best regards,

DataNibbler

Not applicable
Author

You could use a few preceding load statements to sort this out:

Sample:

Load *,

If(ID = Peek('ID'), If(Peek('TotalHits') <= 20 and TotalHits > 20,'Y','N'),[>20Flag]) as FirstBreach,

Peek('TotalHits') as Tester

;

Load *,

If(TotalHits > 20,'Y','N') as [>20Flag]

;

Load *,

If(Peek('ID') = ID, Peek('TotalHits'),0) + Hits as TotalHits

;

Load * Inline [

ID, Hits, Date

1, 10, 41640

1, 10, 41641

1, 3, 41642

2, 3, 41640

2, 2, 41641

3, 50, 41640

3, 3, 41641

]

;

Not applicable
Author

I found it helpful.

Not applicable
Author

That has been very helpful - thanks!

Not applicable
Author

THANKS FOR SHARING, THIS HAS REALLY HELPED ME !!!

Not applicable
Author

thanks to Jeremiah, Manish, Friedrich, Tyler and Janaki for their help