Links

Count Multi-Select Tags

Here’s a database that contains a multi-select property called Members. How can we get the number of members that have been added to each row?
We can accomplish this by using the replaceAll function, as shown in the Shares formula:

View and Duplicate Database

"Shares" Property Formula

Compressed:
1
length(replaceAll(prop("Members"), "[^,]", "")) + 1
Expanded:
1
/* Get the length of the string */
2
3
length(
4
5
/* Convert the list of Members into a string of commas,
6
removing all other characters. */
7
8
replaceAll(
9
prop("Members"),
10
"[^,]", // [^,] = every character EXCEPT `,`
11
""
12
)
13
) + 1 // Add 1, since the last item in the Members string has no comma
The Shares formula counts the number of members listed in the Members multi-select property for each heist.
Notion doesn’t provide a default way to get this count (unless you use a Rollup), so we have to use the replaceAll function with a regular expression.
What’s a regular expression?
Here’s how the formula works:
  1. 1.
    Pull the Members property into our formula. By default, multi-select property values are pulled in as a comma-separated string - e.g. Danny, Rusty, Reuben, Linus.
  2. 2.
    We call the replaceAll function, which finds a pattern in a string and replaces it with another pattern. We write our function as replaceAll(prop("Members"), "[^,]", ""):
    • The first argument - prop("Members") pulls in the Members property.
    • The second argument - “[^,]” - searches for any character that isn’t ,. This pattern is called an exclusion.
    • The third argument - "" - replaces every found character with nothing, effectively removing them from the new string.
  3. 3.
    The replaceAll function returns a string of commas - e.g. ,,,,,,,,,,.
  4. 4.
    We use the length() function to count the characters in our string of commas - e.g. 10.
  5. 5.
    Since the last item in the original list of Members won’t have a comma in it, we add 1.
  6. 6.
    The formula outputs the final Shares count as a number. For the Bellagio Job, that number is 11.

Splitting a Pool of Money

In the divide article, this Heist Splitter example was used to show how we could split a Total pool of money among the members who took part in each heist.
Here’s a new view of that database, which contains a new property called Split (Full). This formula efficiently combines the counting function we built above with the division operation, returning the share each heist member should get.

View and Duplicate Database

Here’s the code:
1
// Compressed
2
prop("Total") / (length(replaceAll(prop("Members"), "[^,]", "")) + 1)
3
4
// Expanded
5
prop("Total") / (
6
length(
7
replaceAll(
8
prop("Members"),
9
"[^,]",
10
""
11
)
12
) + 1
13
)

About the Author

My name is Thomas Frank, and I'm a Notion-certified writer, YouTuber, and template creator. I've been using Notion since 2018 to organize my personal life and to run my business and YouTube channel. In addition to this formula reference, I've created a free Notion course for beginners and several productivity-focused Notion templates. If you'd like to connect, follow me on Twitter.