Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Difference between peek() and previous() function

What is difference between peek() and previous() function.

Regards

Ashish Srivastava

15 Replies
Not applicable
Author

Hi John,

Thanks for your brief explanation,

1.Can i use Peek function inside into previous function.

2.Can i use previous  function inside into Peek function.

Regards,

Raja.

nihhalmca
Specialist II
Specialist II

Hi John,

We can use previous() in OUTPUT table like:

OUTPUT:

Load

  Sales,

  Previous(Sales) as Previous_Sales

From table;

nihhalmca
Specialist II
Specialist II

Hi Ashish,

Please mark it Jhon post as correct answer.

johnw
Champion III
Champion III

You can use previous() while creating an output table. But previous() looks at the previous row of the input table, not the previous row in the output table. For instance, you could not use previous(Previous_Sales), because Previous_Sales doesn't exist in the input table. But you could use peek(Previous_Sales), because peek() looks at the output table.

nihhalmca
Specialist II
Specialist II

Thanks for explanation John.

However we can use Peek() at INPUT table right, for instance

Table1:

Load

     Date

From QVD;

vLastDate  = Peek('Date',Table1);  //getting record from loaded table)

Here Table1 is INPUT table right?

Thanks,

Nihhal.

johnw
Champion III
Champion III

I probably wouldn't call it input or output since it isn't in a load statement. But you write a script like the below, and then I would completely agree that peek(X,recno()-2,'Input') is reading the input table, a counterexample to my overly-simplistic pronouncements, and it further seems to be the equivalent of previous(X).

Input:
LOAD * INLINE [
X
1
3
5
2
4
];
Output:
LOAD
X as InputX
,X+1 as X
,recno() as recno
,rowno() as rowno
,previous(X) as PrevX
,peek(X) as PeekX
,peek(X,recno()-2,'Input') as PeekInputX2
,peek(X,rowno()-2,'Output') as PeekOutputX2
RESIDENT Input
WHERE X > 2
;
DROP TABLE Input;

From the output, and I think this makes sense in terms of what the functions are doing, so I think it's always true, though I'm not fully confident:

previous(X) = peek(X,recno()-2,'Input')
peek(X)     = peek(X,rowno()-2,'Output')

So yes, when we explicitly tell peek() which table to use, it can reference any table we want - input, output, or other. As the help text states it, "If no table_name is stated, the current table is assumed." What I'm trying to drive home about this is that in a load statement, the "current table" is the table you are creating (output), not the data source you are reading data from (input). Previous() would always refer to the data source you are reading from (input), not the table you are creating (output). I think this is where most people are likely to get confused, likely to make mistakes, so it's the point I was pushing. But yes, as you point out, using the third parameter, you can make peek() look at any table, including the input table.

Peek() on the input table might even be a practical alternative to previous() in some cases. Probably easier to do peek(X,recno()-5,'Input') than previous(previous(previous(previous(X)))). It just seems a little confusing to have -2 go one step back, -5 go four steps back, but that's what happens when functions are defined inconsistently.

peek(): "0 denotes the first record, 1 the second, and so on."
recno(): "The first record is number 1."

Time for my angry face. Bad Qlik! No cookie!