How to get Column totals in dataview

Things I have tried

Messed around within obsidian and tried different forum solutions

What I’m trying to do

so im trying to organise my finances using dataview
i made a table covering all the information i need across the notes tagged #debt as shown below:

Table without ID
file.link AS Account, Amount, Payment_rate, DTI_ratio, Payment_duration, Final_payment 
FROM #Debt AND -"DATA/TEMPLATES"

I would like there to be a total for the amount column either in the table or inline immediately below it but i cant seem to do so

I’ve tried adapting the string below i saw on another post on a similar topic but im still new to queury languages in general so i couldnt figure out what to change the ‘this.Spending’ bit to an array containing the values i needed

`=round(sum(number(this.Spending)), 2)`

essentially i need obsidian to look for all the files tagged debt find the amount in each note and give me a sum total at the bottom

any help is appreciated

My main approach to such an issue is to switch to dataviewjs, and do the extra work on the table within the javascript code there. So here is one attempt on doing this:

## Original query

```dataview
TABLE WITHOUT ID
  file.link as Account, Amount, Payment_rate
WHERE file.folder = this.file.folder 
WHERE number(Amount)
```


## With summation
```dataviewjs
const amounts = await dv.query(`
TABLE WITHOUT ID
  file.link as Account, Amount, Payment_rate
WHERE file.folder = this.file.folder 
WHERE number(Amount)
`)

if ( amounts.successful ) {
  const sum = amounts.value.values
    .map(a => a[1])
    .reduce((tmp, curr) => tmp + curr, 0)

  amounts.value.values.push(["<span style='float: right'><strong>Total:</strong></span>", sum])
  dv.table(amounts.value.headers, amounts.value.values) 
  dv.paragraph("Total amount: " + sum)
} else
  dv.paragraph("~~~~\n" + amounts.error + "\n~~~~")

```

Note how my “original” query needs to be replaced within either query for your query for it to work in your setup. And if you change which column you’re going to sum up, you need to change the a[1] into the correct column. (Note that the first column is 0 (and not 1))

That mystical .reduce( ... ) line, is the line which actually reduces the array into the total sum. Kind of magic, but it works as long as it got some values to sum up.

The script provided above, does both include the sum into the table (by doing that line with the .push( ... ), and afterwards with the line of dv.paragraph( ... ). Choose either one, and format/change the text to match your likings.

So with some random test data this gave the following output:

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.