Queries for task management

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

Hi, is it possible to for any of the queries in your OP to generate a table view of all my tasks with columns for project, deadline, priority, etc? Then can I filter and sort this table by the column values? Thanks in advance!

This is partially possible and depends on what you are looking for exactly.
For example the query under the header

Generates a table. See the :view part of the query.

Please post some example data and desired result and I’ll have a look!
Do be aware that you will not be able to filter and sort the table by the column names though. The query will just give you a static result.

Coming to simply queries:

Hi, this has been such a useful resource. Thank you!

I am starting with your “Tasks without a specific page link” and modified it a bit. I have breadcrumbs as false but I still end up getting the references for the tasks that I am pulling. Here is the code that I have

#+BEGIN_QUERY
{:title [:h3 "Tasks without page reference" ]
 :query [:find (pull ?b [*])
   :where
     [?p :block/name "habit-tracker"] ; name is always lowercase
     [?b :block/marker "TODO"]
     (not [?b :block/scheduled])
     (not [?b :block/deadline])
     (not [?b :block/refs ?p]) ; we cannot use not until we have specified the variables used in it
 ]
   :table-view? false
   :breadcrumb-show? false
}
#+END_QUERY

Can you please help to avoid having all these references, so that I can just a list of things?

You need to add

:result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/created-at])) result))

Just after

:table-view? False

2 Likes

To add to @Federico_Frosini answer, any :result-transform works for this.
So something as simple as :result-transform (fn [result] result) also works.

3 Likes

Here’s the example I have in mind - I am wondering if I can get columns for project, priority, deadline, and scheduled?

Query:

