Dataview Table: Summing a column

What I’m trying to do

So I want to create a simple data view table of my monthly subscriptions that sums up the price column for me.

I’ve searched all over the forum and online, and it seems so simple of a task yet the threads have all been extremely confusing for a newbie like myself.

My folder organization is the following:

Folder called “Subscriptions” that contains two subfolders:

a subfolder called “01_Index” with a note inside called “Index”

a second subfolder called “02_List” followed by a subfolder inside named “Active” populated by notes (a note for each subscription service by name).

On the index note I have a data view table as the following:

Table frequency, date, amount, type
From "Subscriptions/02_List/Active"
WHERE contains(frequency, "monthly")
SORT default(sendout, "") desc

This data view table pulls from the YAML of my notes which each have these criteria:


amount: 9.99
frequency: monthly
type: auto renewal
date: 2023-06-10

(the above is an example)

Here is a screenshot of that data view table on the INDEX note

Things I have tried

I’ve not tried a lot because I don’t understand how to apply the advice from other specific help threads to mine.

Essentially I just want a little bar under the amount column that would tell me the sum of those subscriptions.

Thank you

Any help would be greatly appreciated

The queries below work in local tests (see screenshot).

The WHERE file.folder = this.file.folder would need to be changed to your FROM statement.

Used these starting points:

# D1
```dataview
TABLE frequency, date, amount, type
WHERE contains(frequency, "monthly") 
WHERE file.folder = this.file.folder 
SORT default(sendout, "") desc
```
# D2
```dataview
TABLE WITHOUT ID sum(map(rows, (r) => default(r.amount, 0))) AS "Total Price"
WHERE file.folder = this.file.folder 
GROUP BY true
```
# D3
```dataviewjs
const amounts = await dv.query(`
TABLE
  amount, frequency, date, type
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) 
} else
  dv.paragraph("~~~~\n" + amounts.error + "\n~~~~")
```

3 Likes

Thanks a bunch! I had a little trouble getting D3 to work (I’m not sure exactly why), but D1 and D2 were sufficient for now!

I appreciate it

1 Like

A pleasure. Great news. :smile:

1 Like

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