Query page property by date range

Hello,

I have been attempting to get a query working by following guides and googling but nothing has worked so far.

I have a yearly review at work that runs from April 1st to March 31st and as part of that I want to track courses that I have completed in that time frame. My courses have got a page property called type which is set to [[Course]] and then a date-completed property that is set to the date I completed the course.

What I would like the query to do is return all of the completed courses between April 1st 2023 and March 31st 2024 so that I can have that on my review page.

I’m not even sure this is possible but any help or nudges in the right direction would be greatly appreciated.

Thanks

What is your date property like?
It should be one of two things to make it work. Either:

  • a reference link to a journal page
  • a date in format yyyymmdd with optional, but consistent separators

It’s currently a reference to the journal page for that day. I can adapt to the best type to make a query easier though it’s certainly not set in stone.

That’s fine :slight_smile:
It’s both easy to do.

You can see the date range used behind inputs:

#+BEGIN_QUERY
{:title [:h3 "Courses this period"]
 :inputs [20230401 20240331]
 :query [:find (pull ?p [*])
  :in $ ?start ?end
  :where
   (page-property ?p :type "Course") ; case sensitive
   [?b :block/page ?p]
   [?b :block/refs ?j]
   [?j :block/original-name ?journal]
   [?j :block/journal-day ?d]
   [(<= ?start ?d ?end)]
; check that the reference we take is from the property we want
   [?b :block/properties ?prop]
   [(get ?prop :date-completed) ?end]
   [(contains? ?end ?journal)]
 ]
}
#+END_QUERY

Example result

2 Likes

Wow its as easy as that, thank you so much. Something so simple but satisfying about having the right data in the right place.

1 Like