`Only()` formula

A lot of times I filter some table and expect that there will only be a single result.

E.g. [Subscriptions].Filter([Client] = thisRow and [Active?]).First()

To get that final result, I often call First(), but First() has two problems:

  1. It doesn’t check that my filter only returns one result
  2. It doesn’t convey to the person reading my formula that there should only be one result.

If there was an Only() formula, it could do both those things.

E.g. [Subscriptions].Filter([Client] = thisRow and [Active?]).Only()

  1. Now the person reading the formula understands that they can rely on that filter only returning one result
  2. The formula can return a useful error if it’s expectations are violated: “Only() expected a list with only a single value, but instead received [“A”, “B”, “C”].”

I could get around this by writing:

[Subscriptions].Filter([Client] = thisRow and [Active?]).WithName(ActiveSubscription,
  If(ActiveSubscription.Count() > 1, 
    List(Max(List()), "Expected a List with only 1 entry (this is my sneaky way of throwing an exception)"),
    ActiveSubscription.First()
  )

Obviously, this is much much more verbose than just .Only()

What about adding different versions of the filtering formulas, like Find() or FindFirst()?

1 Like

I’m open to that, how would you use Find() or FindFirst() to inform a reader that there should only be one value returned, and how might a doc maker ensure that the returned value is the expected length of 1?

Unique() outputs only one value, why is this not an option?

Unique() does not guarantee a single item is returned. E.g. List("A", "B", "A").Unique()List("A", "B")