#+BEGIN_QUERY
   {:title "🟠 SLIPPING"
  :query [:find (pull ?b [*])
          :in $ ?start ?today
          :where
          (task ?b #{"NOW" "LATER" "TODO" "DOING"})
          (between ?b ?start ?today)]
  :inputs [:-7d :today]
  :result-transform (fn [result]
                      (sort-by (fn [h]
                                 (get h :block/created-at)) result))
  :collapsed? true}
#+END_QUERY

Post 0.9.2 update, all my queries have gone back to show references and they are no more a list of TODO things. Did others also face similar issues?

I believe this is a solution for the issue: :result-transform and grouping of results · Issue #6641 · logseq/logseq · GitHub
Which a lot of people have asked for / complained about. (myself included lol, even though I often used result-transform to get rid of it)
There should be a way to remove the grouping, but I’m unaware if this is now available or not.
Created a feature request topic for it:

2 Likes

Thanks for the info.
I have problem also with :collapsed? Function.
Do you have any idea and solution?

Thanks

From what I read about it, seems that’s an actual bug. So would need a bug report I guess.

ok. I hope the fix it quickly.
Everyone is talking about it even in discord. :sweat_smile:

Tasks that do or do not repeat with specific tag

1 Like

That thread is a goldmine! Thank you so much @Siferiax I wonder if you could help me out. I managed to put together a query that looks for “next actions” as per the GTD terminology and keep only these “next actions” related to projects with deadlines. I just can’t seem to sort the list by :block/deadline of these ancestor blocks in the :result-transform section as the “next actions” don’t really inherit the deadlines themselves from the ancestor tasks… Here’s my query so far:

#+BEGIN_QUERY
{:title [:h2 "NEXT ACTIONS of projects w/ deadlines"]
:query [:find (pull ?b [*])
        :in $ % 
        :where
        [?b :block/marker ?marker]
        [(contains? #{"LATER" "NOW"} ?marker)]
        ;; descendant is true when ?descendant is an descendant of b

        (not [?descendant :block/parent ?b]
             [?descendant :block/marker ?amarker]
             [(contains? #{"LATER" "NOW"} ?amarker)])

(ancestor ?b ?ancestor)
[?ancestor :block/deadline ?d]
]


:inputs
   
  [[

    [(ancestor ?b ?ancestor)
				[?b :block/parent ?ancestor]]
	
    [(ancestor ?b ?ancestor)
	            [?child :block/parent ?ancestor]
	            (ancestor ?b ?child)]
]]

:result-transform  (fn [result]
                            (sort-by  
(juxt
(fn [d] (get d :block/deadline)) 
(fn [h] (get-in h [:block/properties :time]))
) < result)
)
:collapsed? false
}
#+END_QUERY#

We need to associate that deadline property with the result you get.
This happens in the :find, :keys and most importantly :result-transform of the below query.

#+BEGIN_QUERY
{:title [:h2 "NEXT ACTIONS of projects w/ deadlines"]
 :query [:find (pull ?b [*]) ?d
   :keys block deadline
   :in $ % 
   :where
     [?b :block/marker ?marker]
     [(contains? #{"LATER" "NOW"} ?marker)]
     ;; descendant is true when ?descendant is an descendant of b
     (not 
       [?descendant :block/parent ?b]
       [?descendant :block/marker ?amarker]
       [(contains? #{"LATER" "NOW"} ?amarker)]
     )
     (ancestor ?b ?ancestor)
     [?ancestor :block/deadline ?d]
 ]
 :inputs [ [
   [(ancestor ?b ?ancestor)
     [?b :block/parent ?ancestor]
   ]
   [(ancestor ?b ?ancestor)
     [?child :block/parent ?ancestor]
     (ancestor ?b ?child)
   ]
 ] ]
 :result-transform (fn [result]
   (sort-by  
     (juxt
       (fn [d] (get-in d [:block/properties :deadline])) 
       (fn [h] (get-in h [:block/properties :time]))
     )
     < 
; the below associates the deadline key from the query result with the :block/properties attribute from the block key. Which we use above in the sort.
     (map 
       (fn [m]
         (update (:block m) :block/properties
           (fn [u] (assoc u :deadline (get-in m [:deadline]) ) )
         )
       ) 
       result 
     )
   )
 )
 :collapsed? false
}
#+END_QUERY

Things are broken in version 0.9.2, but older versions should work correctly.
Let me know if you encounter any issues!

1 Like

@Siferiax - Sorry to bother you, but I just wanted to follow up on this in case you missed it! With table view, the only columns I see are “block” and “page” but is there any way to see more columns like “deadline”, “priority”, etc.?

Yes I had that on my backlog, but a bunch of other things came on my plate at the same time and I sort of lost track of it. Thanks for the reminder!
I put it off as it is not a simple answer :slight_smile: but definitely should be possible with some magic trickery!
Table view in default will only show block, page and any used properties, not other values. However we can build our own tables in advanced queries!

Here’s the whole thing:

#+BEGIN_QUERY
{:title "🟠 SLIPPING"
 :query [:find (pull ?b [*])
   :in $ ?start ?today
   :where
     (task ?b #{"NOW" "LATER" "TODO" "DOING"})
     (between ?b ?start ?today)
 ]
 :inputs [:-7d :today]
 :result-transform (fn [result] (sort-by (fn [h] (get h :block/created-at)) result ) )
 :view (fn [rows] [:table 
   [:thead [:tr 
     [:th "Page"]
     [:th "Task"]
     [:th "Priority"]
     [:th "Deadline"]
   ]] 
   [:tbody (for 
     [r rows
       :let [content (str (get r :block/content))]
       :let [firstline (first (str/split-lines content))] ;this will show only the first line of the task.
     ]
     [:tr
       [:td [:a {:href (str "#/page/" (clojure.string/replace (get-in r [:block/page :block/original-name]) "/" "%2F"))}
 (get-in r [:block/page :block/original-name])] ] ;make a workable link to the page, even when it is in a namespace
       [:td [:a {:href (str "#/page/" (get-in r [:block/uuid]))} firstline] ] ;link to the block and show the first line
       [:td (get r :block/priority) ] ;get the priority from the results
       [:td (get r :block/deadline)] ;get the deadline from the results
     ]
   )]
 ])
 :collapsed? false
}
#+END_QUERY

You can expand it with any block attributes available basically.

2 Likes