An alternative to IF statements using SWITCH()
  • 04 Oct 2022
  • 1 Minute to read
  • Dark
    Light

An alternative to IF statements using SWITCH()

  • Dark
    Light

Article Summary

SWITCH() is one of Airtable’s most recent additions to the formula field, and it can be a big time-saver when you’re working with conditional-heavy formulas (e.g. nested IF statements).

360056096094ScreenShot2020-01-27at40249PM.jpg

To demonstrate the difference between using SWITCH() and IF(), we’ll use the example of a formula that tells us the phase a project is in based on how close the due date is. The criteria we’ll use are:

  • 4 weeks out from due date = planning phase
  • 3 weeks out from due date = execution phase
  • 2 weeks out from due date = loose ends and review phase
  • 1 week out from due date = launch phase
  • None of the above = out of range

For the sake of simplicity, let’s assume that we already have a field called “weeks until deadline” that provides us with the number of weeks until the project’s deadline.

Nested IF() statement approach
Using the formula below results in a different project phase depending on the {Weeks Until Deadline} field.
360056096254ScreenShot2020-01-27at40702PM.jpg

IF(
  {Weeks Until Deadline} = 4, 'Planning',
    IF(
      {Weeks Until Deadline} = 3, 'Execution',
      IF(
        {Weeks Until Deadline} = 2, 'Loose ends and review',
          IF(
            {Weeks Until Deadline} = 1, 'Launch', 'Out of range'
          )
      )
   )
) 

TIP

When writing long formulas, it can sometimes be helpful to visually break up functions using tabbing and line breaks. Learn how to do this and more in this article.

SWITCH() function approach
Similarly, the SWITCH() statement below produces the same result, but with a much cleaner formula. Notice that {Weeks Until Deadline} is only referenced a single time (as opposed to four times).

360056096254ScreenShot2020-01-27at40702PM1.jpg

SWITCH(
 {Weeks Until Deadline},
   4, 'Planning',
   3, 'Execution',
   2, 'Loose ends and review',
   1, 'Launch',
     'Out of range'
)

Using SWITCH in this instance removes the need for nesting, which is bound to cause headaches (especially when working with more complex formulas than the examples here).


Was this article helpful?