Jun 11, 2019 08:57 AM
Could someone help me understand why this formula throws an error?
SWITCH(1=1,TRUE,“Y”,FALSE,“N”)
Thanks.
Jun 11, 2019 11:42 AM
TRUE
and FALSE
aren’t possible outcomes of the expression you are trying to evaluation (1=1
). That’s why your formula there is failing.
If you put just 1=1
as a formula into a formula field, you’ll get a return value of 1
, which is the numeric equivalent of true
.
If you put just 1=2
as a formula into a formula field, you’ll get a return value of 0
, which is the numeric equivalent of false
.
However, Airtable’s SWITCH()
function appears to not even be able to evaluate this correctly either:
SWITCH(
1 = 1,
1, "Y",
0, "N"
)
Which is odd… but, a SWITCH()
statement is really not the best option for a binary/boolean comparison anyhow – it’s a bit overkill, since it’s meant to handle more than just either/or outcomes for an expression.
You can get the outcome you want with a simple IF()
statement:
IF(
1 = 1,
"Y",
"N"
)
Jun 11, 2019 11:53 AM
Thank you, I did have a more complex use in mind.
It does seem though, that SWITCH does not accept logical conditions in this manner.
Apr 06, 2023 06:11 AM
A very common coding pattern is to pass a "true" to switch and test multiple conditions to run the first to return true, but I couldn't get it to work on airtable formulas:
switch(TRUE(),
REGEX_MATCH(data,regex1), 'one matches',
REGEX_MATCH(data,regex2), 'two matches',
REGEX_MATCH(data,regex3), 'three matches',
'none matches'
)
changing true() to 1 doesn't help. I ended up having to use a bunch of nested IFs...
IF(REGEX_MATCH(data,regex1),
'one matches',
IF(REGEX_MATCH(data,regex2),
'two matches',
IF( REGEX_MATCH(data,regex3),
'three matches',
'none matches'
)
)
)
but this seems to be a limitation in the way Switch is implemented.
Hope this helps
Cheers