Skip to main content
Solved

Count Unique Values in Rollup

  • March 25, 2020
  • 14 replies
  • 345 views

Russell_Bishop1
Forum|alt.badge.img+22

I’ve had to do this quite a few times… my old solution was:

  1. Rollup with ARRAYUNIQUE(values)
  2. Remove any trailing ','s in another column
  3. Count the remaining commas, +1

Now I do it all inside the rollup with the following snippet:

And yes - I’m keeping it as a Gist on GitHub for easy access. I’ll be adding more of these gems as I go.

Best answer by kuovonne

If there might be empty values, you could use
COUNTA(ARRAYUNIQUE(values))
instead of
COUNTALL(ARRAYUNIQUE(values))

The differences between COUNTA() and COUNTALL() are documented in the formula field reference. COUNTALL() counts all values, including blanks. COUNTA() does not count blanks.

14 replies

DavidVM
Forum|alt.badge.img+15
  • Known Participant
  • 32 replies
  • May 28, 2020

That’s awesome. How would you address the issue if some of the values being rolled up included a comma. In my example, a corporate name that included a comma (ie ABC Widgets, Inc.)?


Russell_Bishop1
Forum|alt.badge.img+22
  • Author
  • Inspiring
  • 75 replies
  • May 28, 2020

That’s awesome. How would you address the issue if some of the values being rolled up included a comma. In my example, a corporate name that included a comma (ie ABC Widgets, Inc.)?


Oooph! Very interesting. I suppose you’d have to work from the fact that Airtable will always use a ‘,’ separator…

Perhaps you could make a column called Safe Corporate Name that replaces ','s found in your Corporate Names and replaces them with another character using SUBSTITUTE() – and then you can use this Rollup formula on that field instead.

Hope that helps.


  • New Participant
  • 1 reply
  • November 1, 2021

Hey thanks for this! It’s exactly what I needed.


Breight_Group
Forum|alt.badge.img+6
  • Known Participant
  • 13 replies
  • January 14, 2022

Just what we needed, thank you thank you


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • January 14, 2022

Or you could use …

COUNTALL(ARRAYUNIQUE(values))

Russell_Bishop1
Forum|alt.badge.img+22
  • Author
  • Inspiring
  • 75 replies
  • January 17, 2022

@kuovonne I was about to throw all of my papers in the air from my desk and shout from the rooftops “She’s solved it!”

However, I’m not sure these two are doing the same thing, but I don’t know why that is. Let me demonstrate:

Arrayunique just uses ARRAYUNIQUE(values) to list them.

Any thoughts? Is there a tweak to yours that would work?


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • January 17, 2022

@kuovonne I was about to throw all of my papers in the air from my desk and shout from the rooftops “She’s solved it!”

However, I’m not sure these two are doing the same thing, but I don’t know why that is. Let me demonstrate:

Arrayunique just uses ARRAYUNIQUE(values) to list them.

Any thoughts? Is there a tweak to yours that would work?


That does look strange.

Are you rolling up the same target field from the other table?

Do you have any blanks in the field being rolled up? (I doubt it, or there would be two commas in a row.)

What is the field type of the field being rolled up?


Forum|alt.badge.img+5

Slightly more concise way of counting array items.

Where [Unique Editors] is a compacted, unique, joined array.

Assumes no commas in array values

IF(
  LEN({Unique Editors}) > 0, 
  LEN(REGEX_REPLACE({Unique Editors},"[^,]",""))+1,
0)

Russell_Bishop1
Forum|alt.badge.img+22
  • Author
  • Inspiring
  • 75 replies
  • February 10, 2022

Just returning to this – @kuovonne is 100% correct, the only addition is to add a condition in the rollup that the thing you’re looking for unique values of is not empty.

I am going to edit my post to make sure no one wastes their time on my awful, awful alternative :grinning_face_with_big_eyes:


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • Answer
  • February 10, 2022

If there might be empty values, you could use
COUNTA(ARRAYUNIQUE(values))
instead of
COUNTALL(ARRAYUNIQUE(values))

The differences between COUNTA() and COUNTALL() are documented in the formula field reference. COUNTALL() counts all values, including blanks. COUNTA() does not count blanks.


Sean_Lake1
Forum|alt.badge.img+20
  • Inspiring
  • 113 replies
  • September 2, 2022

Or you could use …

COUNTALL(ARRAYUNIQUE(values))

Doesn’t work:
COUNTALL(ARRAYUNIQUE(values))
for

Rollup fields aren’t an option for other rollups, so in my case, trying to count the unique values in the rollup field won’t work. I have the above in a formula field and it only returns a 1.


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • September 2, 2022

You probably need a rollup of a lookup with ARRAYFLATTEN. It is hard to tell without a deeper dive into your schema.


Forum|alt.badge.img
  • New Participant
  • 1 reply
  • September 22, 2022

Slightly more concise way of counting array items.

Where [Unique Editors] is a compacted, unique, joined array.

Assumes no commas in array values

IF(
  LEN({Unique Editors}) > 0, 
  LEN(REGEX_REPLACE({Unique Editors},"[^,]",""))+1,
0)

This is nice @Collin_Schwantes, I like the [^,]
For a rollup you’d need to convert it into a string and add the separators

IF(
  LEN(ARRAYJOIN(ARRAYUNIQUE(values),',')&"") > 0, 
  LEN(REGEX_REPLACE(ARRAYJOIN(ARRAYUNIQUE(values),',')&"","[^,]",""))+1,
0)

Forum|alt.badge.img+4
  • Known Participant
  • 11 replies
  • August 20, 2024

Hi Russell,

This is the solution that I am looking for but it isn't returning the right number, I think because the values I'm trying to count have a comma in them. Would this be why and would you suggest how to fix? I am not a coder so modifying your code is difficult for me.

Debbie