Queries for task management

A fun one today!

Tasks that don’t reference a page regardless of nesting

This will exclude any tasks that are referencing a page or that one of the parent blocks references a page. This query uses rules for recursive searching.
To change this to reference instead of don’t reference, just remove the (not and closing ) around (check-ref ?p ?b)

#+BEGIN_QUERY
{:title "🔨 TODO"
 :query [:find (pull ?b [*])
   :in $ % ; % is used to pull in the rules below.
   :where
     [?b :block/marker ?marker]
     [(contains? #{"TODO"} ?marker)]
     ;; Exclude pagename
     [?p :block/name "pagename"]
     (not (check-ref ?p ?b)) ; this calls the rules below.
 ]
 :rules [ ; whenever two rules have the same name an or is applied. I'm not exactly sure how to explain it!
   [(check-ref ?p ?b) ; definition of the rule, name and parameters.
     [?b :block/refs ?p] ; rule content to be executed
   ]
   [(check-ref ?p ?b)
     [?b :block/parent ?t]
     (check-ref ?p ?t) ; calling the rule again within this rule will make it recursive.
   ]
 ]
 :breadcrumb-show? false
 :collapsed? true
}
#+END_QUERY
3 Likes

Hi,
I added one of your queries (past deadline) and it worked.
Somehow, I am not sure why but the queries do not work anymore.

NOW works,
Past deadline does not
NEXT does not work either I believe

See the code here:

Blockquote
:default-queries
{:journals
[{:title “:hammer: NOW”
:query [:find (pull ?h [])
:in $ ?start ?today
:where
[?h :block/marker ?marker]
[(contains? #{“NOW” “DOING”} ?marker)]
[?h :block/page ?p]
[?p :block/journal? true]
[?p :block/journal-day ?d]
[(>= ?d ?start)]
[(<= ?d ?today)]]
:inputs [:14d :today]
:result-transform (fn [result]
(sort-by (fn [h]
(get h :block/priority “Z”)) result))
:collapsed? false}
{:title [:h3 “:fire: Past scheduled”]
:query [:find (pull ?b [
])
:in $ ?day ; ?day is the name for the first value in inputs further down.
:where
[?b :block/marker “TODO”] ; Using TODO straight in the clause because I want marker to be a specific value.
[?b :block/scheduled ?d] ; the block ?b has attribute scheduled with value ?d
[(< ?d ?day)] ; the value ?d is smaller than the value ?day
]
:inputs [:today] ; use the Logseq dynamic variable :today as input for this query (gives today’s date as yyyymmdd format)
:table-view? false}
{:title “:date: NEXT”
:query [:find (pull ?h [*])
:in $ ?start ?next
:where
[?h :block/marker ?marker]
[(contains? #{“NOW” “LATER” “TODO”} ?marker)]
[?h :block/page ?p]
[?p :block/journal? true]
[?p :block/journal-day ?d]
[(> ?d ?start)]
[(< ?d ?next)]]
:inputs [:today :7d-after]
:collapsed? false}]}

Could you help me out, please
(not sure why the indentation is not show once posted, when I insert the code in edit mode I see the indentation

Thank you so much!! Really helped my workflow to the next level! Dankjewel :star_struck:

1 Like

For indentation to show you put the code between 3 backticks:
```
Like so.
```
To turn it into

Like so.

Anyway…
The query looks fine.
If you’re not getting a result, it could be an issue with the data.
What data do you have that you expect to be returned?
The past scheduled only shows TODO tasks with a scheduled date before today.
The Next shows only tasks on future journal dates. (Which is a really weird default query, considering journal pages are generated the day of and not in the future, but that is an aside.)

2 Likes

As you can see I have:

the standard NOW:
works fine. Shows a task that is DOING.

Your PAST DEADLINE:
Not ok. I have a task with deadline on the 14th but it doesn’t show up in today’s journal in PAST DEADLINE.

the standard NEXT:
Not sure if it works as I also have the standard SCHEDULED AND DEADLINE
when should what show up.

So my real issue is past deadline.

My ideal setup would include

NOW
the ones with status DOING

PAST DEADLINE
all tasks with deadline in the past when not DONE (this way I include also WAITING)

SCHEDULED AND DEADLINE
the ones scheduled for today and the days to come (I think default is 14?)

It is past scheduled and not past deadline :sweat_smile:
For things with a deadline in the past you have to replace the :block/scheduled with :block/deadline.
As for the not done part.
Change [?b :block/marker "TODO"] to (not [?b :block/marker "DONE"])
Or alternatively to

[?b :block/marker ?m]
(not [(contains? #{"DONE" "CANCELED"} ?m)])

To also filter out canceled tasks.

1 Like

The next query doesn’t look at scheduled or deadline. It just looks at tasks in journal pages that are into the future. Which unless you make those yourself don’t even exist.

1 Like

Ah! I see ! I have to admit I am still figuring out the task management.
So scheduled would be planned where deadline it needs to be done latest at that day right?
Actually I would even prefer to see both in that same query based on the date. So basically it would be a “Past date” or “overdue” regardless of scheduled or deadlines. Both need immediate review.

Thanks for your help. Really appreciate it

1 Like

Tasks past either scheduled or deadline date

#+BEGIN_QUERY
{:title [:h3 "🔥 Tasks past due"]
 :query [:find (pull ?b [*])
 :in $ ?day
 :where
   [?b :block/marker ?m]
   (not [(contains? #{"DONE" "CANCELED"} ?m)])
   (or
     [?b :block/scheduled ?d]  ; Either the scheduled value
     [?b :block/deadline ?d] ; or the deadline value
    )
    [(< ?d ?day)] ; the value is in the past
 ]
 :inputs [:today]
 :table-view? false
}
#+END_QUERY
3 Likes

it works! thanks a lot. I it also helped me realise to pay more attention on how to use scheduled and deadline too.

By the way, the default query NEXT shows what ? I do not see upcoming deadlines either.
I am not sure what that query does.

Again, thank you so much for all your help,
Being a non-coder it is very helpful, and also interesting

figured out my own question. The default queries are:

NOW - Show me only tasks that are “NOW” or “DOING” that were created on a journal page with a date in the last two weeks.

NEXT - Show me only tasks that are “NOW” or “LATER” or “TODO” that were created on a journal page with a date in the next week. (makes no sense, as it does not look at DEADLINE or SCHEDULE, but at tasks in a future journal day)

Built in Scheduled & Deadline - Show me tasks which have a “scheduled” or “deadline” date of today.

2 Likes

Recursively search for tasks that are tagged or have their parent tagged with current page.

Combining “Tasks by current page & it’s aliases” with the recursive search from “Tasks that don’t reference a page regardless of nesting”

#+BEGIN_QUERY
{:title ["Query by page & alias"]
 :query [:find (pull ?b [*])
   :in $ ?page %
   :where
     [?b :block/marker "TODO"]
     [?p :block/name ?page]
     (or-join [?b ?p]
       (check-ref ?p ?b) 
       (and 
          [?p :block/alias ?a]
          (check-ref ?a ?b)
       )
     )
 ]
 :rules [
   [(check-ref ?p ?b)
     [?b :block/refs ?p]
   ]
   [(check-ref ?p ?b)
     [?b :block/parent ?t]
     (check-ref ?p ?t)
   ]
 ]
 :table-view? false
 :inputs [:current-page]
}
#+END_QUERY

Sample result:

4 Likes

hi,
since I have one query for all tasks for today, how can I adapt this query to exclude the tasks scheduled or deadline today:

Hi,
How can I modify this query to exclude deadline or schedule is not today:

{:title "📅 WITHIN NEXT 3 DAYS"
    :query [:find (pull ?h [*])
            :in $ ?next
            :where
            [?h :block/marker ?m]
            [(contains? #{"LATER" "TODO"} ?m)]
            (or-join [?h ?d]
              (and
                [?h :block/ref-pages ?p]
                [?p :block/journal? true]
                [?p :block/journal-day ?d])
              [?h :block/scheduled ?d]
              [?h :block/deadline ?d])
            [(< ?d ?next)]]
    :inputs [:3d-after] 
    :table-view? false
    :collapsed? true}
1 Like

You add :today as input. (:inputs [:3d-after :today] and :in $ ?next ?today)
Then an extra line with [(> ?d ?today)].

2 Likes

thanks a lot, it worked!!!

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

hi again,

actually it still shows a task with deadline today.
I tried to see how to exclude but I don’t get it.
Could you help me out?

{:title "📅  NEXT 3 DAYS, NOT TODAY"
    :query [:find (pull ?h [*])
            :in $ ?next ?today
            :where
            [?h :block/marker ?m]
            [(contains? #{"LATER" "TODO"} ?m)]
            (or-join [?h ?d]
              (and
                [?h :block/ref-pages ?p]
                [?p :block/journal? true]
                [?p :block/journal-day ?d])
              [?h :block/scheduled ?d]
              [?h :block/deadline ?d])
            [(and
              [(< ?d ?next)]
              [(> ?d ?today)])]]
    :inputs [:3d-after :today] 
    :table-view? false
    :breadcrumb-show? false
    :collapsed? true}
1 Like

Idk why you made such a weird query :joy:
Here’s the query as per my instructions and it works perfectly well.

{:title "📅  NEXT 3 DAYS, NOT TODAY"
    :query [:find (pull ?h [*])
            :in $ ?next ?today
            :where
            [?h :block/marker ?m]
            [(contains? #{"LATER" "TODO"} ?m)]
            (or-join [?h ?d]
              (and
                [?h :block/ref-pages ?p]
                [?p :block/journal? true]
                [?p :block/journal-day ?d]
              )
              [?h :block/scheduled ?d]
              [?h :block/deadline ?d]
            )
            [(< ?d ?next)]
            [(> ?d ?today)]
    ]
    :inputs [:3d-after :today]
    :table-view? false
    :breadcrumb-show? false
    :collapsed? true
}

Here it is a little bit more optimized, but shouldn’t change anything.

{:title "📅  NEXT 3 DAYS, NOT TODAY"
    :query [:find (pull ?h [*])
            :in $ ?next ?today
            :where
            [?h :block/marker ?m]
            [(contains? #{"LATER" "TODO"} ?m)]
            (or-join [?h ?d]
              (and
                [?h :block/refs ?p]
                [?p :block/journal-day ?d]
              )
              [?h :block/scheduled ?d]
              [?h :block/deadline ?d]
            )
            [(< ?d ?next)]
            [(> ?d ?today)]
    ]
    :inputs [:3d-after :today]
    :table-view? false
    :breadcrumb-show? false
    :collapsed? false
}

Both worked in my test environment on iPad version 0.8.16

1 Like

Tasks linked to pages with common tag.

1 Like

similar to Tasks for today based on scheduled or deadline, but without the deadline and just either tasks scheduled today or not scheduled at all on the current page:

1 Like