Group Tasks by page Tags using Dataview

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