I’m trying to create a formula that adds up all values from a specific field across multiple related records in Notion. I have two databases connected through a relation property, and I want to sum up all the values from one field in the related database.
For example, I have Database A with records that each have an “amount” field. Database B has a relation to Database A and I want a formula column that calculates the total of all “amount” values from the related records.
I can get individual values using something like prop("My Relation").first().prop("amount") which returns just the first record’s value. I can also use prop("My Relation").last().prop("amount") for the last one.
But I need to loop through all the connected records and add up their “amount” values. Is there a way to iterate through the entire relation array and sum all the field values?
yeah, that works perfectly - prop("My Relation").map(current.prop("amount")).sum() is exactly what you need. I’ve been using this for my budget tracker for years. just heads up - if any amount fields are empty, Notion treats them as 0, which could throw off your totals if that’s not what you want.
Notion formulas don’t have loops, but you can work around this with map() and sum() together. Try this: sum(prop("My Relation").map(current.prop("amount"))). It grabs the amount from each related record and adds them up. I use this all the time in my project databases to sum hours or costs across tasks. Just make sure your amount field is set to number, not text - otherwise sum won’t work. It’s held up fine even with tons of related records.
The formula you want is prop("My Relation").map(current.prop("amount")).sum(). It maps over each related record, grabs the amount value, then sums everything up. I’ve used this for months in my expense tracker to total invoices - works great. Heads up though: empty amount fields get treated as zero. This beats trying to access records by index since relation order can shift around. Handles any number of related records automatically, whether you’ve got two or twenty.