We’re excited to introduce the new WithName() formula which enables two key scenarios - reusing logic within a formula, and nested loops.
Reusing logic within a formula
Sometimes you need to use the same logic twice in different parts of the formula. Until now, you had a few choices:
- Duplicate the logic twice. For example:
If( Tasks.Filter(Team="Eng").Count() > 0, Tasks.Filter(Team="Eng").Count(), “Done!” )
- Create an intermediate column or named formula. For example:
- First write the formula
Tasks.Filter(Team="Eng").Count()and name it
- Then write a second formula:
If(EngCount > 0, EngCount, "Done!")
The first approach is prone to errors if you update the logic and forget to keep it in sync. The second requires extra columns or formulas, even if you never need to use the sub-expression elsewhere.
These cases are now simpler with the new
WithName() formula. You can use it to name any value or sub-expression for use elsewhere in the formula. Just specify:
valueyou want to name
nameyou want to give it
expressionfor the formula you want to evaluate. Within that expression, you can use
nameto reference the corresponding
The example above now becomes:
WithName( Tasks.Filter(Team="Eng").Count(), EngCount, If(EngCount > 0, EngCount, "Done!") )
We hope this helps you create simpler & more readable formulas that are less error-prone, while avoiding bloating your doc.
Note: If you intend to reference the expression you’re renaming in multiple formulas, we still recommend storing it in a separate column or named formula so it only evaluates once. Any formula with multiple costly expressions may also evaluate more efficiently if you represent each part in a separate column or named formula.
Certain loop formulas in Coda like
AddRows() run multiple times across a list, and use the keyword
CurrentValue to reference the current item from the list.
For example, if you want to add a row to a
Schedule table for each date in a
Dates table, you can create a button with this formula:
Dates.FormulaMap( AddRow(Schedule, Date, currentValue) )
Things broke down when you needed to nest multiple loop formulas. For example, if you wanted a row in the
Schedule table for each combination of dates and people from a
People table, you might try:
Dates.FormulaMap( People.FormulaMap( AddRow(Schedule, Date, CurrentValue, Person, currentValue) ) )
The problem this created is that you want the first
currentValue to refer to the current date from the
Dates table, and the second
currentValue to refer to the current person from the
People table. Unfortunately, this wasn’t possible.
With the new
WithName formula, you can now simply assign a unique name to each
currentValue. Here’s an example:
Dates.FormulaMap( WithName(currentValue, currentDate, People.FormulaMap( WithName(currentValue, currentPerson, AddRow(Schedule, Date, currentDate, Person, currentPerson) ) ) ) )
If you prefer, you can also write the same formulas in a different order with dot-chaining:
Dates.FormulaMap( currentValue.WithName(currentDate, People.FormulaMap( currentValue.WithName(currentPerson, AddRow(Schedule, Date, currentDate, Person, currentPerson) ) ) ) )
We’re excited for the opportunities this opens up for more advanced needs. We know many of you have felt the pain of this in the past, and resorted to clever but cumbersome workarounds. Thanks for highlighting this issue and for your patience as we worked to find a flexible solution!!