How to make calculation only when both conditions are true?