• 0

# How to apply custom formula for sub-groups in aggregation

## Question

I have a DataPage that shows Total and Due by Group.

I used two aggregations, one is SUM for total and another one is SUM-2100 for due.

Those values are fine in Group, but Caspio tries to calculate from entire values.

I assume that "-900" is coming from 2100-(400+800). What is the best way to show "-3000" in TOATAL DUE?

(COUNT([@field:Applicants_Email])*2100-SUM) did not work because COUNT increase a number by one.

I am fine to hide "TOTAL DUE" if there is no good way. But, I cannot find a way to hide it. Any idea?

Thank you very much.

## Recommended Posts

• 0

Hello @ttanemori,

Since the formula in the Aggregations has the same logic for Totals and groups, please try this JavaScript code.

This code replaces the value in the Total Due with the sum of values that you receive in the groups (in your example it summarizes -1700 and -1300).

```<script type="text/javascript">

function sumGroups() {
const totals = [];
document.querySelectorAll("td[class*='cbResultSetGroup1LabelCellNumberDate']").forEach(elem => {
totals.push(elem.innerText);
});

const result = totals.reduce( (accumulator, currentValue) => parseFloat(accumulator) + parseFloat(currentValue) );

document.querySelector('tr[data-cb-aggregation="Aggregate1"] > .cbResultSetTotalsData').innerText = result;
};

</script>```

Replace Aggregate1 with the Aggregate2, etc. depending on the number of your Aggregate field.

Add this code to the Header section on the "Configure Results Page Fields" screen of your DataPage.

Do not forget to disable HTML editor before pasting the code.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.