If Statements in Formulas

Overview

When you want a variable to have different values or formulas based on a condition, you can use if statements. They always have to follow the structure if condition then X else Y.

This article contains the following sections:

Options in IF Statements

You can link multiple conditions with and and/or or:

  • if condition1 or condition2 then value1 else value2
  • if condition1 and condition2 then value1 else value2

 

You can also link conditions with a list using the syntax IN, for example:

  • if month in [11, 12, 1, 2] then 90% else 100%:  means if the month is Nov, Dec, Jan or Feb, return 90% else return 100%.
  • You can also use dimensions/dimension items in if-statements, for more information, see Dimension Items in Formulas.

 

You can also link conditions with the exclusion of a list using the syntax NOT IN, for example:

  • if month not in [11, 12, 1, 2] then 90% else 100%: means if the month isn't Nov, Dec, Jan or Feb, return 90%, if it is return 100%.

 

You can have multiple layers of if statements too, for example:

  • if condition1 then value 1 else if condition2 then value2 else [...]

 

  • You can break each of these layers out onto separate lines of your formula editor using Shift + Enter
  • Note that every if statement must have an else, otherwise xP&A will return an error.
Examples of IF Statements
  • if timestep > 4 then 1 else 0
  • if variable1 > variable2 then 100 else 200
  • if timestep > 4 and timestep < 10 then 1 else 0
  • if variable1 < 10 then 100 else if variable1 < 20 then 200 else 300