Why is Contains() working only for the first entry of a list?

I know what the problem is. I will put my answer under a spoiler because I sent this thread to my students as an illustration of what I talked about in a recent lecture.

The answer

Look closely at both the User Roles and Visibility. Those are not lists – those are lists of lists as indicated by the icons in the reference chips as well as by the [] square brackets you can see in the formula popup.

You’re definitely forgetting a .First() somewhere after a filter. E.g. you’re selecting User Roles in a multi-select cell in a table but then you’re reading it from the whole column - e.g. Table.[User Roles] or such, and as a result you get an outer list (for all the cells in a column, even if just one) of an inner list of values (from a multi-select).

Then, of course, if you apply .Contains() on it, it tries to find the whole sublist in a list of sublists, not just one or any reference element in a list of reference elements.

Ideally you should fix your underlying formulas so that you always get what you expect to get – in this case a list of references.

A quick fix would be to either:

  • add .First() after both refs, e.g. [User Roles].First().Contains([Visibility].First()),
  • or, better, use .ListCombine() to flatten whatever structure to a list: [User Roles].ListCombine().Contains([Visibility].ListCombine())

More on the topic:
Here’s a riddle for the community

3 Likes