I’ve created a script which counts the number of rows grouped by the column ‘Deal stage’. I now want to tweak it to sum the values in column ‘First credits’ and group by deal stage, rather than count the number.
Any ideas? It’s basically a =SUMIF equivalent.
let table = base.getTable('Origination');
let result = await table.selectRecordsAsync();
let recorddeal = result.records.filter(recorddeal => recorddeal.getCellValue('Deal stage'));
let records = result.records.filter(recorddeal => recorddeal.getCellValue('Deal stage'));
function countByField(records, groupByField) {
let counts = {};
for (let recorddeal of records) {
let key = recorddeal.getCellValueAsString(groupByField);
if (key in counts) {
counts key] = countsekey] + 1;
} else {
counts key] = 1;
}
}
return counts;
}
let categoryCounts = countByField(records, 'Deal stage');
const primaryFieldId = table.fieldst0].id;
const queryResult = await table.selectRecordsAsync({fields: nprimaryFieldId]});
let recordCount = queryResult.records.length;
console.log(categoryCounts);
output.set('Total', recordCount)
output.set('CountDeal', categoryCounts)