Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
ciaran_mcgowan
Partner - Creator III
Partner - Creator III

View data in next row

Hi all,

Is it possible to view the value of a field in the next row? I'm looking to flag a record if the record that follows it meets certain criteria, then flag that following record if it's previous one meets certain criteria. Basically, I need to display records that occur within 2 hours of each other. So far I have this:

if(Total_Hours-Previous(Total_Hours)<=2, 1) AS Flag

This works fine for returning the second or third records that occurs in less than 2 hours but it neglects the first one most of the time. This is what I'm getting with the Previous() function:

IDJOB_COMPLETETIMETOTAL_HOURSFLAG
1Yes09:000
2Yes12:003
3Yes13:0041
4Yes14:305:301
5Yes16:457:45
6Yes17:308:301
7Yes18:009:001

You can see that it's missed record #2 as record #1 was three hours before it, even though I don't even want to include it. I need to show both records that are with the time period so if there is a way to look ahead in the load script I can use that in conjunction with Previous() (like below):

IDJOB_COMPLETETIMETOTAL_HOURSFLAG
2Yes12:0031
3Yes13:0041
4Yes14:305:301
6Yes17:308:301
7Yes18:009:001

As always, if anyone has any better approaches/recommendations, I would be extremely grateful.

Thanks.

1 Solution

Accepted Solutions
maxgro
MVP
MVP


I read your data twice

- first time to set the flag based on time diff (2 hour)

- second time in reverse order to add the missing record; see newflag

RESULT IS

110637.png

SCRIPT IS

DIRECTORY;

Source:

LOAD ID,

    JOB_COMPLETE,

    TIME,

    TOTAL_HOURS

FROM

[http://community.qlik.com/thread/110637]

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

Table:

LOAD

  ID,

  JOB_COMPLETE,

  TIME,

  interval(TIME-Peek(TIME), 'hh:mm') as TOTAL_HOURS,

  if(interval(TIME-Peek(TIME), 'mm') <= interval('09:00'-'07:00', 'hh:mm') ,1)  as flag

Resident

  Source;

DROP Table Source;

Final:

LOAD

  ID,

  JOB_COMPLETE,

  TIME,

  TOTAL_HOURS,

  flag,

  if(peek(flag)=1 or flag=1,1) as newflag

Resident

  Table

order by ID desc;

DROP Table Table;

View solution in original post

6 Replies
jagan
Luminary Alumni
Luminary Alumni

Hi,

Try this script

Data:

LOAD

*,

if(DecimalHours-Alt(Previous(DecimalHours), 0)<= 2, 1) AS Flag;

LOAD

*,

Num((Num(SubField(TOTAL_HOURS, ':', 1)) + Alt(Num(SubField(TOTAL_HOURS, ':', 2)/60, '##0.##'), 0)), '##0.##') AS DecimalHours;

LOAD ID,

     JOB_COMPLETE,

     TIME,

     TOTAL_HOURS

FROM

[http://community.qlik.com/thread/110637]

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

Regards,

Jagan.

ciaran_mcgowan
Partner - Creator III
Partner - Creator III
Author

Hi Jagan,

I'm not sure what you're suggesting. Are these three separate loads?

jagan
Luminary Alumni
Luminary Alumni

Hi,

Try this script and check, all are single load only, it is call Precedent Load.

Regards,

Jagan.

maxgro
MVP
MVP


I read your data twice

- first time to set the flag based on time diff (2 hour)

- second time in reverse order to add the missing record; see newflag

RESULT IS

110637.png

SCRIPT IS

DIRECTORY;

Source:

LOAD ID,

    JOB_COMPLETE,

    TIME,

    TOTAL_HOURS

FROM

[http://community.qlik.com/thread/110637]

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

Table:

LOAD

  ID,

  JOB_COMPLETE,

  TIME,

  interval(TIME-Peek(TIME), 'hh:mm') as TOTAL_HOURS,

  if(interval(TIME-Peek(TIME), 'mm') <= interval('09:00'-'07:00', 'hh:mm') ,1)  as flag

Resident

  Source;

DROP Table Source;

Final:

LOAD

  ID,

  JOB_COMPLETE,

  TIME,

  TOTAL_HOURS,

  flag,

  if(peek(flag)=1 or flag=1,1) as newflag

Resident

  Table

order by ID desc;

DROP Table Table;

ciaran_mcgowan
Partner - Creator III
Partner - Creator III
Author

Hi Jagan,

Thanks for your help but I couldn't get that to work. I ended up going with Massimo's solution in the end.

ciaran_mcgowan
Partner - Creator III
Partner - Creator III
Author

Hi Massimo,

Thanks for your help. This is the first solution I thought of but didn't want to go with it as it seem inefficient but after spending some time trying to find a better way, I think in this case the simplest solution is the best one.

Thanks you.

Ciarán