Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Connect 2026! Turn data into bold moves, April 13 -15: Learn More!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Set Analysis With Max Function Dependent on Three Fields

I have the following dataset:

NameStageTarget DateStatus
Item 1One1/1/2015Green
Item 1Two2/3/2015Yellow
Item 1Three4/5/2015Green
Item 1Three6/7/2015Green
Item 2Two2/10/2015Red
Item 3One3/1/2015Green
Item 3Two3/15/2015Green
Item 3Three4/2/2015Green
Item 3Three5/3/2015Yellow
Item 3Three6/4/2015Yellow
Item 3Three8/9/2015Red

I am looking for a set analysis expression to return the Status corresponding to the Max(Target Date) for each Stage for each Name:

The result table should look like this:

NameOne DateOne StatusTwo DateTwo StatusThree DateThree Status
Item 11/1/2015Green2/3/2015Yellow6/7/2015Green
Item 22/10/2015Red
Item 33/1/2015Green3/15/2015Green8/9/2015Red

I have tried the following but it does not quite work:

Dimensions: Name

Expressions:

One Date: =Max({<[Stage]={'One'}>} [Target Date])

One Status: =Only({<[Target Date]={"$(=Max({<[Stage]={'One'}>} [Target Date]))"} >} [Status])

Two Date: =Max({<[Stage]={'Two'}>} [Target Date])

Two Status: =Only({<[Target Date]={"$(=Max({<[Stage]={'Two'}>} [Target Date]))"} >} [Status])

Three Date: =Max({<[Stage]={'Three'}>} [Target Date])

Three Status: =Only({<[Target Date]={"$(=Max({<[Stage]={'Three'}>} [Target Date]))"} >} [Status])

I have experimented with the Aggr function but to no avail.

What am I missing?

Thanks!

1 Solution

Accepted Solutions
jerem1234
Specialist II
Specialist II

When there are duplicate dates, then firstsortedvalue doesnt know which one to return (since they are equally ranked). So it will return null. If you want some value to return, such as the first one in your data, then you can add distinct in your expression like:

firstsortedvalue(distinct {<[Stage]={'One'}>}Status,-[Target Date])



Note: This will make the expression turn red and make it look like its incorrect, but it is correct. You can use the distinct keyword in for firstsortedvalue.

Hope this helps!

View solution in original post

6 Replies
reshmakala
Creator III
Creator III

Can you try like this?

One Date = $(=Max({<[Stage]={'One'}>} [Target Date])) ??

Not applicable
Author

The issue is obtaining the correct Status field for the Max(Target Date). I am able to get the Max(Target Date) for the XXX Date field expressions. What I cannot get, reliably, are the Status fields XXX Status.

jerem1234
Specialist II
Specialist II

Used a Straight table with Name as Dimension and expressions for each 1, 2, and 3 similiar to:

One Date:

date(Max({<[Stage]={'One'}>} [Target Date]))

One Status:

firstsortedvalue({<[Stage]={'One'}>}Status,-[Target Date])

Please find attached as well.

Hope this helps!

Not applicable
Author

Well, it *almost* works. I am getting some NULL values with my real dataset using the FirstSortedValue statement. There does not seem to be a pattern as to when it returns or does not return a null Status. I need to check the dates to see if there are duplicate Target Dates which may be influencing the expression. Some of the Status are returned correctly. Some are not.

The Max(Target Date) seems to return the correct value in each instance. It is only the Status field which returns null in some instances when there is a real value present.

jerem1234
Specialist II
Specialist II

When there are duplicate dates, then firstsortedvalue doesnt know which one to return (since they are equally ranked). So it will return null. If you want some value to return, such as the first one in your data, then you can add distinct in your expression like:

firstsortedvalue(distinct {<[Stage]={'One'}>}Status,-[Target Date])



Note: This will make the expression turn red and make it look like its incorrect, but it is correct. You can use the distinct keyword in for firstsortedvalue.

Hope this helps!

Not applicable
Author

DISTINCT did it! Thanks!