Unable to insert a value based on a calculation using conditions