Any way to retrieve only tasks that contain a specific file with Dataview?

Does anyone know how I to craft a query to retrieve all uncompleted tasks that contain a link to the current file in the text of the task OR any tasks contained within a sub-list item containing the same link.

Here’s an example of what my notes usually contain:

In the example above I’m trying to create a DV query inside “My Test File.md” that returns all the tasks above. Ideally it would only including list items that contain a task e.g. “one last task” and not “some more text with no task” but I’d grateful just to get this working :slight_smile:

I’ve tried:
task where file.path = this.file.path and !completed

but that didn’t work.

you can start with this:

TASK
WHERE contains(text, this.file.name) AND !completed

but your first example (a list with nested tasks) isn’t listed in the results.

Topic

Summary

1.How to distinguish list items or task items from the file.lists data?
2.How to filter by a list item which contains a link?
3.How to filter by a task item which contains a link?


Test

Summary
  • dataview: v0.5.41

Input

Summary

dictionary files

  • filename : dic_20110301
---
Date: 2011-03-01
---
#Project/P03

## input
### input_m:  milestones
- 2011-03-01 Started learning Javascript  #Test/d01 [[Note J]] , [[Note K]]
- 2011-03-02 Wrote a "Hello world" script, and it worked! #Test/d02 [[Note J]]
- 2011-03-03 Learned how to fetch data from a URL #Test/d03
- 2011-03-04 Learned about Promises #Test/d04
- 2011-03-05 Learned about await/async #Test/d05[[Note K]]
- 2011-03-06 Started working with NPM libraries #Test/d06
- 2011-03-07 Published my first code to Github #Test/d07
- 2011-03-08 Updated my first code to Github


### input_t: tasks01
-   [[Note P]] links to a project_P1
    -   [x]  do this task
    -   [x]  and that task
-   [[Note P]] links to a project_P2
    -   do this task
    -   and that task 
-   [[Note P]] links to a project_P3
    -   do this task
    -   [x]  and that task
### input_t: tasks02
-   [[Note P]] links to a project
    -   [ ]  do this task
    -   [ ]  and that task
    -   [x]  and that task01
    -   here’s a regular line
        -   [ ]  one last task
            -   [ ]  c3
                -   [ ]  c4
                    -   [ ]  c5
                        -   [ ]  c6
        -   [x]  one last task01 

  • filename : dic_20110401
---
Date: 2011-04-01
---
#Project/P04

## input
### input_m:  milestones
- 2011-04-01 Started learning Javascript  #Test/d01 [[Note J]] , [[Note K]]
- 2011-04-02 Wrote a "Hello world" script, and it worked! #Test/d02 [[Note J]]
- 2011-04-03 Learned how to fetch data from a URL #Test/d03
- 2011-04-04 Learned about Promises #Test/d04
- 2011-04-05 Learned about await/async #Test/d05[[Note K]]
- 2011-04-06 Started working with NPM libraries #Test/d06
- 2011-04-07 Published my first code to Github #Test/d07
- 2011-04-08 Updated my first code to Github


### input_t: tasks01
-   [x]  [[Note P]] links to a project_P1
    -   [x]  do this task
    -   [x]  and that task
-   [x]  [[Note P]] links to a project_P2
    -   do this task
    -   and that task 
-   [x]  [[Note P]] links to a project_P3
    -   do this task
    -   [x]  and that task
### input_t: tasks02
-   [ ]  also remember to do this task for [[Note P]]
    -   some more text with no task
    -   [x]  but a peer that is a task01
    -   [ ]  but a peer that is a task
        -   [ ]  and a child with another task
        -   [x]  and a child with another task01



  • filename : dic_20110501
---
Date: 2011-05-01
---
#Project/P05

## input
### input_m:  milestones
- 2011-05-01 Started learning Javascript  #Test/d01 [[Note J]] , [[Note K]]
- 2011-05-02 Wrote a "Hello world" script, and it worked! #Test/d02 [[Note J]]
- 2011-05-03 Learned how to fetch data from a URL #Test/d03
- 2011-05-04 Learned about Promises #Test/d04
- 2011-05-05 Learned about await/async #Test/d05[[Note K]]
- 2011-05-06 Started working with NPM libraries #Test/d06
- 2011-05-07 Published my first code to Github #Test/d07
- 2011-05-08 Updated my first code to Github


### input_t: tasks01
-   [[Note P]] links to a project_P1
    -   [x]  do this task
    -   [x]  and that task
-   [[Note P]] links to a project_P2
    -   do this task
    -   and that task 
-   [[Note P]] links to a project_P3
    -   do this task
    -   [x]  and that task
### input_t: tasks02
-   last list of things to do remember to do this task
    -   [ ]  the only task I wouldn’t want to return because it doesn’t link to my test file
    -   [ ]  this is the actual last task [[Note P]]


DQL10_flatten_fLists_to_display_tasks_children_and_grandchildren

Summary

4.1. Main DQL

Code Name Data type Group By Purposes Remark
DQL10_flatten_fLists_to_display
_tasks_children_and_grandchildren
list items or task items with outlinks or not no 1.To fiter by not_tasks or by tasks which are not done

2.To filter by L.outlinks

3.To display uncompleted tasks, children and grandchildren as a table
1.It supports different notes.

2.It supports different headers.

3.It supports C1.txt to C6.txt.

Notes

Summary_notes

file.lists

