Sub topic 6
Turning the scenario name into a number with MATCH
The dropdown from the previous chapter gives you a text value such as Upside in the selector cell. CHOOSE cannot use that text. It needs a number. This chapter builds the bridge between the two with MATCH, and covers the ways that bridge fails.
This chapter assumes you can write basic formulas and know the difference between $A$1 and A1. It also assumes you have a selector cell with a data validation list, and a block of assumptions with one column per scenario.
Why the model needs an index
CHOOSE picks a value by position:
=CHOOSE(2, "Base", "Upside", "Downside")This returns "Upside", because 2 selects the second value. If you give CHOOSE the text "Upside" as its first argument, it returns #VALUE!. The function has no way to know that "Upside" is the second scenario.
The index is a whole number that stands for the chosen scenario: 1 for Base, 2 for Upside, 3 for Downside. MATCH produces it by looking up the selected name in the list of scenario names and returning its position.
How MATCH works
MATCH takes three arguments:
=MATCH(lookup_value, lookup_array, match_type)lookup_valueis what you are looking for. Here it is the selector cell.lookup_arrayis a single row or single column to search. Here it is the list of scenario names.match_typecontrols how the search behaves. For scenario switches it is always0.
MATCH returns a position, not the value it found. The position is counted from the start of the range you give it, so the first cell of the range is position 1, whatever its row or column number on the sheet. This is why the result works as a CHOOSE index.
The working formula
Assume this layout:
| Cell | Content |
|---|---|
| B5 | Base |
| B6 | Upside |
| B7 | Downside |
| C3 | Selector (dropdown, source `=$B$5:$B$7`) |
| C4 | Scenario index (the formula below) |
The index formula in C4 is:
=MATCH($C$3, $B$5:$B$7, 0)The results are:
| C3 holds | C4 returns |
|---|---|
| Base | 1 |
| Upside | 2 |
| Downside | 3 |
| Anything not in the list | #N/A |
Both references are absolute. The formula sits in one cell and is not copied, but absolute references protect it if someone copies or moves it. They also show a reader that these cells are fixed anchors, not row-by-row inputs.
The list in $B$5:$B$7 is the same range the dropdown uses as its source. That is deliberate. Validation can only produce names that MATCH can find, because both read the same cells. If you typed the names into the validation dialog separately, the two copies could drift apart.
Why match type 0
The third argument decides everything about reliability. There are three values:
| match_type | Behavior | Use for scenarios? |
|---|---|---|
| 0 | Exact match. Returns `#N/A` if nothing matches. | Yes |
| 1 (default) | Finds the largest value less than or equal to the lookup value. Assumes the list is sorted ascending. | No |
| -1 | Finds the smallest value greater than or equal to the lookup value. Assumes the list is sorted descending. | No |
The default is the dangerous one. If you write =MATCH($C$3, $B$5:$B$7) and omit the third argument, Excel uses 1. The list Base, Upside, Downside is not in alphabetical order, so the search assumption is broken. The formula may return a wrong position, or #N/A, and it may do so only for some selections. A wrong scenario with no error is the worst outcome in a model, because nobody notices it.
Always type the 0. Do not leave the argument out, and do not use a trailing comma with nothing after it.
What exact matching does and does not ignore
Exact matching with 0 has some behaviors that matter for names.
- Case is ignored.
upside,UPSIDEandUpsideall return 2. This is helpful for a typed selector, but do not rely on case to tell two scenarios apart. - Spaces are not ignored.
"Base "with a trailing space returns#N/A. A dropdown prevents this when entries come from the list. Pasted values do not go through the dropdown, so they can still introduce the problem. - Wildcards work in text lookups. In a match type 0 search,
?matches any single character and*matches any run of characters. A selector value ofUp*would return 2. The~character escapes a wildcard. Wildcards matter little when the dropdown restricts entries, but they explain odd results if someone pastes text into the selector cell. - Names that look like numbers can mismatch. The text
"1"does not equal the number 1. Keep scenario names as plain words.
Following the lookup
The diagram shows what happens from the dropdown to the live value.
The names list feeds both the dropdown and MATCH. The index feeds every CHOOSE formula in the live case row.
Connecting the index to CHOOSE
With the index in C4, the live case formula for revenue growth reads:
=CHOOSE($C$4, E12, F12, G12)If C3 is Upside, MATCH returns 2, and CHOOSE returns the value in F12. The model's calculations then read this live cell, not the columns E, F or G directly.
For the arguments to line up, three orders must be identical:
- The order of names in
B5:B7. - The order of the scenario columns, E, F, G.
- The order of values inside every
CHOOSEformula.
Base is first in all three, Upside second, Downside third. If you move a column or sort the names list, the index still returns a valid number, but it now points at the wrong column. Nothing shows an error. You will not see the problem unless you test for it.
Anchoring the index to the column headers
One way to remove the first mismatch is to match against the column headers of the assumptions block, not a separate list. Suppose E11:G11 contain Base, Upside and Downside, the headers above the values in row 12. Then:
=MATCH($C$3, $E$11:$G$11, 0)MATCH accepts a single row as well as a single column, so this works the same way. The index now follows the real column order. If someone swaps two columns, the headers move with the values, and the position stays correct for the CHOOSE formulas that reference those columns by position within the live row.
This only helps if the CHOOSE arguments still point at those columns left to right. The dropdown source should then also point at $E$11:$G$11. A data validation list accepts a single row, so one source can serve both. Many modelers prefer a separate vertical list because it is easy to see, and accept the alignment risk. Choose one approach and use it across the whole model.
Handling errors
When the selector value does not match, MATCH returns #N/A, and that error flows into every live case formula and then into the outputs. It is ugly, but it is honest: the model is telling you the switch is broken.
You may be tempted to wrap the formula:
=IFERROR(MATCH($C$3, $B$5:$B$7, 0), 1)This returns 1 when anything goes wrong, so the model falls back to Base. The cost is that a broken selector no longer looks broken. Someone could be reading Base numbers while believing they are viewing Downside. For a model that drives decisions, a visible error is safer than a quiet default.
If you want to handle the error, handle it visibly. One option is to leave #N/A flowing and add a check cell (covered in a later chapter) that flags an invalid index. The check from the plan returns 0 for a valid index and 1 otherwise:
=IF(AND(ISNUMBER($C$4), $C$4>=1, $C$4<=3), 0, 1)ISNUMBER is evaluated first, so an #N/A in C4 is reported as a failed check without raising a second error.
Testing the lookup
Before wiring the index into the rest of the model, test it on its own.
- Select each name from the dropdown in turn. C4 should show 1, 2, 3.
- Type a wrong name into C3 by temporarily turning off the validation error alert, or by pasting a value. C4 should show
#N/A. Turn the alert back on afterward. - Select the formula in C4 and use Formulas > Evaluate Formula. You can step through and see
$C$3replaced by"Upside"and then the result 2. - Change a scenario name in
B5:B7. The dropdown list and the match both change together, but the selector keeps its old text until you reselect. Expect an#N/Auntil you do. Renaming a scenario therefore means updating the selector too.
Step 4 is a real maintenance trap. If you rename Downside to Stress, a selector still holding Downside stops matching, and the model shows errors. This is the right behavior, because the model refuses to guess.
Common mistakes
- Relative references in the lookup range.
=MATCH($C$3, B5:B7, 0)works in place, but copying it down shifts the range and breaks it. Anchor the range with$. - Searching a two-dimensional range.
MATCHneeds a single row or a single column. A range likeB5:C7returns#N/A. - A selector that is not the validated cell. Point
MATCHat the cell with the dropdown, not a display copy of it. - Hardcoding the index. Typing 2 into C4 leaves the dropdown looking connected when it is not. Keep the index as a formula.
- Different spellings.
"Up-side"in the list and"Upside"in a heading cause a mismatch. Use one cell as the source of each name and reference it.
Where this fits
The scenario selector holds a name, MATCH converts it to an index, and every CHOOSE formula in the live case row uses that index. The next chapter builds that row. Later, you will give the selector and index readable names, so formulas read CHOOSE(ScenarioIndex, ...) in place of CHOOSE($C$4, ...).
Exercise
A modeler writes =MATCH($C$3, $B$5:$B$7) with no third argument, where B5:B7 holds Base, Upside, Downside. The model appears to work when Base is selected. Explain why this formula is unsafe, and write the corrected formula. Then say what the corrected formula returns if C3 holds "Downside " with a trailing space.
Show answer
Omitting the third argument sets match type 1, which does an approximate match and assumes the list is sorted in ascending order. Base, Upside, Downside is not alphabetical, so the formula can return a wrong position or #N/A for some selections, and it may look fine for Base. The corrected formula is =MATCH($C$3, $B$5:$B$7, 0). With "Downside " (trailing space), exact matching finds no equal entry, so the formula returns #N/A. Spaces are not ignored, which is why entries should come from the dropdown list.
Exercise
The scenario columns are E (Base), F (Upside) and G (Downside), and the names list is B5:B7 in the same order. Someone sorts B5:B7 alphabetically, giving Base, Downside, Upside. The selector is C3 and the live formula is =CHOOSE($C$4, E12, F12, G12). With "Downside" selected, which column does the model read, and why is there no error? Suggest one change that prevents this.
Show answer
After sorting, MATCH returns 2 for Downside, so CHOOSE returns F12, the Upside value. No error appears because 2 is a valid index and F12 is a valid cell. The names list and the column order no longer agree. To prevent it, match against the column headers instead, for example =MATCH($C$3, $E$11:$G$11, 0), so the index follows the real column order, or add a check that compares the selected name with the header above the column picked by CHOOSE.