Hello,
I’m not proficient in coding, so I’ve been trying to figure it out on my own, but I’m having trouble.
What I’m trying to do
I’d like to use Dataview to visualize my budget and create a table that groups expenses by type, with a total for each type.
Things I have tried
Thanks to holroy in this topic (Dataview TABLE, GROUP BY with headers), I can create separate tables for each category:
const notes = await dv.query (`
Table without ID
Catégories, Date, file.link as Nom, Montant_depense
from "Comptabilité/Dépenses"
sort Catégories, Date
`)
console.log(notes)
if (!notes.successful) {
dv.paragraph(`~~~~\n${ notes.error }\n~~~~\n`)
return
}
let typeDict = {}
for (let note of notes.value.values) {
if ( !typeDict.hasOwnProperty(note[0]) )
typeDict[note[0]] = []
typeDict[note[0]].push([...note.slice(1)])
}
for (let key of Object.keys(typeDict)) {
dv.header(2, key)
dv.table([...notes.value.headers.slice(1)],
typeDict[key])
}
Thanks to anon63144152 in this topic (Dataview Table: Summing a column), I can calculate the total of all my expenses:
const montants = await dv.query(`
Table without ID
Catégories, Date, file.link as Nom, Montant_depense
from "Comptabilité/Dépenses"
sort Catégories, Date
WHERE number(Montant_depense)
`)
if ( montants.successful ) {
const sum = montants.value.values
.map(a => a[3])
.reduce((tmp, curr) => tmp + curr, 0)
montants.value.values.push([, "" , "", "<span style='float: right'><strong>Total:</strong></span>", sum, " €"])
dv.table(montants.value.headers, montants.value.values)
} else
dv.paragraph("~~~~\n" + montants.error + "\n~~~~")
However, I’d like to combine these two methods to display the total for each expense category. Could someone help me achieve this?