Queries for task management

Sharing my newly complex today query :smiling_face_with_three_hearts:

Tasks for today based on scheduled or deadline

What I wanted was the ability to move a task forward in time through scheduled, but without moving the deadline. However I didn’t want to see these in my tasks for today query anymore. (tasks for today are all those with scheduled or deadline in the past or on today)

#+BEGIN_QUERY
{:title [:h3 " ⚡ Vandaag"]
 :query [:find (pull ?b [*])
   :in $ ?day
   :where
     [?b :block/marker ?m]
     [(contains? #{"TODO" "DOING"} ?m)]
     [?b :block/page ?p]
     (or-join [?b ?d]
       [?b :block/scheduled ?d] ; scheduled on ?d
       (and 
         [?b :block/deadline ?d] ; or deadline on ?d
         (not [?b :block/scheduled]) ; and not scheduled
       )
       (and
         [?b :block/deadline ?d] ; or deadline on ?d
         [?b :block/scheduled ?t] ; and scheduled on ?t
         [(<= ?t ?d)] ; and ?t is less or equal to ?d
       )
     )
     [(<= ?d ?day)] ; ?d is less or equal to ?day, which is set to today.
     [?b :block/parent ?pb] ; the parent of the task
     (not [?pb :block/marker "TODO"]) ; the parent is not a task. I.e. filter out subtasks.
 ]
 :result-transform (fn [result] (sort-by 
   (juxt 
     (fn [r] (get r :block/scheduled 99999999)) ; sort by scheduled, which is substituted by 99999999 when it doesn't exist, meaning unscheduled tasks end up at the bottom instead of the top.
     (fn [r] (get r :block/deadline)) ; and then sort by deadline.
   ) 
   (map 
     (fn [m] (assoc m :block/collapsed? true)) ; collapse the result blocks
     result
   )
 ) )
 :table-view? false
 :breadcrumb-show? false
 :inputs [:today]
}
#+END_QUERY
1 Like