Sub topic 3
Building a Flag with IF
This chapter assumes you can write formulas with relative and absolute references, copy a formula across a row of period columns, and read a simple forecast layout. It also assumes you know basic terms such as revenue and pre-tax income. The earlier chapters in this tutorial introduced flags and comparisons. Here you build flags with the IF function, learn why it is the standard tool for them, and see where flag formulas usually go wrong.
The layout used in this chapter
The examples use one layout throughout:
- Inputs sit in column C. Cell C2 is the scenario switch, C10 is a start date, C11 is an end date, and C5 is the tax rate.
- The timeline runs across columns F onward. Row 5 holds the period end date for each column.
- Row 8 holds a timing flag. Row 20 holds a line item that the flag controls.
Every formula in a row should be identical across all period columns. The only things that change from column to column are the relative references, which are the ones without a $ in front of the column letter.
How IF works
IF takes three arguments:
=IF(logical_test, value_if_true, value_if_false)Excel evaluates the test first. If the test is true, IF returns the second argument. If it is false, IF returns the third. Only the chosen branch is returned, so the other one has no effect on the result.
The simplest flag tests one condition and returns 1 or 0:
=IF(F$5>=$C$10,1,0)This returns 1 in every period whose end date is on or after the start date in C10. It returns 0 in earlier periods. The result is a flag: a cell whose only job is to say "yes" or "no" in a form that other formulas can use.
The diagram shows the decision that each period column makes.
Why return 1 and 0 instead of TRUE and FALSE
A comparison such as F$5>=$C$10 already returns TRUE or FALSE, so you may wonder why the formula wraps it in IF. There are three reasons.
- Arithmetic works on 1 and 0. Multiplying an amount by a flag keeps or removes it. Multiplying by TRUE also works in Excel, but some tools and some functions, such as
SUMover a range of logical values, treat TRUE and FALSE as text or ignore them. - The intent is visible. Anyone reading
IF(...,1,0)knows the cell is a flag. A bare comparison could be a flag or a check. - Flags can be summed and checked. A row of 1s and 0s can be added up to count active periods, which is a useful audit test.
Shorter forms exist, such as =--(F$5>=$C$10) or =(F$5>=$C$10)*1. They give the same result. Prefer the IF form in shared models, because the double minus sign puzzles many readers.
A flag for a window of time
Most flags need a start and an end. Combine the two tests with AND, which returns TRUE only when every argument is true:
=IF(AND(F$5>=$C$10,F$5<=$C$11),1,0)This formula returns 1 when the period date falls between the start date in C10 and the end date in C11, and 0 otherwise. Put it in row 8, then copy it across the timeline. Here is a worked example. The start date is 1 January 2028, the end date is 31 December 2029, and the line item in row 20 is 100 in every period.
| Period end date | Start test (>= C10) | End test (<= C11) | Flag in row 8 | Row 20 amount | Included amount |
|---|---|---|---|---|---|
| 31 Dec 2026 | FALSE | TRUE | 0 | 100 | 0 |
| 31 Dec 2027 | FALSE | TRUE | 0 | 100 | 0 |
| 31 Dec 2028 | TRUE | TRUE | 1 | 100 | 100 |
| 31 Dec 2029 | TRUE | TRUE | 1 | 100 | 100 |
| 31 Dec 2030 | TRUE | FALSE | 0 | 100 | 0 |
Look at the 2029 row. The period date equals the end date, and the test uses <=, so the period is included. If you had written <, the final period would drop out. Choosing between < and <= is a decision about whether the boundary date counts. Make it on purpose and document it next to the input cells.
Using the flag in a line item
The flag does its work when another formula multiplies by it:
=F20*F$8This formula takes the amount in row 20 and keeps it only in periods where the flag in row 8 is 1. The $ before the 8 locks the row, so the formula always points at the flag row even if you copy it down to other line items. The column stays relative, so each period picks up its own flag.
This is the main benefit of a flag. The timing logic lives in one place, row 8. Fifty line items can multiply by that row. To change when the item starts, you edit one date in C10, and every formula follows.
The alternative is to bury the test inside each line item, such as =IF(AND(F$5>=$C$10,F$5<=$C$11),F20,0). That works, but the date test is now repeated in every row. When the logic needs to change, you must edit and re-check each copy.
Reference anchoring in flag formulas
Flag formulas fail most often because of the way references are anchored. Compare the four forms of the start-date reference:
| Reference | Behavior when copied across | Typical use |
|---|---|---|
| `C10` | Shifts to D10, E10, and so on | Almost never right for an input |
| `$C$10` | Never moves | Inputs in column C |
| `F$5` | Column moves, row stays at 5 | Period dates in a header row |
| `$F5` | Column stays at F, row moves | Column of labels copied down |
In the flag formula, F$5 is mixed so each period reads its own date, and $C$10 is fully anchored so every period reads the same input. If you forget the $ on C10, the formula in column G tests against D10, which is probably empty. An empty cell counts as 0, which Excel treats as the date 0 January 1900, so the start test returns TRUE in every period. The flag is then wrong everywhere except the first column, and no error appears.
Flags driven by a switch cell
A flag does not have to compare dates. It can also read a control cell. If C2 holds the scenario number and the downside case is number 3, a flag for downside-only logic is:
=IF($C$2=3,1,0)You can also use IF to choose between values directly, for example =IF($C$2=1,F10,IF($C$2=2,F11,F12)). This picks the base, upside, or downside value from rows 10 to 12. It works, but it nests, and each additional scenario adds another level. The CHOOSE function handles this more cleanly, and a later chapter covers it. Use IF when there are two outcomes and CHOOSE when there are three or more.
Common errors
- Dates stored as text. If a date was pasted in as text,
F$5>=$C$10compares text with a number and gives the wrong answer without warning. Test the cells withISNUMBER. A real date returns TRUE. - Hard-coded values inside the formula.
=IF(F$5>=DATE(2028,1,1),1,0)hides an assumption in the formula. Put the date in an input cell and reference it. - Returning text instead of numbers.
=IF(...,"Yes","No")cannot be multiplied. A flag should return 1 or 0. - Returning an empty string.
=IF(...,1,"")looks tidy but produces text in the cells where the test fails. Any formula that multiplies by that cell returns a#VALUE!error. - Comparing for exact equality on calculated numbers. A test like
IF(F30=0,1,0)can fail when F30 holds a tiny rounding residue such as 0.0000000001. UseROUNDon the calculated value or compare with a tolerance. - Nesting too deeply. Beyond two levels,
IFformulas become hard to audit. Split the logic into separate flag rows and combine them. - Using a number as the test.
IFtreats any non-zero number as TRUE and 0 as FALSE.=IF(F$8,...)therefore works, but it is unclear. Write the comparison, such asF$8=1.
Choosing between one flag and several
Suppose a bonus applies only after a start date and before an end date, and only in the upside scenario. You could put all three tests in one AND. Alternatively, build a timing flag in row 8, a scenario flag in row 9, and multiply them. A single formula is compact. Separate rows let you see which condition is switching an item off. Use separate rows once a formula has more than two conditions, or whenever other line items need to reuse one of the conditions. The next chapters cover timing flags and combining flags in more detail.
Checking your flag
After building a flag, run three quick tests:
- Scan the row. Do the 1s start and stop in the periods you expect?
- Change the start and end dates in the inputs. Does the pattern move accordingly?
- Set the end date earlier than the start date. The flag should be 0 in every period. If any 1 appears, the logic is wrong.
You can also add a total of the flag row beside it. The total should match the number of active periods you expect from the dates.
Exercise
A flag in F8 uses =IF(AND(F$5>=C10,F$5<=C11),1,0) and is copied across to column J. The flag is 1 in column F and 0 in every other column, even though the dates in row 5 should place columns H and I inside the window. What is the likely cause, and what is the fix?
Show answer
The references to C10 and C11 are not anchored. When the formula is copied to column G, they shift to D10 and D11, which are empty or hold other data. Change them to $C$10 and $C$11 so every column tests against the same two input cells.
Exercise
Write a formula for row 9 that returns 1 only when the scenario number in C2 equals 2, and then write the formula that applies this flag to a line item in row 22 in the same column. Explain why you would use a flag row instead of putting the test inside every line item.
Show answer
Row 9: =IF($C$2=2,1,0). Line item: =F22*F$9. A flag row puts the condition in one place, so a change to the logic needs one edit instead of many. It also makes the active periods visible, and it keeps the line item formulas short and consistent across the row.