Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
mellerbeck
Creator II
Creator II

Handling a duplicate max date

Given

ACCOUNT, TERMS, EFFECTIVEDATE

    1, NET 20, 2015-01-01

    1, NET 40, 2015-01-01

    2, NET 30, 2015-01-01

    2, NET 45, 2016-01-01

Would like to have the max date of the account and term returned like below. Of course the duplicate effective date makes it interesting

ACCOUNT, TERMS, EFFECTIVEDATE

1, NET 40, 2015-01-01

2, NET 45, 2016-01-01


I tried


LOAD ACCOUNT,

FirstSortedValue (TERMS,EFFECTIVEDATE + rand()) as TERMS,

date(MAX(EFFECTIVEDATE)) as EFFECTIVEDATE

Resident DATA

Group by ACCOUNT;

Which breaks the tie, but it also alternates. I would like to break the tie in a repeatable manner, say using the maxstring of terms or something?

Thanks for any thoughts!

1 Solution

Accepted Solutions
sunny_talwar

One option is to do something like this

DATA:

LOAD ACCOUNT, TERMS, DATE(EFFECTIVEDATE, 'YYYY-MM-DD') as EFFECTIVEDATE,

DATE(EFFECTIVEDATE, 'YYYY-MM-DD')*100000 + RowNo() as KEY INLINE [

    ACCOUNT, TERMS, EFFECTIVEDATE

    1, NET 20, 2015-01-01

    1, NET 40, 2015-01-01

    2, NET 30, 2015-01-01

    2, NET 45, 2016-01-01

];


Right Join (DATA)

LOAD ACCOUNT,

Max(KEY) as KEY

Resident DATA

Group By ACCOUNT;

Capture.PNG

View solution in original post

9 Replies
mellerbeck
Creator II
Creator II
Author

Is it as simple as using the distinct with the FirstSortedValue ?

LOAD ACCOUNT,

FirstSortedValue (DISTINCT TERMS, EFFECTIVEDATE) as TERMS,

date(MAX(EFFECTIVEDATE)) as EFFECTIVEDATE

Resident DATA

Group by ACCOUNT;

mellerbeck
Creator II
Creator II
Author

No, darn

vishsaggi
Champion III
Champion III

Can you try something like this?

DATA:

LOAD ACCOUNT, TERMS, SUBFIELD(TERMS, ' ') AS TERMNUM, DATE(EFFECTIVEDATE, 'YYYY-MM-DD') as EFFECTIVEDATE,

     ACCOUNT&'|'&TERMS AS KeyAccnt  INLINE [

    ACCOUNT, TERMS, EFFECTIVEDATE

    1, NET 20, 2015-01-01

    1, NET 40, 2015-01-01

    2, NET 30, 2015-01-01

    2, NET 45, 2016-01-01

];

NoConcatenate

LOAD ACCOUNT,

          Max(TERMNUM) AS TERMS,

         MAX(EFFECTIVEDATE) AS EFFECTIVEDATE

Resident DATA

Group by ACCOUNT

;

drop table DATA;

OR Just use MaxString() like below:

DATA:

LOAD ACCOUNT, TERMS, DATE(EFFECTIVEDATE, 'YYYY-MM-DD') as EFFECTIVEDATE,

     ACCOUNT&'|'&TERMS AS KeyAccnt  INLINE [

    ACCOUNT, TERMS, EFFECTIVEDATE

    1, NET 20, 2015-01-01

    1, NET 40, 2015-01-01

    2, NET 30, 2015-01-01

    2, NET 45, 2016-01-01

];

NoConcatenate

LOAD ACCOUNT,

        

     MaxString(TERMS) AS TERMS,

     MAX(EFFECTIVEDATE) AS EFFECTIVEDATE

Resident DATA

Group by ACCOUNT

;

drop table DATA;

vvira1316
Specialist II
Specialist II

You can use FirstSortedValue in Chart not in script

mellerbeck
Creator II
Creator II
Author

No, because then it will return the max of the TERMS for other entries as well

sunny_talwar

One option is to do something like this

DATA:

LOAD ACCOUNT, TERMS, DATE(EFFECTIVEDATE, 'YYYY-MM-DD') as EFFECTIVEDATE,

DATE(EFFECTIVEDATE, 'YYYY-MM-DD')*100000 + RowNo() as KEY INLINE [

    ACCOUNT, TERMS, EFFECTIVEDATE

    1, NET 20, 2015-01-01

    1, NET 40, 2015-01-01

    2, NET 30, 2015-01-01

    2, NET 45, 2016-01-01

];


Right Join (DATA)

LOAD ACCOUNT,

Max(KEY) as KEY

Resident DATA

Group By ACCOUNT;

Capture.PNG

vvira1316
Specialist II
Specialist II

My bad, I tried checking help to make sure before I commented

mellerbeck
Creator II
Creator II
Author

Thanks!