Sub topic 7
Replacing Simple IF Logic with MIN and MAX
This chapter assumes you can build a 1/0 flag with IF, anchor references with $, and copy a formula across a row of periods. It also assumes you know what MIN and MAX return for a list of numbers. Here you will learn when an IF can be replaced by a shorter MIN or MAX, and when it should not be.
The pattern behind the shortcut
Many IF formulas in a model do one thing: they compare a number with a limit and return whichever side wins. Two examples:
=IF(F30>0,F30,0)
=IF(F20*$C$3>$C$4,$C$4,F20*$C$3)The first returns F30 if it is positive and 0 otherwise. The second returns the cap $C$4 if the calculated bonus exceeds it, and the bonus otherwise. In both, the result is always one of the two values being compared. When that holds, MIN or MAX does the same job:
| Intent | IF version | Short version |
|---|---|---|
| Never below a floor | `=IF(x<floor,floor,x)` | `=MAX(x,floor)` |
| Never above a cap | `=IF(x>cap,cap,x)` | `=MIN(x,cap)` |
| Ignore negatives (floor of 0) | `=IF(x>0,x,0)` | `=MAX(0,x)` |
| Keep between a floor and a cap | nested `IF` | `=MIN(cap,MAX(floor,x))` |
The two versions agree when x equals the limit, because both branches then return the same number. There is no edge case at the boundary.
The naming trap: MAX is the floor, MIN is the cap
The function names describe what the function returns, not what it does to the number. They feel backwards at first.
MAX(0,x)returns the larger of 0 andx. The result can never be below 0, so it is a floor.MIN(x,cap)returns the smaller ofxandcap. The result can never be above the cap, so it is a cap.
A quick test: put a very large number in x and a very small number in x, and see which function changes the result. With MAX(0,x), a large x passes through and a small x is lifted to 0. The large side is free, the small side is limited. That is a floor.
Worked example: a capped bonus
Suppose the inputs sit in these cells.
| Cell | Content | Value |
|---|---|---|
| C3 | Bonus rate | 10% |
| C4 | Maximum bonus per period | 8,000 |
| F20 | Base amount (period 1) | 100,000 |
| G20 | Base amount (period 2) | 60,000 |
The bonus is the rate times the base, but never more than the maximum:
=MIN(F20*$C$3,$C$4)For period 1, F20*$C$3 is 10,000, which is larger than 8,000, so MIN returns 8,000. Copied to G20, the calculation gives 6,000, which is below the cap, so 6,000 is returned.
The IF version of this formula calculates F20*$C$3 twice, once in the test and once in the result. If someone later edits one copy and not the other, the two branches silently disagree. MIN states the calculation once, which makes it easier to audit and harder to break.
Notice the anchoring. The rate and cap are inputs, so they use $C$3 and $C$4. The base amount uses a relative column, so it moves as you copy across. Putting the cap in an input cell, not typing 8000 inside the formula, is what keeps the limit visible and changeable.
Worked example: a floor at zero for tax
Tax should be charged only on positive pre-tax income. With the rate in C5 and pre-tax income in row 30:
=MAX(0,F30)*$C$5MAX(0,F30) turns any loss into 0, and multiplying by the rate gives 0 tax. With a 25% rate, three periods behave like this:
| Period | Pre-tax income (row 30) | MAX(0, income) | Tax |
|---|---|---|---|
| 1 | 120 | 120 | 30 |
| 2 | -40 | 0 | 0 |
| 3 | 80 | 80 | 20 |
Without the floor, period 2 would show -10 of tax, which is a refund the company usually cannot claim. This formula treats each period on its own. It does not carry the period 2 loss forward to reduce period 3 tax. If your model needs loss carry-forward, that needs extra rows and is a separate topic.
Combining a floor and a cap
Debt repayment has two limits at once. You cannot repay a negative amount, and you cannot repay more than the opening balance. The cash available is in row 38 and the opening balance is in row 41:
=MIN(F41,MAX(0,F38))Read it from the inside out. MAX(0,F38) removes any negative cash, which means no repayment is possible. MIN(F41, ...) then stops the repayment at the balance. Three cases, with an opening balance of 500:
| Cash available | MAX(0, cash) | Repayment |
|---|---|---|
| 200 | 200 | 200 |
| -50 | 0 | 0 |
| 800 | 800 | 500 |
The general form of this pattern is:
=MIN(cap,MAX(floor,x))The MAX is inside, so the floor is applied first, and the MIN is outside, so the cap has the last word. When the floor is below the cap, which is the normal case, the order does not matter. The two nestings give the same answer.
When the floor is above the cap
If an input error makes the floor larger than the cap, the order starts to matter. Take a floor of 5, a cap of 3 and x of 4:
=MIN(3,MAX(5,4))
=MAX(5,MIN(3,4))The first returns 3 (the cap wins). The second returns 5 (the floor wins). Neither is correct, because no number satisfies both limits. In a repayment formula, the version that lets the cap win is the safer one, since it never repays more than the balance. Choose that order deliberately and use it consistently. If the limits come from inputs a user can edit, consider a check cell that flags when floor is greater than cap.
When to keep the IF
MIN and MAX only work when the result is one of the numbers being compared. Keep IF in these cases:
- The two branches return different things.
=IF(F5>0,F6*$C$3,F7)chooses between unrelated calculations. NoMINorMAXequivalent exists. - The result is text or a flag you want to read. A check that returns "OK" or "Error" needs
IF. - The condition involves a different cell from the value returned. A flag from row 8 that decides whether row 20 counts is a switch, not a limit. Use
=F20*F$8orIF. - The condition is a date or period test. Timing flags compare a period date with a start and end date and return 1 or 0. Those are better as
IFwithAND. - The formula would become harder to read. A short
IFthat colleagues understand is better than a clever nestedMINandMAXthat nobody checks.
A good rule is to replace IF only when the new formula reads more easily than the old one.
Behavior to know about
MIN and MAX handle unusual inputs in ways that differ from IF:
- Empty cells and text in references are ignored. If
F30is blank or contains text,MAX(0,F30)returns 0, not an error. That can hide a missing input. AnIF(F30>0,...)formula treats text as greater than any number, which gives a different and equally surprising result. - Errors pass through. If
F30contains#DIV/0!,MAX(0,F30)returns the error. This is usually what you want, because it makes a broken upstream cell visible instead of hiding it. - All-empty ranges return 0. If every argument in a reference is empty or text, both functions return 0.
- Ranges work too.
=MAX(0,F30:H30)returns the largest value in the range or 0. This is not the same as flooring each cell, so use it only when you mean it.
Checking a replacement
Before you swap an IF for MIN or MAX, test it as follows.
- List three test values for the main input: well below the limit, exactly on the limit, and well above it.
- Calculate the original
IFresult and the new result for each. - Confirm they match, then replace the formula across the whole row.
- Check that the limit comes from an input cell and not a typed number.
Exercise
A bonus pool is 12% of profit in F20, with the rate in C3, and the total must never exceed the limit in C4 and never go below 0 when profit is negative. Write one formula that does this, and say what it returns for profit of -500, 20,000 and 90,000 when the rate is 12% and the limit is 6,000.
Show answer
=MIN($C$4,MAX(0,F20*$C$3)). For -500, the product is -60, MAX gives 0, and MIN returns 0. For 20,000, the product is 2,400, which is below the limit, so it returns 2,400. For 90,000, the product is 10,800, MAX leaves it unchanged, and MIN caps it at 6,000.
Exercise
A colleague writes =MIN(0,F30)*$C$5 to calculate tax on pre-tax income in F30. With F30 = 100 and C5 = 25%, what does it return, and what should the formula be?
Show answer
It returns 0, because MIN(0,100) is 0. For a loss of -40 it would return -10, a negative tax. The function is swapped. The correct formula is =MAX(0,F30)*$C$5, which returns 25 for an income of 100 and 0 for a loss.
Exercise
Which of these can be replaced by MIN or MAX, and which should stay as IF? (a) =IF(F12>$C$6,$C$6,F12) (b) =IF(F5>0,F6*$C$3,F7) (c) =IF(F12<0,0,F12)
Show answer
(a) can be =MIN(F12,$C$6), a cap. (c) can be =MAX(0,F12), a floor. (b) stays as IF, because its branches are two different calculations and neither is one of the compared numbers.