Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
spetushi
Creator
Creator

Rank dates within a record

Hello,

I am loading a table in qlikview similar to this sample:

IDSeqNrDate
11105-Sep-2015
12807-Oct-2015
11426-Sep-2015
21813-Sep-2015
22115-Sep-2015
22225-Sep-2015
22705-Oct-2015
33215-Oct-2015
49221-Nov-2015
49525-Nov-2015

Would like to rank the dates within each ID from oldest to earliest. Tried different concepts using grouping and while loop, but couldn't get it to work. Desired result:

IDSeqNrDateRank
11105-Sep-20151
12807-Oct-20153
11426-Sep-20152
21813-Sep-20151
22115-Sep-20152
22225-Sep-20153
22705-Oct-20154
33215-Oct-20151
49221-Nov-20151
49525-Nov-20152

Thank you

1 Solution

Accepted Solutions
swuehl
MVP
MVP

LOAD ID,

        SeqNr,

         Date,

         Autonumber(Date, ID) as Rank

RESIDENT YourTable

ORDER BY ID, Date asc;

DROP TABLE YourTable;

View solution in original post

14 Replies
swuehl
MVP
MVP

LOAD ID,

        SeqNr,

         Date,

         Autonumber(Date, ID) as Rank

RESIDENT YourTable

ORDER BY ID, Date asc;

DROP TABLE YourTable;

pra_kale
Creator III
Creator III

Hi,

Please check this..

A:

LOAD ID,

     SeqNr,

     Date

FROM

(ooxml, embedded labels, table is Sheet1);

NoConcatenate

B:

LOAD *,

AutoNumber(Date,ID) as Rank

Resident A Order by Date asc;

DROP Table A;

EXIT Script

sunny_talwar

Another option could be to use Peek() function:

Table:

LOAD ID,

    SeqNr,

    Date

FROM

[https://community.qlik.com/thread/212687]

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

FinalTable:

LOAD *,

  If(Peek('ID') = ID, RangeSum(Peek('Rank'), 1), 1) as Rank

Resident Table

Order By ID, Date;

DROP Table Table;


Capture.PNG

Anonymous
Not applicable

That's quite cunning.  Never thought of using AutoNumber() like that.

swuehl
MVP
MVP

Yes, might be also an option. Note that there is a difference between the two options if  ID's SeqNr share a Date.

sunny_talwar

That make sense

spetushi
Creator
Creator
Author

Thank you Sunny, Swuehl, and Pra, for your responses.

With a minor modification to address the scenario where ID-SeqNr have same date, all your suggestions work.

Here is what it looks like:

FinalTable:

LOAD *,

  If(Peek('ID') = ID, RangeSum(Peek('Rank'), 1), 1) as Rank

Resident TempTable

Order By ID, SeqNr asc;

DROP Table TempTable;

OR

FinalTable:

LOAD *,

   Autonumber(SeqNr, ID) as Rank

RESIDENT TempTable

ORDER BY ID, SeqNr asc;

DROP TABLE TempTable;

Example2.PNG

Saravanan_Desingh

Hi swuehl‌,

I have tried your code. But the AutoNumber generates Rank only on the input LOAD order and not taking the ORDER BY. Am I missing something here? Can you please advise? Thank you.

sunny_talwar

Are you sure it isn't taking Order by into account? I think it is:

Script1:

Data:

LOAD * INLINE [

    ID, SeqNr, Date

    1, 11, 05-Sep-2015

    1, 28, 07-Oct-2015

    1, 14, 26-Sep-2015

    2, 18, 13-Sep-2015

    2, 21, 15-Sep-2015

    2, 22, 25-Sep-2015

    2, 27, 05-Oct-2015

    3, 32, 15-Oct-2015

    4, 92, 21-Nov-2015

    4, 95, 25-Nov-2015

];

DataOut:

LOAD ID,

        SeqNr,

        Date,

        Autonumber(Date, ID) as Rank

RESIDENT Data

ORDER BY ID, Date asc;

DROP TABLE Data;


Capture.PNG

Script2:

Data:

LOAD * INLINE [

    ID, SeqNr, Date

    1, 11, 05-Sep-2015

    1, 28, 07-Oct-2015

    1, 14, 26-Sep-2015

    2, 18, 13-Sep-2015

    2, 21, 15-Sep-2015

    2, 22, 25-Sep-2015

    2, 27, 05-Oct-2015

    3, 32, 15-Oct-2015

    4, 92, 21-Nov-2015

    4, 95, 25-Nov-2015

];

DataOut:

LOAD ID,

        SeqNr,

        Date,

        Autonumber(Date, ID) as Rank

RESIDENT Data

ORDER BY ID, Date desc;

DROP TABLE Data;

Capture.PNG

The only change between the two scripts in the Order By for Date field. Are you not seeing these changes on your end?