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

Display value that is nearest another value

Hi all,

I have a straight table with dimension = Date, and expression = Price1, Price2. The problem is, sometimes there are multiple values of Price2 for a single date. How can I display the value of Price2 that is nearest to Price1 in the expression?

The formula I'm using to get the minimum difference between Price1 and Price2 = min(fabs(Price1-Price2))

5 Replies
Anil_Babu_Samineni

Instead of fabs you can try Interval

Min(Interval(Price1 - Price2))

Can you provide sample and expected output with Inline Data

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
sifatnabil
Specialist
Specialist
Author

Hi loveisfail‌, that returns the minimum difference, however, how can I display the Price1 and Price2 associated with that minimum difference? Here's some sample data:

I need to display the value of Price1 that corresponds to the minimum difference between Price1 and Price2 (I've highlighted in bold the price that should be displayed):

    

ProductDatePrice1Price2
rws01/06/201664.87;66.2565.115
rws02/06/201664.87;69.5068.54357
rws03/06/201664.87;69.5068.54357
rws06/06/201664.87;69.5068.31633
rws07/06/201664.87;70.5069.615
rws08/06/201664.87;71.5072.115
rws09/06/201664.87;74.5072.115
rws10/06/201664.87;73.5074.32333
rws13/06/201664.87;73.5074.115
rws14/06/201664.87;72.5074.14967
rws15/06/201664.87;74.0071.615
rws16/06/201664.87;74.0073.615
rws17/06/201664.87;72.5073.615
sunny_talwar

How about this:

FirstSortedValue(Price1, Aggr(fabs(Price1-Price2), Date, Price1))

sunny_talwar

Or this:

FirstSortedValue(Aggr(Price1, Date, Price1), Aggr(fabs(Price1-Price2), Date, Price1))

sasikanth
Master
Master

Try this one too

Sum(If( Aggr( rank(-fabs(Price1-Price2)),Date,Price1)<=1, Price2))