Missing Manual - Peek()

              1f4fe19.jpg

        Have you ever wondered how the examples from the Qlikview help may look like?

    Please see below and enjoy responsibly...

     

    Peek()

    not a boo...

       

    This function belongs to to Inter-Record functions and can only be used in the script (back-end).


    l-Peek-a-Boo.jpg

    Description(Qlikview Help)

    Returns the contents of the fieldname in the record specified by row in the internal table tablename. Data are fetched from the associative QlikView database.

    Fieldname must be given as a string (e.g. a quoted literal).

    Row must be an integer. 0 denotes the first record, 1 the second and so on. Negative numbers indicate order from the end of the table. -1 denotes the last record read.

    If no row is stated, -1 is assumed.

    Tablename is a table label  without the ending colon. If no tablename is stated, the current table is assumed. If used outside the load statement or referring to another table, the tablename must be included.



     

    Syntax:

    2015-06-15_235908.png

     

    Field_name -  is a name of of your field(column)

    row_no - the row from which the data is returned

    (0 is first row

    1 is second row

    and so on..

    -1 is the last row)

    table_name - a name of table from where the data are fetched

     

     

    Returns values from previous row or row specified by the row-no argument.

    • If no row is stated, -1 is assumed.
    • you can reference field that was not been previously loaded (created on the with AS alias)(See "Bonus 2" example)
    • From the Help description "Fieldname must be given as a string (e.g. a quoted literal)."

              This is true only if we use this function to create variable (please see below examples)


    Data Model
    (Copy and Pasted below code into Edit Script window and reload)


    Tab1:

    load

    peek(Sales) as S1,

    peek( Sales,2 ) as S2,

    peek( Sales,-2 ) as S3,

    if(rowno()=1,Sales,Peek(RunnigTotal,-1)+Sales) as RunnigTotal,

    Previous(Sales) as PSales,

    numsum( Sales, peek( 'Sales' ) ) as Bsum,

    Sales

    inline [

    Sales

    100

    200

    300

    400

    ]

    ;

     

    load

    peek( Sales, 0, 'Tab1' )as S4

    resident Tab1;

     

    LET vpeek= peek( 'Sales', -1, 'Tab1' ) ;

     

    ligtbulb.jpgPlease see below swuehl comments about difference when you use negative or positive numbers as second argument.

     

     

     

    Is worth to mention that Peek() is reading from Output table(opposite to Previous() function which reads from Input Table.

     

    Example:

    OutputTable:

    LOAD field

    RESIDENT InputTable;


    source:Difference between peek() and previous() funcation

     

     

     

    Example 1


    In this example as we did not specify the row_no argument, all but last rows are returned.


                                      peek( 'Sales' )

    peek1.png

                                                      ligtbulb.jpgpeek( 'Sales' ) = peek( 'Sales',-1)



    Example 2


    IF the row_no argument  is added only ONE value is returned


                            peek( 'Sales',2 )  ligtbulb.jpg Remember 2 = third row

    peek2.png                       



    Example 3


    If we use negatives numbers as row_no  the return value is our initial table minus number of rows specified by the second argument.


                                  peek( 'Sales',-2 )


    2015-06-17_000648.png


    As you see the last 2 rows have been removed from the table.


    Example 4


    The below syntax is used when we want to return value from external table(see data model)


    peek( 'Sales', 0, 'Tab1' ) as S4

     

    2015-06-17_004944.png

     


    Example 5


    Each row is a SUM of current row + one row above.


    numsum( Sales, peek( 'Sales' ) ) as Bsum

    2015-06-16_213048.png



    Bonus

    How to store value into variable:


    With below example

    LET vpeek= peek( 'Sales', -1, 'Tab1' ) ;

     

    we will store value of 400  in variable vpeek

    and then use in front end development.

    2015-06-17_001509.png

     

     

    Bonus 2


    As previously stated we can use Peek to return values from field that was not been yet created.


    How does it wok?


    In our data model we have this line:


    if(rowno()=1,Sales,Peek(RunnigTotal,-1)+Sales) as RunnigTotal,


    Although RuningTotal has not yet been loaded we can return the values from that line:


    2015-06-19_145812.png


    Still feeling Qlikngry?


    How To /Missing Manual(15+ articles)