Sum YAML values from notes that link to this note ('FROM [[#]]'), DataViewJS?

Help with → Sum YAML values from notes that link to this note (‘FROM [[#]]’), DataViewJS?

Trynig to → Track payment from tag (#payment) and link to the current note ([[#]]); sum values in specific YAML tag resulting from aforementioned query.

Hello folks! First time posting here, so please be patient =)

I’m trying to use Obsidian + DataView + Templater (essentially) to keep track of my classes (I’m a dance instructor). I’ve already managed to make templates for each class, each course, and each student. The student files are basically derived from the classes and courses he/she takes, using DataView.

The next step is to track payment. I’ve made a simple file for each payment, with a YAML header containing the corresponding class, date, value, the tag #payment and a link to the students’ note (the link is both in the YAML header and the body of the payment note).

Recently I added to the students’ note (and to the template for new students) a DataView table with his/her payments. That was the easy part, using FROM [[#]] AND #payment in my query whithin the student’s note.

I’ve been looking for a way to sum all the payments from a specific student, and have not found it yet. The solutions I’ve found (wich use DataViewJS) can sum all the “value” YAML tags from all notes with the tag #payment but it sum all the payments, from everybody.

How can I return something similar to te results from the query FROM [[#]] and #payment, except that all the values are summed up? I think it is possible to do it using DataViewJS, but JavaScript isn’t something I can learn overnight (I’ve studied the basics, a few months ago, but I’m still very noob, it’s hard even to adapt answers I find to my specific problem). Any ideas?

If it’s not possible, I can work around it by making individual folders to group up the payments by student… But I’d like to make the same analysis for different courses, or just the private classes, or just within a semester… A makeshift solution would solve the immediate problem, but prevents further enhancement.

Thanks in advance!

One thing is the goal, other thing is the way how the wanted metadata is structured:

  • you don’t show us the metadata structure;
  • you only mention the source filter;
  • you don’t show us the query per student (without the sum);
  • etc.

Não podemos adivinhar a estrutura de metadados utilizada e se ela permite obter a soma dos valores!

Ok, I’ll be more specific. The notes are in portuguese, but I think most of the tags have similar names in english, so it should not be an issue. If there is need to a complete translation I can do that with a bit more time.

Here is a student’s note, with the option to generate a new payment:

After clicking the button and filling the fields, the note for that specific payment is generated:

According to the DataView query in XOXO’s note (first image), the new payment is visible in a table:

What I would like is to sum every payment of that particular student. I’ve found and modified the following DataViewJS snippet:

which returns the sums of all payments, not only those from XOXO.

Can the snippet above be modified to look for:

  • pages with the tag #pagamento
  • AND pages that link to the current page (in this case, XOXO’s student note)?

Or would it be better to take a step back and approach this problem another way?

Não há problema com o Português (apenas nos divide o Atlântico).

You can use an js inline query (just to simplify).

`$="R$ " + dv.pages('#pagamento AND [[]]').valor.array().reduce((acc, val) => acc + val, 0) + ",00"`

Funcionou! Muito obrigado! :star_struck:

By the way, I changed the formatting a bit to be a block instead of an inline, which I prefer for readability/understanding. The final implementation has the following form:

dv.header(1, "Total Pago:")
let sum = dv.pages('#pagamento AND [[]]')
	.valor
	.array()
	.reduce((acc, val) => acc + val, 0)
dv.paragraph("R$ " + sum + ",00")

Thanks again!

1 Like

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