The first conditional logic function that most people use is the IF function. The IF function has the following format:
IF(<condition> , <value to return if true> , <value to return if false)
Complex conditional logic can be created by nesting IF functions. For example, in the true or false value to return, another IF statement can be inserted. To create our custom quarter calculation, we will actually need three nested IF functions. Create a new column in R09_Table with the following formula:
QuarterNo =
IF('R09_Table'[MonthNo] < 4,
"Q3", // If the month is 1-3 then this is Q3
IF('R09_Table'[MonthNo] < 7,
"Q4", // If the month is 3-6 then this is Q4
IF('R09_Table'[MonthNo] < 10,
"Q1", // If the month is 7-9 then this is Q1
"Q2" // If no other condition is met, then the month is 10-12, Q2
) // End 3rd IF
) // End 2nd IF
) // End 1st IF
IF statements work perfectly fine for conditional logic, but, as you can see, nesting can become somewhat difficult to format and read. A preferred way to do conditional logic is to instead use the SWITCH statement for easier formatting and readability. The SWITCH statement has the following format:
SWITCH(<expression> , <value> , <result> [ , <value> , <result>]...[ , <else>])
An equivalent SWITCH statement can be written by creating a new column with the following formula:
QuarterNo1 =
SWITCH(
'R09_Table'[MonthNo], // Check the [MonthNo] column
1,"Q3", // If the [MonthNo] is 1, then return Q3
2,"Q3", // If the [MonthNo] is 2, then return Q3
3,"Q3", // If the [MonthNo] is 3, then return Q3
4,"Q4", // If the [MonthNo] is 4, then return Q4
5,"Q4", // If the [MonthNo] is 5, then return Q4
6,"Q4", // If the [MonthNo] is 6, then return Q4
7,"Q1", // If the [MonthNo] is 7, then return Q1
8,"Q1", // If the [MonthNo] is 8, then return Q1
9,"Q1", // If the [MonthNo] is 9, then return Q1
"Q2" // Else if none of the other conditions are met, Q2
)