dataview_v0.4.23 :
For DataviewJS users, the task and list representation has changed: file.tasks (and the new file.lists) contain
every single task (including subtasks) in the file, instead of only the root elements. You can return to previous
behavior by filtering out tasks with a non-null parent - i.e., file.tasks.where(task => !task.parent). dv.taskList
will intelligently deal with properly nesting and de-duplicating tasks, so just filter to the tasks you want to render and
the API will do the rest.

searching for the current file ONLY

to modify(M1)
Original Example
FROM "100_Project/02_dataview/Q12_Tasks/Q12_test_data" AND #Project
Your Example
FROM "100_Project/02_dataview/Q12_Tasks/example.md"
Wrong Example:
  • performance issues : Obsidian slows down quite noticeably when I add this query e.g. it locks up for a several seconds, especially when using FROM “” (my vault has ~400 files in it)
FROM ""
to modify(M2)
Original Example
WHERE file.name != this.file.name
Your Example
WHERE file.name = this.file.name
to modify(M3)
Original Example
WHERE (contains(L.outlinks, [[Note P]]) AND length(L.children) > 0) 
      OR contains(L.children.outlinks, [[Note P]])
Your Example
WHERE (contains(L.outlinks, [[My Test File]]) AND length(L.children) > 0) 
      OR contains(L.children.outlinks, [[My Test File]])

New Fields

C5.text
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.completed)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.completed)), (cc) =>
                      map(filter(cc.children, (e) => !(e.task AND e.completed)), (ccc) =>
                          map(filter(ccc.children, (e) => !(e.task AND e.completed)), (cccc) =>
                              map(filter(cccc.children, (e) => !(e.task AND e.completed)), (ccccc) =>
                                  choice(ccccc.task AND !ccccc.completed, "- [ ] " + ccccc.text, ccccc.text)
                              )                      
                          )   
                      )                      
                  )
      )
      AS "C5.text",


C6.text
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.completed)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.completed)), (cc) =>
                      map(filter(cc.children, (e) => !(e.task AND e.completed)), (ccc) =>
                          map(filter(ccc.children, (e) => !(e.task AND e.completed)), (cccc) =>
                              map(filter(cccc.children, (e) => !(e.task AND e.completed)), (ccccc) =>
                                  map(filter(ccccc.children, (e) => !(e.task AND e.completed)), (cccccc) =>
                                      choice(cccccc.task AND !cccccc.completed, "- [ ] " + cccccc.text, cccccc.text)
                                  )
                              )                      
                          )   
                      )                      
                  )
      )
      AS "C6.text",



Code DQL10_flatten_fLists_to_display_tasks_children_and_grandchildren

Summary_code
title: DQL10_flatten_fLists_to_display_tasks_children_and_grandchildren =>1.To fiter by not_tasks or by tasks which are not done 2.To filter by L.outlinks 3.To display uncompleted tasks, children and grandchildren as a table
collapse: close
icon: 
color: 
```dataview
TABLE WITHOUT ID 
      choice((L.task AND !L.completed), "- [ ] " + L.text,  "- " + L.text) 
      AS "milestones_or_tasks",
    
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.completed)), 
          (LC) => choice(LC.task AND !LC.completed, "- [ ] " + LC.text, LC.text)
      ) 
      AS "C1.text",
     
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.completed)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.completed)), (cc) =>
                      choice(cc.task AND !cc.completed, "- [ ] " + cc.text, cc.text)
                  )
      )
      AS "C2.text", 

      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.completed)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.completed)), (cc) =>
                      map(filter(cc.children, (e) => !(e.task AND e.completed)), (ccc) =>
                          choice(ccc.task AND !ccc.completed, "- [ ] " + ccc.text, ccc.text)
                      )                      
                  )
      )
      AS "C3.text", 
      
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.completed)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.completed)), (cc) =>
                      map(filter(cc.children, (e) => !(e.task AND e.completed)), (ccc) =>
                          map(filter(ccc.children, (e) => !(e.task AND e.completed)), (cccc) =>
                              choice(cccc.task AND !cccc.completed, "- [ ] " + cccc.text, cccc.text)
                          )   
                      )                      
                  )
      )
      AS "C4.text", 



          
      file.link AS "File"


FROM "100_Project/02_dataview/Q12_Tasks/Q12_test_data" AND #Project

FLATTEN file.lists AS L
FLATTEN meta(L.header).subpath AS F_subpath
FLATTEN meta(L.header).type AS F_type
WHERE file.name != this.file.name
WHERE (!L.task AND length(L.children) > 0 AND filter(L.children, (c) => (c.task AND !c.completed))) 
      OR (L.task and !L.completed) 
WHERE (contains(L.outlinks, [[Note P]]) AND length(L.children) > 0) 
      OR contains(L.children.outlinks, [[Note P]])
SORT file.name ASC


```

Screenshots(DQL10)


Thanks @mnvwvnm!

This works great, except for the nested tasks as you mentioned - any way to return those as well?

WOW, thank you @justdoitcc!! This is probably one of the most comprehensive and helpful answers I have come across (thank you for taking the time!!) :raised_hands:

Most of what you’re doing is above my head so I may be missing a lot but I can’t seem to get this to return any results.

I tried making the two edits you mentioned:

  1. Changing the the FROM to “” or even a subfolder
  2. Removing the “!” from WHERE file.name = this.file.name

Are there any other changes I should be making? I also downloaded the example files and query which work great so my guess is that it has something to do with the 2nd change?

Lastly, i’ve noticed that Obsidian slows down quite noticeably when I add this query e.g. it locks up for a several seconds, especially when using FROM "" (my vault has ~400 files in it). Did you experience any performance issues?