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: 
Not applicable

previous not working correctly

Hi,

Attached an excel sheet i need to have the Date column show one date less if:

if(Product=Previous(Product) and ColumnFrom<Previous(ColumnFrom) and Date=Previous(Date),date(Date-1),Date) as Date

order by Product,ColumnFrom desc;

See bold 15/08/2013--> 14/08/2013  below for Product 0542

i think the requirement is best decribed by the expression above, but it is not working correctly for some products

0542MZPWDZXT  OIX - KETRZXZFEXD30/09/201331
0542MZPWDZXT  OIX - KETRZXZFEXD15/08/201331
0542GT, Hrfizylcgtqf, Oqsfdkeh14/08/201311
0542GT, Hrfizylcgtqf, Oqsfdkeh31/05/201311
0542GT, Hrfizylcgtqf, Oqsfdkeh18/03/201311
0542CG, Wyyo, Myypnps17/03/20131
0542CG, Wyyo, Myypnps07/03/20131
0542CG, Wyyo, Myypnps20/12/20121

Regards

1 Solution

Accepted Solutions
swuehl
MVP
MVP

Just sorting by Product and ColumnFrom allows QV to sort the records different from what you might need, I assume you need to keep to your original input sort order ordered additionally by Date desc.

Something like:

SET DateFormat = 'DD/MM/YYYY';

INPUT:

LOAD Product,

     Category,

     Date,

     ColumnFrom,

     recno() as RecID

FROM

[Ques date-1.xls]

(biff, embedded labels, table is Sheet1$);

RESULT:

LOAD RecID as Rowno,

    Product,

    Category,

    if(Product=Previous(Product) and ColumnFrom<Previous(ColumnFrom) and Date=Previous(Date),date(Date-1),Date) as Date,

    ColumnFrom

RESIDENT INPUT

order by Product,ColumnFrom desc, Date desc;

DROP TABLE INPUT;

View solution in original post

6 Replies
alexandros17
Partner - Champion III
Partner - Champion III

Use above, not previous

Not applicable
Author

i need to do this script only so above isn't an option

swuehl
MVP
MVP

Just sorting by Product and ColumnFrom allows QV to sort the records different from what you might need, I assume you need to keep to your original input sort order ordered additionally by Date desc.

Something like:

SET DateFormat = 'DD/MM/YYYY';

INPUT:

LOAD Product,

     Category,

     Date,

     ColumnFrom,

     recno() as RecID

FROM

[Ques date-1.xls]

(biff, embedded labels, table is Sheet1$);

RESULT:

LOAD RecID as Rowno,

    Product,

    Category,

    if(Product=Previous(Product) and ColumnFrom<Previous(ColumnFrom) and Date=Previous(Date),date(Date-1),Date) as Date,

    ColumnFrom

RESIDENT INPUT

order by Product,ColumnFrom desc, Date desc;

DROP TABLE INPUT;

Not applicable
Author

Thanks it worked perfectly.

But i need to know:

1) the role of RecID here

2) "Just sorting by Product and ColumnFrom allows QV to sort the records different from what you might need"

what is wrong with this"?

order by Product,ColumnFrom desc, Date desc;

why not order by Product,ColumnFrom,Date desc;

Thanks

swuehl
MVP
MVP

1) You can drop the RecID, you can use it of you want to sort by the previous input record of your excel table instead by Date.

If you remove the RecID / Rowno lines in the load, take care to use a NOCONCATENATE prefix with the second LOAD to avoid auto-concatenating the two tables:

SET DateFormat = 'DD/MM/YYYY';

INPUT:

LOAD Product,

     Category,

     Date,

     ColumnFrom

FROM

[Ques date-1.xls]

(biff, embedded labels, table is Sheet1$);

RESULT:

NOCONCATENATE LOAD,

    Product,

    Category,

    if(Product=Previous(Product) and ColumnFrom<Previous(ColumnFrom) and Date=Previous(Date),date(Date-1),Date) as Date,

    ColumnFrom

RESIDENT INPUT

order by Product,ColumnFrom desc, Date desc;

DROP TABLE INPUT;

2) why not order by ColumnFrom ascending (ascending is the default sort order)?

Well, that was your initial requirement, check your first post.

Not applicable
Author

Please help me with this also.

http://community.qlik.com/thread/81911

Thanks