Function Macro - A Simple Way of Aggregating Values in a Table

I’ve discovered a built-in macro today, which I find really useful.

  • Given that,
    → we have a simple query:
    {{query (and (task TODO) (property :task-time-plan))}}
    → where the task property :task-time-plan stores the number of minutes, e.g. 45, 65, 120,
  • use the following example:
      total: {{function (sum :task-time-plan)}} mins 
      i.e. {{function (sum (map (fn [x] (quot (:task-time-plan x) 60)) result))}} hours {{function (sum (map (fn [x] (mod (:task-time-plan x) 60)) result))}} mins
      for {{function (count :block)}} tasks
      which gives you {{function (average :task-time-plan)}} mins per task
    
  • to get the result:
      total: 510 mins
      i.e. 8 hours 30 mins
      for 6 tasks
      which gives you 85 mins per task
    

According to Logseq Documentation, you can use functions by typing /function in a child block under the query’s results. See the link for more examples.

This way of counting tasks or aggregating values seems simpler to me than an advanced query with Clojure code.