I need help creating a Notion formula that adds up values from a specific property across all related database entries.
Here’s my situation: I have two databases connected through a relation. In the first database, I have records with an “amount” property containing numbers like 8 and 3. In the second database, I want to create a formula column that calculates the total of all “amount” values from the related records in the first database.
I can get individual values using something like prop("Connection B").first().prop("Amount") which returns 8, or I can access the last item. The prop("Connection B") gives me an array of related records.
The problem is I don’t know how to loop through all the connected records to sum up their “amount” properties. I want the formula to automatically calculate 8 + 3 = 11 in my second database.
Is there a way to iterate through all items in the relation array and add up their property values? What formula syntax should I use for this?
there’s actually an easier way - just use prop("Connection B").prop("Amount").sum(). notion automatically sums all the amount values from your related records. you don’t need the map function since relation properties can directly access connected record properties and sum them in one go.
Both approaches work, but I hit problems with the simpler syntax on empty relations. What saved me was wrapping everything in an if statement first. Try if(empty(prop("Connection B")), 0, sum(prop("Connection B").map(current.prop("Amount")))). This stops errors when there aren’t any related records yet. Learned this the hard way - my formulas kept breaking on new entries before I’d added connections. The map approach beats direct property chaining, especially if you want to add filtering for specific related records later.
You need map() with sum(). Since you already know prop("Connection B") gets the relation array, just iterate through each record and grab the amounts. Use this: sum(prop("Connection B").map(current.prop("Amount"))). This maps through each record in your relation, pulls the Amount from each one with current.prop("Amount"), then sum() adds them all up. current is just each individual record as it loops through. I use this same pattern in my databases when I need totals from child records. Works great - the sum updates automatically when you add or remove related records.