Summing Minutes in a Base

Hi.

I have property in my yyyy-mm-dd daily notes that tracks how long I read each day, with the duration stored in minutes, such as readtime: 188m

Using Obsidian 1.10.0, is there a way to create a formula in a base that can sum all the minutes from the filtered daily notes?

Would be ideal to sum the minutes and then produce a total in days, hours, and minutes. Obsidian won’t sum the readtime property because of the m stored in the values. :thinking:

Thanks for any guidance. :smiling_cat_with_heart_eyes:

Given that readtime is a text (not a list) property, secondary-click your readtime column header then select Summarize... > Add summary and enter this formula:

values.filter(duration(value)).reduce(number(value) + acc, 0)

That should give you the total minutes.

You can decide what to do with it from there, such as append “m” or “minutes”, wrap the whole formula in duration() to use Bases’ built-in rounding, or parse it into days-hours-minutes format.

2 Likes

Thank you, once again, for the help.

I looked at your post at crazy o’clock my time and wanted to wake up properly before replying.

Of course, values.filter(duration(value)).reduce(number(value) + acc, 0) works perfectly. I scoured the help pages for bases for reduce but couldn’t find any mention of it. So I am pretty bamboozled by the bardic cipher, and I don’t even know what acc is or what it is doing. You presumably can apply coding knowledge to bases that I can only dream of. So I am very grateful for your help, but also in the dark about the what and why of it all.

I tried wrapping the code as you mentioned, but so far I haven’t stumbled on the right way to wrap it. I started off with:

duration(values.filter(duration(value)).reduce(number(value) + acc, 0))

… but that didn’t work. I am trundling my way through other permutations and will have a eureka moment at some point. For any other users with similar summing needs, I’ll post my eureka when I stumble on it.

Know I am too much on a neophyte to be able to answer any question you might ever have, but that won’t stop me reading the forum in the hope of one day being able to repay at least a portion of your many kindnesses. As hopelessly optimistic as a fool can be. :joy_cat:

1 Like

FWIW, this also gives the same total when summing minutes:

values.filter(value).reduce(number(value) + acc, 0)

Don’t know why it works, nor if there are any pros or cons for this code, but leaving it here as a reference point.

bases

Since Bases language is JavaScript-like, I just looked up whether JS has a reduce function. It does. The docs there were enough to reveal how to use it Bases.

(I do keep meaning to learn JS one of these days!)

duration

This ought to work for duration():

duration(values.filter(duration(value)).reduce(number(value) + acc, 0) + "m")

filters

  • .filter(value)

    This one filters for the presence of any value, which is enough to prevent reduce() from choking on absent and null values.

  • .filter(duration(value))

    This one filters out values that aren’t durations. It forestalls an empty/nothing result should something other than a duration find its way into your readtime.

To see the difference in action, you could set readtime: a in one of your daily notes. When using the first filter, the “a” breaks the summarize formula, returning something to the effect of “Empty” (I forget what phrase Bases displays). The second filter skips the “a” and allows the formula to carry on.

To make it more useful, we could have it check not for any duration but for specifically a number followed by “m”. But if your readtime data are clean, then the filter you posted is perfect, no changes needed.

1 Like

Thank you for the solution and the explanation—squirrelled away for future reference in my vault, of course.

I doubt I would have happened upon adding + "m" to the line unless I had had a boundless amount of time, working as I do à la the infinite monkey theorem. I don’t know what it is doing and why it works, but now the yearly totals read beautifully as days, for example, 25 days. Think I might stick with the original count of minutes for the time being until I can IMT a way to get something more expressive, along the lines of 3 weeks, 3 days, 8 hours, 28 minutes. Nice to have, but not critical in any way.

The main thing for me is that with group by and sum being added to bases in the latest beta, I can now do everything I want to do in my vault without needing Dataview, which I will be bidding an affectionate adieu to either later today or tomorrow. Love Dataview (and would have migrated to Datacore but for Bases), but Bases load a lot faster and will presumably have better long-term support as a core plugin.

Thanks again.

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