Problem with conditional formatting of linked fields

Please fill in the questionnaire below.

Help Questionnaire

Describe the problem

Describe, step by step, how to reproduce the error or problem you are encountering.

Create TableA and TableB.

Create a linked field LinkedField in TableA linking to TableB.

Create a record in TableB.

Create several records in TableA, some that has LinkedField set to link to the record in TableB, some that have not set it (ie, the LinkedField is empty, no relations).

Create a formula field in TableA, MyFormula

Set MyFormula to all kinds of variants for how to check if LinkedField has any values and then do something based on it, with the ultimate intent being to look up LinkedField if LinkedField is set and use a default otherwise

Here are some examples of variants that fail:

  • if(equal(count(field(‘LinkedField’)), 0), ‘Nothing’, field(‘LinkedField’))
  • when_empty(field(‘LinkedField’), ‘Something’)
  • if(is_null(field(‘LinkedField’)), ‘Null’, ‘Something’)
  • Creating intermediate other fields with e.g. t(totext(field(‘LinkedField’()) and then doing is_null or when_empty against that field.

What DOES work is

  • if(equal(count(field(‘LinkedField’)), 0), ‘Nothing’, ‘Something’)

but it does not give me access to the values of LinkedField when LinkedField is set.

Qualified guesses at what is wrong and how to fix it

Overall, what all of this looks like is that there is eager evaluation in if() and when I do field(‘LinkedField’) and LinkedField is empty it generates an error that invalidates the expression. This error is not visible in the UI; it looks just like a blank string.

Typically, for an expression language like this, you need if() to be lazy - ie, it cannot evaluate the arguments until it’s evaluated the condition, and then it has to evaluate just the appropriate branch. Alternatively, you need to treat error as a first class value and provide functions like is_error (and a workable display that shows it) rather than having errors abort the evaluation of everything.

Hey @eeklund, welcome to the Baserow community! :wave:

I’ve quickly checked the formula examples you provided. They should work when changing the curved quotation marks to straight or double ones.

Regarding empty values, we have this issue on our backlog to fix: Using the if() function for if empty/null (#944) · Issues · Baserow / baserow · GitLab

Thanks for the welcome!

The curly quotes is something that’s been inserted by the Discourse editor. I was using standard single quotes (ascii 39). I tried changing to double quotes (ascii 34) but it made no difference (and I didn’t expect it to.)

I tried to take a look at the underlying expression/language evaluation code and see if I can find the problem; it’s non-trivial. It seems clear that if() is not evaluated lazily, but not how the underlying error handling goes. I’ve brought up a dev environment reproducing the problem in an isolated case and I’ll see if I can get you a patch.