Task AND Group by?!

Were you aware that we’re actually able to use GROUP BY on TASK queries using dataview? I’ve recently written some queries using GROUP BY for the LIST and TABLE query, and noticed that changes the “first” column from the default file link, into whatever you group by. This got me thinking, what will happen if I do the same to a TASK query? Will it even work, or just do something strange?!

So I set out to test this, and it worked just beautifully, so take a look at the following code:


- [x] Also done 📅 2023-01-31
- [ ] Should have been done 📅 2023-02-01
- [ ] To do 📅 2023-12-31
- [/] Working on this one 📅 2023-04-01
- [x] Already done

## GROUP BY Status
```dataview
TASK
WHERE file.name = this.file.name
GROUP BY {
  " ": "Still to be done", 
  "/": "In progress",
  "x": "Finished tasks" }[status]
```


## GROUP BY due date

```dataview
TASK
FLATTEN
  choice(due < date(today), "pre",
    choice(due > date(today), "post", "now")) as D
WHERE file.name = this.file.name AND !completed
GROUP BY 
 { "now": "01 Due today",
   "pre": "02 Due before today",
   "post": "03 Past due already" }[D] as dueGroup
SORT dueGroup 
```

Which produces the following output for me:

Notice both how it’s able to group on the various conditions, but also how the latter query doesn’t include the “today” group since it doesn’t hold any tasks, so it automatically hides the header (and no-item-list) if there is no tasks in that group.

I find this rather nice, and just wanted to share it with you guys. I think this opens up a lot of options related to both ordinary task presentation like I’ve focused on here, but also even more uses if one uses other task statuses (or decorations as I like to call them).

I reckon it can of also be used for whatever field you have in your tasks, and can think of as natural to group by. I’m thinking tags, or linked files, or texts, or just about anything related to the task. It’s most likely potential for usage in a GROUP BY clause like the ones I’ve shown above.

(PS! The queries above limits the search to the current file, using the WHERE file.name = this.file.name, this to keep the example self contained. This can of course be exchanged for whatever criteria you’re using to gather up your tasks)

4 Likes