Dynamic task query with date from frontmatter or templater

What I’m trying to do

I’m trying to create a view in my weekly journal that has tasks completed in that week. Ideally, this would use the file name to get the week number and the first and last day of that week, but I’ve added frontmatter with start_date and end_date for the week. Then in my tasks or dataview queries (I’ve tried both, I can’t seem to use the field that is tracking the done date of the task. I’m using the option in tasks to append the done date in the task, so it is there but I can’t seem to figure out how to access the field. Some attempts below:

Things I have tried

This works, but the dates are hardcoded, which isn’t great.

```tasks
done after 2022-07-03
done before 2022-07-09
  1. (Seemed like the best way):
```tasks
done after tp.frontmatter.start_date
done before tp.frontmatter.end_date

Result: “Tasks query: do not understand done date”

  1. (maybe it was the formatting?):
```tasks
done after <% tp.frontmatter.start_date %>
done before <% tp.frontmatter.end_date %>

Result: “Tasks query: do not understand done date”

  1. (ok, maybe I’ll use dataview, but I can’t figure out the field name for the done-date):
```dataview
TASK
WHERE completed AND 
done >= this.start_date AND 
done <= this.end_date

Result: “Dataview: No results to show for task query” (if I stop at WHERE completed, then I do get everything that is completed)

  1. Various other tries…
  • Using this.start_date instead of tp.frontmatter.start_date
  • Using <%+ notation for templater
  • several different fields for dataview (done, completion-date, done-date, completed-at)

Ok, I found a solution but I’m still curious if there is an answer to the above that allows for dynamic queries.
My workaround is to update the template for weekly journals to use <% tp.date.weekday(“YYYY-MM-DD”, 0, tp.file.title, “gggg-[W]ww”) %> instead of the frontmatter start_date and <% tp.date.weekday(“YYYY-MM-DD”, 6, tp.file.title, “gggg-[W]ww”) %> instead of the frontmatter end_date. This then populates the file with the date value, which works correctly.

I would not have been able to guess this one either! You got much closer in your guesses than I would have. According to the dataview documentation (also scroll up from this link to see the emoji shorthands):

completion: The date a task was completed; set by [completion:: ...] or shorthand syntax.

Something @wealthychef discovered a while ago is that there needs to be a couple seconds of delay between when frontmatter information is added to a file and when Obsidian gets that information into its cache where plugins can find it. If you were setting your frontmatter in the same template insertion as your task query, you might have run into a timing issue. If your start_date and end_date in frontmatter were already set, I’m not sure why <% tp.frontmatter.start_date %> would not have worked the same way as this solution.

Once you have inserted your template and the <% ... %> gets processed and transformed into a specific value (date in this case), there is no longer any connection between the frontmatter value and the tasks query. I think you can make a dataview query that will change when you change the start_date in the frontmatter, but I do not think tasks plugin queries support that.

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