14 Replies Latest reply: Jan 11, 2014 4:00 AM by Manish Kachhia

# FirstSortedValue

Dimension

PartNumber

Expression

FirstSortedValue(Price,-InvoiceDate)

1) Giving me '-' whenever same part number is sold more than once on the same date.

2) What if same part number is sold more than once with different price

My Idea is to get Minimum Price for each part number sold on the latest Date...

• ###### Re: FirstSortedValue

Try

FirstSortedValue(Aggr(Min(Price),PartNumber,InvoiceDate),-InvoiceDate)

HIC

• ###### Re: Re: FirstSortedValue

Hi Henric,

Its not working...

• ###### Re: Re: Re: FirstSortedValue

You can do it using a cross-table and the Above() function.

LastPrice = If(IsNull(Min(Price)),Above(LastPrice),Min(Price))

• ###### Re: FirstSortedValue

Dear Henric,

Thanks for your valuable time but this will not work for me...

I have around 80,000 part numbers...

The preferable solution should be as per below table.

 PartNumber MinPrice MaxInvoiceDate A 105 08/01/2014 B 110 04/01/2014 C 130 10/01/2014 D 110 07/01/2014
• ###### Re: FirstSortedValue

Your help would be highly appreciated...

• ###### Re: FirstSortedValue

This might not be the most elegant, but you could sort using the concat function. Try this out:

subfield(concat(aggr(min(Price), PartNumber, InvoiceDate), '|', -InvoiceDate), '|', 1)

Hope this helps!

• ###### Re: FirstSortedValue

Tricky and working but still would love to get another way.

• ###### Re: FirstSortedValue

Hi,

Here is my suggestion:

=Min(if(InvoiceDate=aggr(NODISTINCT Max(InvoiceDate), PartNumber),  Price))

• ###### Re: FirstSortedValue

Hi.

This will give you the result:

=FirstSortedValue(Aggr(Min(Price),PartNumber,InvoiceDate),-aggr(InvoiceDate, PartNumber, InvoiceDate))

• ###### Re: Re: FirstSortedValue

=FirstSortedValue(DISTINCT Aggr(Min(Price),PartNumber,InvoiceDate),-InvoiceDate)

-Rob

• ###### Re: FirstSortedValue

It works, but I don't understand why

• ###### Re: FirstSortedValue

So after a very hard and tired day, I got some useful solutions.

Jerem

SubField(CONCAT(Aggr(MIN(Price),PartNumber,InvoiceDate),'|',-InvoiceDate),'|',1)

B Aydin

Min(If(InvoiceDate = Aggr(NODISTINCT Max(InvoiceDate), PartNumber), Price))

Would appreciate if you explain to understand the use of NODISTINCT in this expression...

Rob

FirstSortedValue(DISTINCT Aggr(Min(Price),PartNumber,InvoiceDate),-InvoiceDate)

Hi Rob, What is the reason of getting syntex error (red lines) in expression?

Whiteline

FirstSortedValue(Aggr(Min(Price),PartNumber,InvoiceDate),-Aggr(InvoiceDate, PartNumber, InvoiceDate))

Can anyone try to explain how above expression is working? Want to understand the magic behind two InvoiceDate in single Aggr.

• ###### Re: FirstSortedValue

"... Hi Rob, What is the reason of getting syntax error (red lines) in expression?"

DISTINCT appeared in the doc some time back (I think an early QV10 SR) but the syntax checker was never updated. (or may have worked for a while -- can't recall). The red squiggle is probably why most people don't know about DISTINCT in FirstSortedValue . Welcome to the club.

-Rob

• ###### Re: FirstSortedValue

Thanks ROB....