Script to count non-empty records for a field

Hi,

I’m new to Airtable scripting and figuring this out as I go along. What I’m trying to do is to get a script to count the number of non-empty records for a specific field in one of my tables. This is because this field does have some blank values for some records and I don’t want to simply count all the records in the table.

let query = await myTable.selectRecordsAsync({
    fields: [myField],
});

let recordCount = query.records.length;

When I use the script above, I get the total number of records in myTable.

How can I add a condition to this so that I’m only getting a count of those records where myField is not empty?

Thanks

There are two aspects to doing this

  • You have to be able to tell if a field has a value for a given record. You need record.getCellValue(field) for this.
  • You need a way to get the number.

There are multiple ways to get the count. Which method to use depends on you coding experience and what you are most comfortable with. Here are just a few common methods.

  • a for loop to loop through the records, and a sum variable that gets increments inside the loop if the record has a value
  • the array filter function that identifies records that meet your condition, then get the length of the filtered array
  • the array reduce function to reduce the array to a number

Pick the method that fits your current level of coding experience. If you are new to coding and want to learn to code, I recommend implementing all three as part of your learning journey.

2 Likes

Thanks kuovonne. That makes sense.