Logical Functions and Variables
Meera wants every branch, product and day labelled Strong, Steady or Weak by revenue. Arjun's first draft nests one IF inside another and calls [Total Revenue] three times. It works, but it is hard to read and does the same sum over and over. SWITCH and variables clean up both problems.
The rule
Name a value once with VAR, then test it; the first true test wins.
Logical functions:
IF ( condition, value_if_true, value_if_false ). Leave out the third argument and a false condition returns BLANK.AND ( a, b ),OR ( a, b )andNOT ( a ). AND and OR take exactly two arguments; for more, use the&&operator.SWITCH ( TRUE (), test1, result1, test2, result2, …, else_result )checks the tests in order and returns the result of the first true one.COALESCE ( [Total Revenue], 0 )returns the first value that is not blank, which is handy for showing 0 instead of an empty cell.
Variables: VAR Name = expression stores a value, and RETURN gives the final result. A variable is calculated once, where it is defined, and can then be used as often as you like. That makes formulas shorter, easier to read and often faster. To debug, temporarily RETURN a single variable and look at its value.
Revenue Status =
VAR Rev = [Total Revenue]
RETURN
SWITCH (
TRUE (),
Rev >= 300, "Strong",
Rev >= 200, "Steady",
"Weak"
)| Row in a visual | Total Revenue | Revenue Status |
|---|---|---|
| Pune | 240 | Steady |
| Mumbai | 360 | Strong |
| Masala Chai | 180 | Weak |
| Green Tea | 280 | Steady |
| Samosa | 140 | Weak |
| 5 January | 100 | Weak |
| 6 January | 200 | Steady |
| 7 January | 300 | Strong |
Look closely at 6 and 7 January: >= includes the boundary, so exactly 200 is Steady and exactly 300 is Strong.