Group Tasks by page Tags using Dataview

I am trying to Group my Tasks by tags

Things I have tried

To reproduce… create 2 pages with tasks and tags

---
tags: [team, person1]
---
- [x] Task 1
- [ ] Task 2
---
tags: [team, person2]
---
- [x] Task 3
- [ ] Task 4

I have tried using the following dataview to Group those tasks based on tag

TASK 
WHERE !completed
GROUP BY tags

I have also tried changing the tagging definition from tags to tag
I have also tried changing the tag definition to tags: [team, person1] to tags: team, person1

Results when using tags (always in 1 group)

(2)
- [ ] Task 2
- [ ] Task 3

Results when using tag (always grouped by combining all the tags on the page)

team, person1 (1)
- [ ] Task 2
team, person2 (1)
- [ ] Task 4

What I’m trying to do

I would like to see a list of tasks grouped by individual tags

team (2)
  - [ ] Task 2
  - [ ] Task 4
person1 (1)
  - [ ] Task 2
person2 (1)
  - [ ] Task 4

Some conflicts and misreading of what’s the structure of the metadata.

  • When you create some tag in the content - #tag - it becomes a “real” tag to Obsidian and to dataview (an implicit field - file.tags).
  • When in frontmatter you write tag: [one, two] or tags: [one, two] it happens two things:
    • Obsidian (and dataview) read the values as real tags (#one and #two) and for dataview they’re target by file.tags (or file.etgs - see docs for understand the difference) - and attention: file.tags are always an array, even if only one value… even if you write tags: one, two
    • But for dataview tag: [one, two] it’s also a normal field with the key tag (or tags) - that’s why if you write tags: one, two it’ll be read as an array if targeted as file.tags and a string - “one, two” - if targeted as tags
  • As normal tags they’re metadata at page level, not at task level or lists level (that is another thing). As tags field it’s also a page level metadata.

Topics above are intended to explain the difference between targeting tags or file.tags. And as file.tags they’re page level. So, if you ask for tasks to be grouped by a page level (parent level to tasks), there’s no way to you achieve what you want in that way… because the file.tags is a list of tags, not a flattened values (maybe with another query, with the flatten command…)

A second point is related with the conflict you create when you’re using a taks query with the key tags. Why? because task query is a little confusing… it works in two levels at same time: at page level and at tasks level (a file.tasks sub-level of page level).

And the conflict exists here: inside tasks level there’s an implicit field called “tags”, i.e., a field for tags inside each task text. For example:

- [ ] this is a task
- [ ] this is another one with a #tag in the text

in this case the “#tag” is a page level tag but also a task level tag. It’s possible to filter tasks with a specific tag inside:

TASK
WHERE contains(tags, "#tag")

This to say: when you write in your query GROUP BY tags it try to group by the tags inside the task level, not by the field you create in the frontmatter (a conflict because the same key field). In your case, because they don’t exist the result is:

(2)
- [ ] Task 2
- [ ] Task 3

In a specific note (use one of the examples you write), place this inline js query (enable js queries in settings > dataview) to check the metadata structure for dataview:

`$=dv.span(dv.current())`
1 Like

But you can try this:

TASK 
WHERE !completed
FLATTEN file.etags AS T
GROUP BY T

Thanks for the detailed response @mnvwvnm

I had tried various combinations of FLATTEN as well, but didn’t have the context you provided above until now

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.