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

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Help with FirstSortedValue() and Aggr()

Hi all,

I want to use the Aggr() function inside of FirstSortedvalue(). The only way I've ever used FirstSortedValue is on static values in a field. I'm not sure if what I want to do is possible. See below expression:

=FirstSortedValue(POSupplierNameClean, Aggr(if(Avg(PODate - PODateApproval) < 0, 0, Avg(PODate - PODateApproval)), POSupplierNameClean))

I want to average the difference in PODate and PODateApproval ("Avg. PO Days"), grouped by supplier. Once I have that average, I want to pick the supplier with the largest "Average PO Day." The expression is not calculating.

1 Solution

Accepted Solutions
Not applicable
Author

I figured it out. The expression needed the DISTINCT keyword in case there were multiple suppliers with the same value, as shown below:

=FirstSortedValue(DISTINCT POSupplierNameClean, Aggr(if(Avg(PODate - PODateApproval) < 0, 0, Avg(PODate - PODateApproval)), POSupplierNameClean))

View solution in original post

4 Replies
MK_QSL
MVP
MVP

If possible, please provide sample data.

FirstSortedValue + Aggr >> A very good video available on QlikShare.com

Try to search by Aggr...

Not applicable
Author

Sorry, I forgot to mention that this is in a text box - so there is no dimension variable. I'm not able to provide sample data for now, but the above expression should be sufficient support. If anyone notices syntax that looks incorrect, please let me know.

I tried:

Max(Aggr(if(Avg(PODate - PODateApproval) < 0, 0, Avg(PODate - PODateApproval)))

and this returns the correct value. Once I add the FirstSortedValue with SupplierNameClean, it returns Null.

MK_QSL
MVP
MVP

Max(Aggr(if(Avg(PODate - PODateApproval) < 0, 0, Avg(PODate - PODateApproval)))

The highlighted part may be something like customername or suppliername or some other field

Not applicable
Author

I figured it out. The expression needed the DISTINCT keyword in case there were multiple suppliers with the same value, as shown below:

=FirstSortedValue(DISTINCT POSupplierNameClean, Aggr(if(Avg(PODate - PODateApproval) < 0, 0, Avg(PODate - PODateApproval)), POSupplierNameClean))