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: 
dana
Creator III
Creator III

Using Peek() and Previous() duplicate rows

Hi People,

I have these transactions for a certain car.

If the date is identical for different locations (marked in red), I would like to update a counter, using it later as a coefficient.

CarKey LocationCode TMP_Station_StartDateTime Station_EndDateTime TransNo
20-CARE013941 53 02/06/2022 00:00:00 02/06/2022 00:00:00 708253
20-CARE013941 14 02/06/2022 00:00:00 01/07/2022 00:00:00 708963
20-CARE013941 888 01/07/2022 00:00:00 01/07/2022 00:00:00 737098
20-CARE013941 53 01/07/2022 00:00:00 28/07/2022 00:00:00 737116
20-CARE013941 999 28/07/2022 00:00:00 28/07/2022 00:00:00 750255
20-CARE013941 55 28/07/2022 00:00:00 01/08/2022 00:00:00 750257
20-CARE013941 55 01/08/2022 00:00:00 19/08/2022 00:00:00 751463

 

Here is my script:

LOAD
CarKey,
TransNo,
TMP_Station_StartDateTime,
Station_EndDateTime,
LocationCode,
if(CarKey = Previous(CarKey)
and LocationCode <> Previous(LocationCode)
and TMP_Station_StartDateTime = Previous(TMP_Station_StartDateTime), Peek('Counter') + 1,0) As Counter
Resident TMP_LedgerEntry
Order By CarKey,TransNo;

The result I get is duplicates for rows with identical dates:

PEEK DUPLICATES.png

Any suggestions for fixing it?

Thanks!

Labels (1)
1 Solution

Accepted Solutions
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I think something may be missing in your example. Using the data and example exactly as you've posted, I don't see additional rows.  Are there other tables in your data model that may be contributing rows to your chart?

For example, no duplicate rows from this script:

TMP_LedgerEntry:
LOAD CarKey,
    
LocationCode,
    
TMP_Station_StartDateTime,
    
Station_EndDateTime,
    
TransNo
FROM
[https://community.qlik.com/t5/App-Development/Using-Peek-and-Previous-duplicate-rows/td-p/2046340]
(
html, utf8, UserAgent is 'Mozilla/5.0', embedded labels, table is @1);

Counted:
LOAD
CarKey,
TransNo,
TMP_Station_StartDateTime,
Station_EndDateTime,
LocationCode,
if(CarKey = Previous(CarKey)
and LocationCode <> Previous(LocationCode)
and TMP_Station_StartDateTime = Previous(TMP_Station_StartDateTime), Peek('Counter') + 1,0) As Counter
Resident TMP_LedgerEntry
Order By CarKey,TransNo;

 

View solution in original post

2 Replies
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I think something may be missing in your example. Using the data and example exactly as you've posted, I don't see additional rows.  Are there other tables in your data model that may be contributing rows to your chart?

For example, no duplicate rows from this script:

TMP_LedgerEntry:
LOAD CarKey,
    
LocationCode,
    
TMP_Station_StartDateTime,
    
Station_EndDateTime,
    
TransNo
FROM
[https://community.qlik.com/t5/App-Development/Using-Peek-and-Previous-duplicate-rows/td-p/2046340]
(
html, utf8, UserAgent is 'Mozilla/5.0', embedded labels, table is @1);

Counted:
LOAD
CarKey,
TransNo,
TMP_Station_StartDateTime,
Station_EndDateTime,
LocationCode,
if(CarKey = Previous(CarKey)
and LocationCode <> Previous(LocationCode)
and TMP_Station_StartDateTime = Previous(TMP_Station_StartDateTime), Peek('Counter') + 1,0) As Counter
Resident TMP_LedgerEntry
Order By CarKey,TransNo;

 

dana
Creator III
Creator III
Author

Hi Rob,

You nailed it!

It was a single table with duplicate records.. exporting to Excel  of course did not "reveal" the issue.

Thanks for helping me to resolve this!