Post

2 followers Follow
0
Avatar

Calculation field - if this then Total, if this then average

I have an new enquiry app that links through with a relationship field to another app called Quoted. In the Quoted app we we can select if a band is an 'Option' or whether it is a 'Pencil' via a category field and add a quoted value.

In the enquiry app I have an 'enquiry value field' I want it to show the value of the following:

If there is a Quoted item linked that is marked as 'Pencil' = The full value of Quoted Value

If there are Quoted items that are marked as 'Option' = The average value of Quoted value of all items marked as 'Option' 

Can anyone help?

Rachel Gretton

Please sign in to leave a comment.

2 comments

0
Avatar

Hi Rachel,

this should work:

var cat = @All of the category field;
var val = @All of Quoted Value;
var sumPencil = 0;
var sumOpt = 0;
var numberOpt = 0;
for(var i = 0; i < cat.length; i++){
    if(cat[i] == "Pencil){
     sumPencil += Number(val[i]) || 0;
    }
    if(cat[i] == "Option"){
    sumOpt += Number(val[i]) || 0;
    numberOpt += 1;
  }
};
var sumOpt = sumOpt > 0 ? (sumOpt/numberOpt) : 0;
var total = sumPencil + sumOpt;
total

Rainer

Rainer Grabowski 0 votes