I’m working with pyairtable library to retrieve records from my Airtable database using Python. I have two separate formulas that work perfectly on their own, but I need help combining them into a single query.
The first formula filters records where a specific field equals a certain value. The second formula excludes records where another field is empty. I want to merge these conditions so I can get records that match both criteria at once.
Here’s what I have so far:
from pyairtable import Table
from pyairtable.formulas import match
table_instance = Table(my_api_key, database_id, table_name)
# First condition works
filter_formula = match({"category": "active"})
data = table_instance.all(formula=filter_formula, fields=['name'])
# Second condition also works
filter_formula = "NOT({status}='')"
data = table_instance.all(formula=filter_formula, fields=['name'])
How can I create one formula that checks both category equals ‘active’ AND status is not empty?
Wrap both conditions in an AND() function. Since pyairtable’s match function creates proper Airtable formula syntax, you can combine it with raw formula strings.
Here’s what I do:
from pyairtable import Table
from pyairtable.formulas import match
table_instance = Table(my_api_key, database_id, table_name)
# Combine both conditions
match_condition = match({"category": "active"})
not_empty_condition = "NOT({status}='')"
filter_formula = f"AND({match_condition}, {not_empty_condition})"
data = table_instance.all(formula=filter_formula, fields=['name'])
I’ve done this tons of times building data pipelines. The trick is knowing that match() returns a string formula, so you can use it like any other formula piece.
For more complex formulas, this video really helped me understand Airtable formula basics:
Btw, you can swap AND() for OR() if you want records that meet either condition instead of both.
I ran into this same issue building a client dashboard. Skip the manual string construction - just import the AND function from pyairtable.formulas instead. Here’s what actually works:
from pyairtable import Table
from pyairtable.formulas import match, AND
table_instance = Table(my_api_key, database_id, table_name)
filter_formula = AND(
match({"category": "active"}),
"NOT({status}='')"
)
data = table_instance.all(formula=filter_formula, fields=['name'])
This kills the string formatting headaches and stays readable when you add more conditions. Bonus: pyairtable’s formula functions handle type conversion automatically, which is huge when you’re mixing different field types.
use pyairtable.formulas.AND instead of string formatting - way cleaner. filter_formula = AND(match({'category': 'active'}), 'NOT({status}='')') then pass it to table.all(). Skips the f-string mess and handles escaping better.
use AND() to combine: filter_formula = f"AND({match({'category': 'active'})}, NOT({{status}}=''))" then use that in table.all() call. should work perfectly!