Advanced Dataview Query

Things I have tried

I’ve tried a variety of approaches, but I don’t know if I need to go to dataviewjs for this (I suspect I do)

What I’m trying to do

I want to count the number of tasks that appear on a page as well as tasks that link to a page.

For the first part, I just want to count the number of tasks on a page. How do I select this with dataview? I know I can select meta data, but how do I count the number of tasks from a page?

For the second part, I’ve done this before using inline meta tags. The setup is pretty simple:

- [ ] This is my todo [x::[[link to page]]]

What I am unsure about is how to count both of those.

The trick to counting is taking the length() of the array holding the tasks, most often file.tasks, but can be something else if you start grouping and playing around with the queries.

Try out these two to see whether they’re getting close to what you want:

## Counting tasks

```dataview
LIST count
FLATTEN length(file.tasks) as count
WHERE length(file.tasks) > 0
SORT count DESC
LIMIT 5
```

## Counting tasks having outlinks

```dataview
LIST length(rows.task)
FLATTEN file.tasks as task
WHERE length(task.outlinks) > 0
GROUP BY file.link
```

I added a LIMIT 5 to the first, just to limit the output a little, it can be removed if you want to, of course. In my Sandbox vault this gave the following output:
image

In the last query note that we first use FLATTEN (file.tasks) as task in order to check each task (instead of the array) for links going out of it, aka link to a page, aka length(task.outlinks) > 0, which means having more than zero links out of that particular task.

And then I re-group the rows by the file.link to get the nice output with links to the files having these tasks.

These queries should give you something to play with for a little while! :smiley:

Great stuff holroy! It’s always the simple stuff to trip me up - I was trying length (tasks) rather than calling length (file.tasks). :face_with_peeking_eye:

I get both of the queries separately but not sure if I can get them to go together. Essentially I am looking at something akin to a JOIN statement (I think) as I want a table with:

File Name | sum of tasks on page + tasks linking to that page |

So say you’ve got the Something note, you want the number of tasks listed within Something, and you want the count of tasks elsewhere linking back to Something?

And possibly in just one query? For that kind of magic, someone like @mnvwvnm needs to enter the stage! :woozy_face:

My take on that would be to use dataviewJS and build two queries, which I then would join up. My second query then would be a list of outlinked tasks, grouped by the outlink, so that I could that as the common key in the joint array later on.

Would you need the query to answer for multiple page, or just one at a time? For example, would it suffice to only work on the Something page, but the query itself could be copied to another page, and get similar results?

So say you’ve got the Something note, you want the number of tasks listed within Something, and you want the count of tasks elsewhere linking back to Something?

And possibly in just one query?

Yeah, that’s what I’m looking for. I was thinking dataviewJS as well, but I really am not strong on that at all.

The query just needs to work on on page at a time (only on Something page).

Here’s the actual use case (might have been helpful before…). I have a “Life MOC” page which links to different areas of my life (“House”, “Car”, “Kids”, etc). For each of these areas of my life, there may be tasks that need to be done.

I would love to see how many tasks are related to those areas from my “LIfe” page. I also want the flexibility to record a task in any other note and relate it back to that area.

For example, let’s say I am in a meeting with my kid’s teacher. I would have a meeting note opened up for that. I might have a task from that note which relates to my kids. I would want to record that task from the meeting note (so I can associate it there), but still see it / count it on the Life page when I am looking at the different areas of my life.