In dataview, How can I make the tasks in a task query have links to their respective pages right next to them?

I have this query:

```dataview
TASK
WHERE regextest("^\d\d\d\d-\d\d-\d\d (January|February|March|April|May|June|July|August|September|October|November|December)(?!.+?sync)", file.name)
WHERE contains(list(" "), status) AND !contains(tags, "")
GROUP BY file.link
SORT rows[0].file.day DESC
```

Which shows tasks like this:

# 2023-05-12 May 12th 2023 Friday
- Task 1
- Task 2
# 2023-05-08 May 8th 2023 Monday
- Task 3
- Task 4

I also want to group them by tag instead of by link, I know I can change GROUP BY file.link to GROUP BY tag to do so. How can I first group them by tag, then sort all tasks with the same tag by date, and then show the links next to each task like this?

- [[2023-05-12 May 12th 2023 Friday]] #Tag1 Task 1
- [[2023-05-08 May 8th 2023 Monday]] #Tag1 Task 2
- [[2023-05-12 May 12th 2023 Friday]] #Tag2 Task 3
- [[2023-05-08 May 8th 2023 Monday]] #Tag2 Task 4

That last list you present doesn’t seem to be grouped, only sorted, but it’s still doable with a little bit of magic… :smiley:

```dataview
TASK
FLATTEN file.link + " " + text as visual
WHERE regextest("^\d\d\d\d-\d\d-\d\d (January|February|March|April|May|June|July|August|September|October|November|December)(?!.+?sync)", file.name)
WHERE contains(list(" "), status) AND !contains(tags, "")
SORT tag ASC, file.name DESC
```

This assumes that your tag already are at the start of the tasks for the visual representation of the task. It utilises a rewrite of the task text including the link at the front, and then dual sorting options.

Code untested, but I’m hoping it’ll work!

I accidentally wrote !contains(tags, "") when I meant to write contains(tags, ""), I want it to show only tasks with tags. Anyway, that worked, except the tag order isn’t working properly. It looks like this:

  • 2023-05-12 May 12th 2023 Friday #Later #Obsidian-Tasks
  • 2023-05-12 May 12th 2023 Friday #Later
  • 2023-05-11 May 11th 2023 Thursday #Obsidian-Tasks
  • 2023-05-08 May 8th 2023 Monday #Obsidian-Tasks
  • 2023-05-07 May 7th 2023 Sunday #Obsidian-Tasks
  • 2023-05-06 May 6th 2023 Saturday #Obsidian-Tasks
  • 2023-05-05 May 5th 2023 Friday #Obsidian-Tasks
  • 2023-05-05 May 5th 2023 Friday #Obsidian-Tasks
  • 2023-04-25 April 25th 2023 Tuesday #Obsidian-Tasks
  • 2023-04-23 April 23rd 2023 Sunday #Maybe
  • 2023-04-18 April 18th 2023 Tuesday #Obsidian-Tasks
  • 2023-04-18 April 18th 2023 Tuesday #Obsidian-Tasks
  • 2023-04-18 April 18th 2023 Tuesday #Obsidian-Tasks

Notice the #Maybe in the middle

If you want to check for presence of tags, use just tags…

Are your tasks empty?! Do they just have a random tag?

They’re not empty, I took the text out for sharing this, my bad!

No problem, it just looked strange, that’s all.

What’s empty though is the tag variable, we’re supposed to use tags (at least within task queries). So try the following:

```dataview
TASK
FLATTEN file.link + " " + text as visual
WHERE regextest("^\d\d\d\d-\d\d-\d\d (January|February|March|April|May|June|July|August|September|October|November|December)(?!.+?sync)", file.name)
WHERE contains(list(" "), status) AND tags
SORT tags ASC, file.name DESC
```

Thanks for the solution!

But after I asked this, I’ve been doing some reflecting. I’ve been asking these questions lately but haven’t been doing much work to try to figure it, or at least not as much as I should’ve been doing, and you’ve basically been doing work for me, what do you think?

I have glossed over Dataview’s documentation over and over again and thought that this would take too much work to put together without asking. Skimming the pages isn’t a good way to figure out how the query language works and I should look at examples of Dataview and try to thoroughly understand it. Even if Dataview becomes obsolete in the near future. Whenever it comes out, I might submit PRs for documentation to provide examples.

Do you know of any resources other than the documentation to learn the Dataview Query Language?

Part of me has thought I shouldn’t put much effort into learning the query language if Dataview will be obsolete. Back in January, I spent hours trying to figure out Dataview, specifically the DataviewJS API, but I found it was poorly documented and was upset to hear that DataCore would come out at the end of January, which it didn’t. It’s kept getting pushed back but I think it will likely come out before or by the end of 2023.

There is an example vault having loads of various queries, but in general I’m self-thaught related to Dataview. Although my background is a computer science profession, so I do know programming beforehand.

My best tip, though, is to try to get your task done by yourself as much as you can, and plunder away to get as close as you’re able to. And then ask questions.

And one way to get started is to approach any task from a general query version, and then keep narrowing it down until you’ve got what you really wanted. Related to this query in particular, tasks are kind of hard to get extra information out of, but there is a trick related using FLATTEN where one can play around a lot more.

In fact, on the way testing my latest version, I had an intermediate version like the following:

```dataview
TABLE WITHOUT ID visual, T.text, T.tags, length(T.tags)
FLATTEN file.tasks as T
FLATTEN file.link + " " + T.text as visual
WHERE contains(list(" "), T.status) AND T.tags
WHERE file.folder = this.file.folder
SORT T.tag desc, T.file.name DESC
GROUP BY T.tag
```

Here I experimented with using length(T.tags) to see if there was an issue with multiple tags when sorting, and I also ended up grouping on the tags, so if that was the case of the anomaly with the #Maybe tag.

The key point, just now, however is that instead of using a TASK query, I do a TABLE WITHOUT ID in combination with FLATTEN file.tasks as T. Now I can access all the task related information through T.*, and I can show multiple columns. This trick has proven useful when developing/refining many a (task) query.

When you’re ready to do the actual task query, you just need to remove the T. part, and look over the other bits, to ensure it actually works as a TASK query. You’ll get the hang of that part, rather quickly, hopefully.

But using TABLE queries for debugging is really helpful, as you can then output whatever you intend to use in WHERE, GROUP BY, SORT, … clauses, and see what value the query actually produces, and that your logic and variable handling is to the point and doing what you expect it to.

1 Like

Thanks for the advice! I think I’ll look back at it whenever I try to learn Dataview.

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