0

Find the minimum value in a group of records

Hello Ninox users!

I’m completely addicted to Ninox but still young in scripting… so…

Does anybody knows how is scripted the formula used to find the min/max/… of records grouped in a category (see picture).

To be more accurate, I would like to find my best engine’s seller (meaning the lowest price) for each engine grouped by power.

Something like:

If ‘your price’ = min (price of a given power category)  then « your the winner » end

Any ideas?

Many thanks and love!🙂

5 replies

null
    • Fred
    • 9 mths ago
    • Reported - view

    On your Dashboard table you can try this in a new view element:

    let a := (select Options);
    let x := unique(a.Puissance);
    for loop1 in x do
            first(a[Puissance = loop1] order by Couts)
        end;
    

    Of course verify that I spelled the names of table names and fields correctly.

    Line 1: puts all of the records in the Options table into a variable.

    Line 2: creates an unique array of the data in the Puissance field.

    Lines 3 - 5: uses a for loop to iterate through the array created in Line 2 where we first find all records that match each instance from Puissance. Then we order all found records by Couts by smallest to largest. Then we get the first instance, which would be the smallest.

    You should see a list of records from each Puissance that has the lowest Couts in each group.

      • frsalabert.1
      • 9 mths ago
      • Reported - view

       Thanks Fred! It works very well ! 

      Is there a way to do the same in my table ‘options’, in the formula column « ranking », and display a smile if the seller is equal to the minimum price in a dedicated power category, see below the idea:

    • Fred
    • 9 mths ago
    • Reported - view
     said:
    Is there a way to do the same in my table ‘options’, in the formula column « ranking », and display a smile if the seller is equal to the minimum price in a dedicated power category, see below the idea:

    You can try:

    let t := this;
    let similarRecs := ((select Options where Puissance = t.Puissance) order by Couts);
    if first(similarRecs) = t then
        "😃"
    else
        "😢"
    end
    

    Line 1: get the current record Id.

    Line 2: finds all records that have the same Puissance then orders is in ascending value of Couts

    Lines 3 - 7: checks if the first record Id in the ordered group of related records is equal to the current record Id. If that is true then it prints a smiley emoji. If false it prints a sad emoji.

      • frsalabert.1
      • 9 mths ago
      • Reported - view

       That’s great! It just work fine. Exactelly what I was looking for! Many thanks again! 

    • Alain_Fontaine
    • 9 mths ago
    • Reported - view

    The proposed solution works perfectly if there is one winner only. If, by chance, two (or more) suppliers quote the same best price, only one of them will be flagged - the one that happens the get in the first place when sorting by "Couts". To be sure to flag all the winners, change the final test to:

    if first(similarRecs).Couts = Couts then
    

Content aside

  • Status Answered
  • 9 mths agoLast active
  • 5Replies
  • 80Views
  • 3 Following