# Group Tasks by page Tags using Dataview

**URL:** <https://forum.obsidian.md/t/group-tasks-by-page-tags-using-dataview/47354>\
**Category:** Help\
**Tags:** dataview\
**Created:** [November 10, 2022, 1:19am UTC](https://forum.obsidian.md/t/group-tasks-by-page-tags-using-dataview/47354 "2022-11-10T01:19:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mawiseman](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mawiseman/32/31364_2.png) [@mawiseman](https://forum.obsidian.md/u/mawiseman)\
**Post date:** [November 10, 2022, 1:19am UTC](https://forum.obsidian.md/t/group-tasks-by-page-tags-using-dataview/47354/1 "2022-11-10T01:19:29Z")

</div>

I am trying to Group my Tasks by tags

### Things I have tried

To reproduce… create 2 pages with tasks and tags

```auto
---
tags: [team, person1]
---
- [x] Task 1
- [] Task 2

```

```auto
---
tags: [team, person2]
---
- [x] Task 3
- [] Task 4

```

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

```auto
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)

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

```

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

```auto
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

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

```

---

<div class="post-metadata">

**Author:** ![mnvwvnm](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mnvwvnm/32/9766_2.png) [@mnvwvnm](https://forum.obsidian.md/u/mnvwvnm)\
**Post date:** [November 10, 2022, 2:03am UTC](https://forum.obsidian.md/t/group-tasks-by-page-tags-using-dataview/47354/2 "2022-11-10T02:03:17Z")

</div>

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:

```nohighlight
- [] 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:

```sql
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:

```auto
(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:

```javascript
`$=dv.span(dv.current())`

```

---

<div class="post-metadata">

**Author:** ![mnvwvnm](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mnvwvnm/32/9766_2.png) [@mnvwvnm](https://forum.obsidian.md/u/mnvwvnm)\
**Post date:** [November 10, 2022, 2:11am UTC](https://forum.obsidian.md/t/group-tasks-by-page-tags-using-dataview/47354/3 "2022-11-10T02:11:33Z")

</div>

But you can try this:

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

```

---

<div class="post-metadata">

**Author:** ![mawiseman](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mawiseman/32/31364_2.png) [@mawiseman](https://forum.obsidian.md/u/mawiseman)\
**Post date:** [November 13, 2022, 10:32pm UTC](https://forum.obsidian.md/t/group-tasks-by-page-tags-using-dataview/47354/4 "2022-11-13T22:32:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [November 20, 2022, 10:33pm UTC](https://forum.obsidian.md/t/group-tasks-by-page-tags-using-dataview/47354/5 "2022-11-20T22:33:23Z")

</div>

Closed on November 20, 2022, 10:33pm UTC

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