How to Build Notion Formula That Counts Filtered Sub-Items
🔍 WiseChecker

How to Build Notion Formula That Counts Filtered Sub-Items

Counting only the sub-items that meet specific conditions is a common need in Notion databases. A direct count of relation-linked items includes every sub-item regardless of its status or properties. This article explains how to build a Notion formula that counts only filtered sub-items using the filter() and length() functions. You will learn to create a formula that returns the number of linked database entries that match criteria you define.

Key Takeaways: Counting Filtered Sub-Items in Notion

  • filter() function: Creates a temporary list of only the sub-items that pass your condition, such as Status equals “Complete”.
  • length() function: Counts the number of items in the filtered list and returns a number you can display in any property.
  • Relation property setup: You must have a Relation column linking the parent database to a child database before writing the formula.

ADVERTISEMENT

How Notion Formula Functions Work for Counting Sub-Items

Notion formulas operate on database properties, not on the database rows themselves. When you have a Relation property that links to another database, the formula sees that relation as a list of linked page IDs. To count only sub-items that match a filter, you need to apply the filter() function to that list. The filter() function requires a condition written as a boolean expression. For example, you can check if a sub-item’s Status property equals “Completed” by writing prop("Status") == "Completed" inside the filter. The length() function then counts the resulting list. The entire formula follows this pattern: length(filter(prop("Related Items"), condition)). The condition must reference properties that exist in the child database.

Prerequisites for the Formula

Before writing the formula, confirm these items exist in your workspace:

  • A parent database with a Relation property pointing to a child database.
  • The child database contains the property you want to filter on, such as a Select property named Status or a Checkbox named Is Complete.
  • At least one parent row has linked sub-items. The formula returns 0 if no sub-items exist.

Steps to Build a Notion Formula That Counts Filtered Sub-Items

The following steps assume you already have a Relation property named “Tasks” that links to a child database containing a Select property named “Status” with options like “Not Started,” “In Progress,” and “Completed.” The goal is to count only sub-items where Status equals “Completed.”

  1. Open the Formula property editor
    Click the + button at the top of the parent database column header. Select Formula from the property type list. Name the property “Completed Tasks Count” or a similar descriptive name.
  2. Write the filter condition
    In the formula editor, type filter(prop("Tasks"), . Press Enter to add a new line. Inside the filter, reference the child property using current. followed by the property name. For example: current.prop("Status") == "Completed". Close the parentheses: ). The formula so far is filter(prop("Tasks"), current.prop("Status") == "Completed").
  3. Wrap with length()
    Place your cursor at the beginning of the formula. Type length(. Move to the end of the formula and type ). The final formula is: length(filter(prop("Tasks"), current.prop("Status") == "Completed")). Click Save.
  4. Test the formula
    Add or edit a sub-item in the child database and set its Status to “Completed.” The parent row should automatically update the count. Change the Status to a different value and verify the count decreases by one.

Alternative Filter Conditions

You can replace the condition with any boolean expression that references a child property. Common examples:

  • Checkbox: current.prop("Is Complete") == true
  • Date range: current.prop("Due Date") <= now()
  • Multiple conditions: current.prop("Priority") == "High" and current.prop("Status") == "Not Started"

ADVERTISEMENT

Common Mistakes When Counting Filtered Sub-Items

Formula returns an error: "Type mismatch: cannot apply filter to Text"

This error occurs when the property passed to filter() is not a list. Confirm that the property name inside prop() is a Relation column, not a Text or Number column. If you renamed the Relation property, update the formula to use the new name.

Formula returns 0 even though sub-items match the condition

The most common cause is a mismatch in the property name or value. Verify the child property name is spelled exactly as it appears in the child database. Select property values are case-sensitive: "Completed" is different from "completed." Open the child database and check the exact spelling and capitalization of the property name and option value.

Formula counts all sub-items, not just filtered ones

This happens when the filter() function is missing or the condition is not applied correctly. Review your formula to ensure filter() wraps the entire property reference and the condition uses current. to refer to child properties. A formula that uses length(prop("Tasks")) counts all linked items without filtering.

Notion Formula Functions for Counting: filter vs map vs length

Function Purpose Example Use
filter() Returns a list of items that match a condition filter(prop("Tasks"), current.prop("Status") == "Complete")
length() Returns the number of items in a list length(prop("Tasks")) counts all linked sub-items
map() Transforms each item in a list into a new value map(prop("Tasks"), current.prop("Name")) returns a list of sub-item names

You can now build a Notion formula that counts only the sub-items meeting your criteria. Start by confirming the Relation property and the child property you want to filter on. Use the pattern length(filter(prop("Relation Name"), condition)) and replace the condition with your own logic. For advanced filtering, combine multiple conditions using and or or operators. If you need to count sub-items based on a date relative to today, use current.prop("Due Date") <= now() inside the filter.

ADVERTISEMENT