Dataview: A tasks & subtasks question

Hi there :blush: !

So, I don’t really have an issue to solve (as I’ve already found a solution :innocent: ) but there’s something I would like to understand when it comes to Dataview Tasks list, tasks and subtasks in a note and I would really appreciate some clarifications from the Dataview Experts out here :blush: !


What I was trying to do:

Well, I was trying to get a Dataview tasks list containing only “parent” tasks (i.e.: containing no potential subtasks/children of any kind)

So, for the context, consider this bit of note living somewhere in my vault:

- [ ] Task 1 [project:: Project 1]
- [ ] Task 1 [project:: Project 2]
- [ ] Task 2 [project:: Project 1]
	- [ ] Task 2.1  [project:: Project 1]
	- [ ] Task 2.2  [project:: Project 1]
	- [ ] Task 2.3  [project:: Project 1]

From there, I wanted to get a Dataview tasks list for the tasks where the project key had the value "Project 1" and was solely a “parent task”.
So the result of the query should have been something like:

- [ ] Task 1 [project:: Project 1]
- [ ] Task 2 [project:: Project 1]

Trying to keep it simple, I first created a DQL:

```dataview
TASK 
WHERE project = "Project 1" AND !children
```

… which gave some results but not the ones I was expecting, especially the !children :sweat_smile:

As this did effectively return the tasks tied to "Project 1" only but instead of returning the Task 2, it returned the whole list of subtasks tied to it:

Project 1 - Tasks & Subtasks

… and this is what I don’t understand :innocent: :

Why can’t I just negate the presence of any possible subtasks in a task, in DQL ?

I mean, I tried variations of solution I found (see below) in DQL but either the query didn’t return any result at all (without errors) or the results were still not quite the expected one :woman_shrugging:

I’m really not used to query tasks lists with Dataview :see_no_evil: (and I try to avoid having to deal with subtasks/sublists), so there’s probably something I don’t know about how tasks lists work in Dataview, or something I’m missing or I’m just generally mistaken and misunderstand the general concept behind tasks list in Dataview :sweat_smile:

Hence why I’m here :blush:.


Things I done to get to my desired result:

I asked in the #Dataview channel on Obsidian Discord Server and was told that I should probably switch to DataviewJS to filter out the undesired subtasks :blush:

I tried but was still stuck :innocent:.
Thinking this would be a common Dataview use case, I searched in the Discussions of the Dataview GitHub repo and stumbled upon an answer which worked perfectly :tada: (Thank you Holroy :grin: )

So, I ended up with this DVJS query :

```dataviewjs
const parentTasks = dv.pages().file.tasks
  .filter(t => t.children = [])
  .where(p => p.project === "Project 1")

dv.taskList(parentTasks)
```

Which, even though it doesn’t return the appropriate count of tasks ( :woman_shrugging: ), at least, it displays all the tasks I wanted to see :tada: !

Project 1 - Tasks & Subtasks 2

I also guess that another way to make it work would have been to not tie any project key to the subtasks so wouldn’t have to try and exclude them from any query :innocent:

Thank you so very much in advance to anyone who could point me what I’m probably missing in all this :smile: :raised_hands:!!!

1 Like

The base list of tasks when doing the task query is any task at any level. So when you’re asking for tasks with no children belonging to the project. It thinks as follows:

  • Task 1 - Is project, and has no children, show this one
  • Task 2 - Is project, and has children, ignore this task
  • Task 2.1, 2.2 and 2.3 – Is project, and doesn’t have children, show these

A better test to actually get only parent tasks, is to check on the .parent metadata which holds a line number related to a given parent task. So try something like the following:

```dataview
TASK WHERE project = "Project 1" AND !parent
```
4 Likes

Thank you so very much :grin: !!!

That’s definitively something I didn’t know … and now it all finally makes sense :raised_hands: !

I’ll run more tests with your suggestion but I really didn’t envision the “under the hood” concept of task lists the way you’re describing it :sweat_smile: !

TIL :grin: !!! Thank you very much again Holroy :grin: !

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