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

sort & firstvalue

Customer #Start DateEnd dateContractTo be picked in results final
12Nov-5-2013Nov-6-2014XYZdropped
12Nov-5-2014Nov-8-2015PTRyes because End date is later for same customer
123Oct-15-2014Dec-16-2015YYZyes only record for that customer
324Nov-5-2015Nov-6-2016TYPdropped
345Nov-5-2016Nov-6-2016XPXyes only record for that customer
324Nov-7-2015Nov-6-2016DDYyes because start date is later for same customer
Final results
12Nov-5-2014Nov-8-2015PTR
123Oct-15-2014Dec-16-2015YYZ
345Nov-5-2016Nov-6-2016XPX
324Nov-7-2015Nov-6-2016DDY

How can i achieve above results. I have bunch of customers where latest contract needs to be picked. The criteria for latest contract is:

1) Where end date is latest, even if start date varies.

2) If end date is same however start date varies then pick the latest start date.

3 Replies
maxgro
MVP
MVP

in a script

with a resident load order by end date desc and also start date desc

use the peek function to only get first customer

MarcoWedel

Hi,

one solution could be:

QlikCommunity_Thread_143528_Pic1.JPG.jpg

table1:

LOAD [Customer #],

    Date#([Start Date],'MMM-DD-YYYY') as [Start Date],

    Date#([End date],'MMM-DD-YYYY') as [End Date],

    Contract

FROM [http://community.qlik.com/thread/143528]

(html, codepage is 1252, embedded labels, table is @1, filters(Remove(Row, Pos(Top, 12)),Remove(Row, Pos(Top, 11)),Remove(Row, Pos(Top, 10)),Remove(Row, Pos(Top, 9)),Remove(Row, Pos(Top, 8)),Remove(Col, Pos(Top, 5))));

Right Join

LOAD [Customer #],

    Max([Start Date]) as [Start Date]

Resident table1

Group By [Customer #];

hope this helps

regards

Marco

Not applicable
Author

Imagine you have the following table:

FieldA, FieldB

A, 3

B, 4

C, 1

D, 2

If you run a FirstSortedValue(FieldA, FieldB) you'll get the first sorted value from FieldA based on the sorting of FieldB. Value 1 is the first value for FieldB so the function will in this example return C.