Automatically sum up numbers within a note

What I’m trying to do

I want to automatically sum up estimates for each task of a project (person days) to a whole project estimate. Tasks are captured as a bullet list within a note, day estimates noted next to them. Project estimate on top of the task list should show total sum.

Things I have tried

Checked dataview docs.

Have you tried to use Dataview Inline fields to store the number of days a task could take ?

You should then be able to query these and sum them, I think :thinking:

If bracket syntax [estimate:: 10] for inline metadata is used inside tasks:

# My project (estimate: `=sum(this.file.tasks.duration)`) 
- [ ] task 1 [duration::5]
- [ ] task 2 [duration::4]
- [ ] task 3 [duration::10]

The query is not very dynamic. You need to place cursor to it to refresh. In general the dataview inline metadata syntax is not very useful since you can characterize items with separate notes and corresponding metadata. You just write [[note<enter><cmd+enter> to create notes quickly. Option+Enter will open note to current tab. There are Navigate back and Navigate forward commands to navigate between notes in current tab. These commands are included in the tab title bar.

If completely keyboard‑driven approach is desired when specifying metadata for new note, you can write YAML inside pairs of --- in source mode or you can use Add file property command in live preview. Both of these are extremely powerful way to describe metadata for the created note. Using Auto pair markdown syntax setting writing --- in the first line will create YAML‑block automatically in source mode or properties input view in live preview. If Navigate back has keyboard shortcut then above describes world’s most powerful way to describe in‑text metadata. After navigate back your cursor is placed where it was previously and then you just continue writing your original note. That is absolutely fantastic!

Thanks for the inline dataview solution! Can I maybe retrigger the recalculation of the sum by hotkey?
Moving each task to an individual note is not an option as the estimate might contain 30 tasks or. More…

I investigated two Dataview commands accessible using the command palette but they do not work with my query in case of m2 mac sonoma 14.2.1.

You could get better responses if you provided some examples of your markup, as otherwise we can only answer in generic terms.

I’m seeing (at least) two different alternatives for such a use case, where each task holds a duration inline field:

  • Make a local presentation of the sum for that particular file. Then redo the summation logic on a project level. ( Note that if you use just an inline query to calculate the local sum, this can’t be used for the project level sum since a field with a query stores the query, not the result )
  • In addition to doing the local sum, add logic to store that intermediate sum into the properties, and use that intermediate sum for project sums

Due to complexity and potential cache issues with local query updates, I believe the most reliable solution would be to recalculate the sum at the project level, with possibly a local sum not stored in a field but just presented to give the local sum. (Note that this latter part would in some cases not be updated before you re-run the queries (which could be achieved going back/forth in page-history, or reloading the note, or switching between edit modes, or triggering a rebuild view command)).

I tested that my query updates after switching to other note and then going back. However running a script that presses forward and back is not going to help since tab history is not preserved when Obsidian is closed or when switching to other workspace. Using QuickAdd macros is not going to help since Obsidian doesn’t offer a command to open an existing note. You can use other plugin to implement note opening and then build a macro

  1. switch to note X
  2. navigate back

You can however use QuickAdd to execute the following command:

 await app.workspace.activeLeaf.rebuildView()

Which will trigger the rerun of the queries on the current window. Then you wouldn’t need to fumble about going back/forth in the page history, or re-opening the file, or …

So don’t be so quick on turning down a perfectly viable solution, which I’ve used for quite some time in various contexts when testing queries in my test vault.

Thanks. I referred to using QuickAdd macros without writing code. Using Javascript can offer nice solution as you showed. If I understand correctly app.workspace is part of general Obsidian API, although QuickAdd offers its own scripting API as well.

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