SWITCH

Short answer

Map a short list of codes or cases without nested IFs

SWITCH(expression, value1, result1, [value2, result2], ..., [default])
Excel version
2019+
Also searched as
case statement, replace nested if, map codes to labels, scenario name selector, switch true, tiered rate

What it returns

SWITCH compares one expression against a list of values and returns the result paired with the first value that matches. If nothing matches, it returns the default you supply, or #N/A if you supplied none.

=SWITCH(B5, "B", "Base case", "U", "Upside case", "D", "Downside case", "Unknown case")

The expression, B5 here, is evaluated once. That is the practical difference from nested IFs, which repeat the test in every branch, and it is why SWITCH reads cleanly when the expression is itself a calculation.

Matching is exact equality. SWITCH cannot test “greater than” directly. The SWITCH(TRUE, …) pattern in the worked example works around that.

It is the wrong tool for anything that is really a mapping table. Cost center to department, account to statement line, entity to currency: these lists change, and a change to a SWITCH formula is a change to code in every cell that holds it.

Arguments that matter

ArgumentRequiredWhat it does
expressionYesThe value to test. A cell, or a calculation such as MONTH(A2). Evaluated once.
value1, result1YesThe first value to compare and the result to return if it matches.
further pairsNoUp to 126 value and result pairs in total.
defaultNoThe final unpaired argument. Returned when nothing matches. Without it, no match returns #N/A.

Two rules decide most outcomes:

  • The first match wins. Later pairs are not checked.
  • Types must match. The number 4000 does not equal the text “4000”, so a code column imported as text falls through to the default.

In a financial model

SWITCH is for short, fixed lists that are part of the model’s logic rather than its data:

  • Turning a scenario code or number into a printable case name.
  • Mapping a period type (“M”, “Q”, “Y”) to the number of months it spans.
  • Translating a status flag into a label on an output page.

Once a list is longer than about eight items, or is something the business owns and updates, it belongs in a table. One mapping table per dimension is the rule, and XLOOKUP against that table replaces the SWITCH. The table can be reviewed, sorted and extended without touching a formula.

SWITCH earns its place against nested IFs, which three levels of nesting is the limit caps for good reason. A four-way case written as nested IF is three levels deep. The same logic in SWITCH is flat.

Worked example

Months per period type, for a model that supports monthly, quarterly and annual columns. The period type sits in row 6:

=SWITCH(D$6, "M", 1, "Q", 3, "Y", 12)

No default is supplied on purpose. A typo in row 6 returns #N/A, which the check row counts, instead of quietly treating the column as monthly.

Scenario name for an output header, from a numeric selector in Inputs!$C$3:

=SWITCH(Inputs!$C$3, 1, "Base case", 2, "Upside case", 3, "Downside case", "Check scenario selector")

Here a default is useful, because the header should say plainly that something is wrong.

A tiered rate with SWITCH(TRUE, …). Each value is a test that returns TRUE or FALSE, and the first TRUE wins. With quarterly sales in C8:

=SWITCH(TRUE, C8>=1000000, 5%, C8>=500000, 3%, 1%)
Quarterly salesRate returned
$1,250,0005%
$750,0003%
$200,0001%

The tests must run from the highest threshold down. Put the 500,000 test first and every sale above it, including $1.25 million, gets 3%.

This rate applies to the whole amount. Commission plans that pay each rate only on the slice of sales inside its band need a marginal calculation, usually SUMPRODUCT across a tier table.

The thresholds and rates in that formula are constants. In a model they belong in a small tier table on the inputs tab, per no constants inside formulas, which is the strongest argument for using XLOOKUP’s approximate match on that table instead.

Old way vs new way

The old four-way case was nested IFs, repeating the test each time:

=IF(D$6="M", 1, IF(D$6="Q", 3, IF(D$6="Y", 12, NA())))

SWITCH states the test once and lists the cases:

=SWITCH(D$6, "M", 1, "Q", 3, "Y", 12)

Where it breaks

#NAME? when a colleague opens the file. SWITCH arrived in Excel 2019. In Excel 2016 and earlier the formula shows _xlfn.SWITCH and returns #NAME?. Fix: confirm every recipient’s version before using it in a shared model, or use CHOOSE, which works everywhere, when the cases are numbered.

Valid codes fall through to the default. The codes are numbers in the formula and text in the data, or the reverse, often after a CSV import. Fix: settle the type when the data is loaded.

#N/A with no obvious cause. No value matched and there is no default. Sometimes that is the intent. When it is not, the cause is usually a trailing space in the data. Fix: clean the source column, and decide deliberately whether a missing match should error or default.

A tier returns the wrong rate. The SWITCH(TRUE, …) tests are in ascending order, so a lower threshold catches amounts meant for a higher one. Fix: order the tests from highest to lowest and test each boundary value.

The formula has become a mapping table. Twenty pairs of account codes and statement lines in one cell, copied down a column. Fix: move the pairs into a table and replace SWITCH with XLOOKUP.

Related functions

  • CHOOSE. Picks by position rather than by value. Works in every Excel version.
  • XLOOKUP. The replacement once a SWITCH list becomes data. Approximate match also handles tiers.
  • LET. Names a calculated expression before SWITCH tests it, when the same result is needed elsewhere in the formula.
  • IFERROR. Not a substitute for a default. A deliberate default says what no-match means; IFERROR hides every error.
  • The play one mapping table per dimension is where long SWITCH lists should go.

All functions