Sum the integer in YAML tag, within two files, into Dataview Table

As shown above, I have two files, one named:
the other:

Both of them have a YAML field/tag named: Meditation and Sleep
I enter values into each of these fields each day

I have now another file, that I’d like to have a TABLE within, which adds these two values together for both files, and provide it as a single number.

Where am I going wrong?

Do you have one or two yaml tag(s)? Do you want to add birth the meditation value and the sleep value from both dates into a single value?

Could you show some example data, and expected output?

Thanks so much Holroy!

I realised in writing back to you, where I’ve gone wrong.
But it has lead me to a second question.

Is it possible for me to use Dataview, to sum both the values, in both files, and output it as a single value?

If contained the values:
meditation: 45
sleep: 8

and, contained the values:
meditation: 30
sleep: 7.5

the total value would be: 16.75 (because meditation is minutes, and sleep is hours)

My objective would be to have this new 16.75 value, stored in it’s own YAML tag named: Mind

Yes, this part is possible, but let’s start with just gathering the values one by one, and one way to do this (with a little calculation) is:

TABLE WITHOUT ID meditation, sleep, meditation / 60, round(sleep + meditation/60, 2)
WHERE ( = "2023-05-30" OR = "2023-05-31")

Here we do a multi-select based upon the file name. This can be written in different ways using combinations of list() and contains(), and various other ways. But given two static names, this is as easy as it gets.

Then we calculate the sum for each given day, through changing the minutes into hours using division as there are 60 minutes in each hours. You could also go the other way, if you wanted that. I then present the result purely as a decimal rounded to two digits.

Summation of the values

In order to sum across the files (aka days) one needs to group the result, and one way to do this is to do GROUP BY true, this groups all the variables from the previous results into a rows list. So now we’ve got rows.sleep and rows.meditation, and although you could manipulate these arrays there’s an easier way.

Let’s us calculate the value to sum before we group it, so that we’ve got an extra value in the rows object. So we can add FLATTEN sleep + meditation/60 as daySum, and then afterwards use rows.daySum.

The query now looks like:

TABLE WITHOUT ID rows.meditation, rows.sleep, sum(rows.daySum)
FLATTEN sleep + meditation/60 as daySum
WHERE ( = "2023-05-30" OR = "2023-05-31")

And produce the output of:

And here we see your wanted value in the last column. Notice also how it says (1) at the first column indicating that this table has only one row. This shows that the dual values for meditation and sleep are actually lists within the “first” (and only) row of this table.

I said “this part is possibler” earlier on, and that is related to storing this value somewhere, and that’s not as easy at it sounds using dataview. First of all turning this query into any inline query wouldn’t be easy as inline queries are internal to the current file.

Secondly, even if you did something like: mind:: `= this.sleep + this.meditation/60`, the value of the mind field would be the actual query, and not the result of this query. So if you referred to this from another file it’d try to calculate the value again (based on new current values).

So can this be done somehow? Well, kind of, but you’d need to change into using dataviewjs, execute the query to get the result, and then tap into some other API (like Meta-edit) to store the result somewhere in the current field (like the frontmatter). And even then, since the query doesn’t change (even if you changed either of the base data files), you might run into issues related to some caching issues.

In general, it’s easier for you to have local display of the mind variant in each files, and if you want that value in another query outside of the defining file, then redo the calculation into the mind value in that query.

1 Like

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