CurrentValue Conflict in Nested Formulas

I’ve encountered an issue with the CurrentValue reference when working with nested formulas in Coda. Generally, CurrentValue works as expected when referencing the current value in a list. However, problems arise when I have nested functions.

For example, when using the FOREACH function to list items in a row and apply another calculation to each item, the issue occurs if the nested calculation involves a FILTER function that also uses CurrentValue. There seems to be confusion between the CurrentValue in FOREACH and the one in FILTER.

For example, I want to calculate availability for each user from the resource pool. I am using ForEach to list resources and filter through the annual leaves table to find leaves for the user, mentioned in this resource. So I have
[list of resources].ForEach([Annual Leaves].Filter(CurrentValueFilter.User = CurrentValueForEach.User).the rest of the formula)

There’s no way to differentiate between CurrentValueFilter and CurrentValueForEach

I’m using WithName to wrap CurrentValueForEach into something else, and it works, but it adds a ton to the complexity of the final calculation.

It would be great if there was a way to distinguish between the different CurrentValue references or another solution to this problem.

Hey Vasily!
WithName() was created to solve this issue :slight_smile: Best of luck!

Oh sorry I only skimmed through your post.

I’m using WithName to wrap CurrentValueForEach into something else, and it works, but it adds a ton to the complexity of the final calculation.

I agree it’s not the cleanest solution but I think it works fine, I wouldn’t call this a bug

1 Like

I run into this “issue” almost daily, and the formulas are much more verbose because of the withName workarounds.

I created a suggestion a while ago: Custom name for current value as a function parameter

1 Like

Hey @Vasily_Skalon! I tend to agree with @Rickard_Abraham: maybe it is a bit more verbose, but actually not a bug.

I am actually grateful to WithName() introduction as it allows to write more maintainable and understandable code (especially when shared with others).

[ListOfUsers].ForEach(CurrentValue.WithName(n_User,
  [ListOfResources].ForEach(CurrentValue.WithName(n_Resource, 
     If(n_Resource.[Annual Leaves] = n_User, ...)
  ))
))

There are definitely 2 more parenthesis (I don’t know if you were referring to this with a ton of complexity), but I find it comprehensible.
When you have even more nesting, refer to named variables lets the logic to be more explicit.

Happy to dig more into it if you have other examples to share.
Cheers!

3 Likes

It’s okay when it’s a single “WithName”, but I wrote this post after spending roughly 4 hours trying to crack a formula with four extra “WithNames”, and it was very confusing.

An alternative possible solution would be for coda to enable using super/parent/outer like some programming languages do. For example:

[Users].ForEach(
    [Resources].ForEach(
        [Items].Foreach(
            CurrentValue // item
            Outer.CurrentValue // resource
            Outer.Outer.CurrentValue // user
        )
    )
)
1 Like