Adding values of same units

Dear SS Team,

I have a variable for Total Consumed (quantity and unit) and I need to make a validation to check that the amount received as gift, purchased and home produced should be equal the Total Consumed. My problem is that all units should be the same in all variables and some variables of gift, purchased and home produced can be skipped if they don’t have.

this is my code:
!(self==h1503c && self==h1503f && self==h1503k && self==h1503n &&
((h1503q + h1503m + h1503j + h1503e) > (h1503b)))

where the first line check that the units are the same and the second line sums the variables to equal the total consumed (h1503b).

This works when all variables have a value but does not work when one or more variable is skipped.

Grateful for your help.

Best
TR

Toga, you can use the function IsAnswered() to check if a certain question has been answered in combination with the ? operator to decide what to do.

IsAnswered(x_value)?x_value:0

But I don’t understand how your check will work if the units are different. A proper check should normalize all amounts to the same units, then compare the values.

Best, Sergiy

1 Like

Thanks Sergiy … I will try this.

Best,
TR

What happen if the units aren’t be the same for all variables?

I only do validations for the same units - No validation checks for the different units.

Hi Sergiy,

It is me again. I tried your suggestion and it worked for the quantity (values). I cannot get the solution to ensure the units (single select) are all the same e.g kgs for cash, home produced, gift. My problem is one of these is not enabled and how do you refer to this not enabled variable. An example would be much appreciated. Thanks heaps.

toga

(IsAnswered(x_value) && IsAnswered(x_units))?x_value:0 +
(IsAnswered(y_value) && IsAnswered(y_units))?y_value:0 + 
....

Thanks much Sergiy … much appreciated.
Your response in my queries helped me to find another way to fix my problem i.e creating new variables to record the unit of the Total Consumed as the default unit if it is null - this enabled me to check totals of the same units.
Thanks again,
Best,
toga