Calculate row expressions and collection totals
In the model editor, use expr to combine editable scalar fields without writing callbacks. Row fields keep their declared type: text fields work with eq, numeric fields work with arithmetic, and Money fields preserve their currency.
const rows = deccf.input.objectArray([
{ id: deccf.input.id("a"), status: deccf.input.str("sell"), annual: deccf.input.money(1200, deccf.currencies.SEK) },
{ id: deccf.input.id("b"), status: deccf.input.str("keep"), annual: deccf.input.money(2400, deccf.currencies.SEK) },
]);
const row = rows.item.data;
const monthly = deccf.calculation({
name: "Monthly amount",
calculate: deccf.expr.when(
deccf.expr.eq(row.status, "sell"),
deccf.expr.multiply(row.annual, 1 / 12),
deccf.money(0, deccf.currencies.SEK),
),
});
const items = deccf.repeat(rows, deccf.component({ calculations: { monthly }, outputs: { monthly } }));
const total = deccf.calculation({
name: "Selected monthly total",
calculate: deccf.expr.sum(items.outputs.monthly, { resultType: "money", currency: deccf.currencies.SEK }),
});Declare rows among the model's preconditions, items among its components, and total among its calculations. The two rows above produce a selected monthly total of 100 SEK. Adding, removing or replacing rows in a scenario recalculates the total from that scenario's rows.
expr.sum() accepts arrays, keyed records, repeated calculation or flow outputs, and typed columns. It sums the collection's members. Within a repeat, an ordinary repeated output operand refers to the matching row; passing the same output directly to sum selects its whole collection. Include the source repeat in the model's components, directly or inside an ordinary component. Sources and their consumers can appear in either order; each source is created once in its enclosing row.
Supply resultType: "number" or resultType: "money" when a collection may be empty. An empty total is zero of that type. For Money totals, supply the currency when it must be preserved even without rows. Actual members must match the declared type and use one currency. Named records are not flattened into numeric fields automatically.
Sum input fields before creating components
column(source, field) selects a keyed field map from an ordinary object-array input, a nested object-array projection, or a repeated collection. A repeated collection also keeps its existing .column(field) method.
const annualTotal = deccf.expr.sum(deccf.column(rows, "annual"), {
resultType: "money",
currency: deccf.currencies.SEK,
});This total needs only the declared input rows; no repeat is required. For loans nested under a home, column(homeRow.loans, "amount") selects that home's loan amounts, including when used inside one of its loan rows. It can provide an independent aggregate debt assumption to each tranche's amortization rule, avoiding a dependency on the same loans' repayment calculations.
Use lookup(column(source, field), relatedId) to select one related row by its explicit ID. Missing IDs fail unless an explicit fallback is supplied. The selected value keeps the field's type, so it can be used in compatible scalar expressions.
A sum of flow outputs is available at period end. Use input fields or period calculations for assumptions that must be available before payments, such as a loan's interest rate. For multiple nested collections, calculate a scalar total within each enclosing row and then sum those totals.
Sum current account balances
Use expr.balance(accounts) for the combined opening ledger balance of an account array, keyed account map, or repeated account output map. The result is Money. An empty collection gives zero in the model currency. All accounts must belong to the model and use its currency. Nested records and repeated aliases for the same account are rejected.
const openingDebt = deccf.expr.multiply(
deccf.expr.balance(debtAccounts.outputs.debt),
-1,
);
const selectedDebt = deccf.expr.balance(
deccf.lookup(debtAccounts.outputs.debt, loanRow.id),
);Ledger liabilities normally have credit balances, so the first expression converts their negative ledger total into positive debt. Passing a repeated account output directly to balance selects the whole account collection, including inside another repeat. Use lookup to select one account by ID. For a loan list nested under each home, the collection stays within the enclosing home; identical loan IDs in different homes remain separate.
For an LTV-based repayment rule, declare liability accounts independently from the loan calculations, and pass their current combined opening debt as debtForLtv. Keep the regulatory valuation and highest-credit principal base separate. Post principal repayments to those accounts so later periods use the reduced debt. Add same-period drawdowns explicitly when the repayment policy requires them; an opening balance does not include current-period postings.
The existing balance(account, { after: [flow] }) snapshot also works with account collections. It includes the selected flows and their required predecessor flows, preserving the existing calculation order and phase restrictions. It does not include unrelated current-period postings.