![]() Some programmers or developers would regard this as a matter of personal style. Sometimes it's not necessary and then it's up to you. So if it's necessary, absolutely, make the default result explicit. Now in this case, I could have left it blank - I could have stopped after the comma at the end of the line for Saturday, closed the parentheses and gone home. Here I've included a default response of null or nothing (the empty quotes). Now we can fix that by making the test for Saturday an explicit part of the switch, rather than the default: SWITCH( Here using a default or none-of-the-above response makes sense.īut what if the field containing the reference date is empty? The formula will return "Hmm, it must be Saturday" - but that's wrong. If I'm absolutely sure that the reference date is there, that there actually is a reference date value (and it's a valid date), then the formula above is okay. In my earlier reply I gave this example of SWITCH(): SWITCH( Should you use that 0, that is, should you specify a default? Well, you definitely should when it matters. The "0" is just the default or "none of the above" response - which is to adjust the calculated date by 0 days. Now to determine what adjustment to make, you have the SWITCH statement, and you seem to understand that now. The first or "outer" DATEADD statement is the one that makes a (potential) adjustment to the inner statement (in case the initially calculated date is a weekend). The second or "inner" DATEADD statement is the main one: it takes your reference date and subtracts 70 days or 56 days. In both parts of the formula (the November/May part, and the default part for the rest of the year) there are two DATEADD() statements, one inside the other. If nothing matches, the weekday must be 6, so it's Saturday. THEN it works its way down the list of possible matches, with the result for each one in case of a match. So the first part of the statement looks at the date in the date field, calculates its weekday value. The WEEKDAY() function returns 0 for Sunday, 6 for Saturday, etc. a 'comparand' = some dynamic value, typically a field.Can't do that with SWITCH().īut when you *can* use SWITCH, it's brilliant. Notice that each of the tests is quite different from the others. It's trying to figure out how you would address somebody. For example, this is a reasonable nested IF statement that could NOT be formatted with SWITCH(): IF( NameLast = "Jones", "Jonesy", SWITCH() is an alternative to nested IF statements. The interesting function in there is SWITCH(). It says if the month is November or May, then add (well, subtract) about 70 days (based on whether resulting date falls on a weekend or a weekday) OTHERWISE, add (well, subtract) 56 days (based on whether the resulting date falls on a weekend or a weekday). And use a proper text editor like Sublime Text or BBEdit so you can make sure your parentheses are balanced.īasically it's an IF() statement. ![]() I find it helpful sometimes to build formulas using several different fields, so I can work on smaller bits. ![]() Look up each of the functions in your formula, understand what it does. I urge you to spend some time with the Airtable Formula Field Reference. And you're right: It makes it a lot easier to see what the heck is going on. To keep formatting in things like code or formulas, enclose the code or the formula in code block formatting.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |