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.

1 Like

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

Q12_test_data

  • 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 (Hidden)
    - [x] do this task
    - [x] and that task
- [[Note P]] links to a project_P2 (Hidden)
    - do this task
    - and that task 
- [[Note P]] links to a project_P3 (Hidden)
    -  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 (Hidden)
    - [x] do this task
    - [x] and that task
- [[Note P]] links to a project_P2 (Hidden)
    - do this task
    - and that task 
- [[Note P]] links to a project_P3 (Hidden)
    -  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]]

Q12_test_data02

  • filename : dic_20110601
---
date: 2011-06-01
---
#Project/P06

- [ ] Task 1 - Uncompleted
    - Item 1.1
        - Item 1.1.1
            - [ ] Task 1.1.1.1 - Uncompleted
            - [x] Task 1.1.1.2 - Completed
        - [x] Task 1.1.2 -  - Completed and not fullyCompleted
            - [ ] Task 1.1.2.1 - Uncompleted
            - Item 1.1.2.2
    - [x] Task 1.2 - Completed
    - Item 1.3
        - Item 1.3.1
        - [x] Task 1.3.2 - Completed shown for [[Note P]]



  • filename : dic_20110606
---
date: 2011-06-06
---
#Project/P06

- [x] Task 2 - Completed and not fullyCompleted
    - Item 2.1
        - Item 2.1.1
        - [ ] Task 2.1.2 - Uncompleted
            - [x] Task 2.1.2.1 - Completed and not fullyCompleted
                - Item 2.1.2.1.1
                - [x] Task 2.1.2.1.2  - Completed and not fullyCompleted
                    - [ ]  Task 2.1.2.1.2.1 - Uncompleted



  • filename : dic_20110611
---
date: 2011-06-11
---
#Project/P06

- [x] Task 3 - Completed and not fullyCompleted
    - [ ] Task 3.1 - Uncompleted
    - [x] Task 3.2 Completed and not fullyCompleted 
        - [ ]  Task 3.2.1 - Uncompleted
- [x] Task 4 - Completed



  • filename : dic_20110701
---
date: 2011-07-01
---
#Project/P07

- [ ] 1. ==This should be shown== 
	- 1.1 This should be hidden??? shown for Justdoitcc
	- [x] 1.2 This should be hidden???  shown for [[Note P]]
	- [ ] 1.3 ==This should be shown==
		- 1.3.1 This should be hidden??? shown for Justdoitcc
		- [x] 1.3.2 ==This should be shown, as it contains uncompleted sub-tasks==
			- 1.3.2.1 This should be hidden??? shown for Justdoitcc
			- [ ] 1.3.2.2 ==This should be shown==
			- [x] 1.3.2.3 This should be hidden
		- [ ] 1.3.3 ==This should be shown==
		- 1.4 ==This should be shown, as it contains uncompleted sub-tasks==
			- 1.4.1 This should be hidden??? shown for Justdoitcc
			- [ ] 1.4.2 ==This should be shown==
			- [x] 1.4.3 This should be hidden



  • filename : dic_20110801
---
date: 2011-08-01
---
#Project/P08

- [[My Test File]] 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
- [ ] also remember to do this task for [[My Test File]]
    - 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
- 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 [[My Test File]]




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 fullyCompleted

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:
  • Inline DQL: get file.path
    path==[[dic_20110501]].file.path
    //=>“100_Project/02_dataview/Q12_Tasks/Q12_test_data/05/dic_20110501.md”
FROM "100_Project/02_dataview/Q12_Tasks/Q12_test_data/05/dic_20110501.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 ""

New Fields

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


C6.text
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.fullyCompleted)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.fullyCompleted)), (cc) =>
                      map(filter(cc.children, (e) => !(e.task AND e.fullyCompleted)), (ccc) =>
                          map(filter(ccc.children, (e) => !(e.task AND e.fullyCompleted)), (cccc) =>
                              map(filter(cccc.children, (e) => !(e.task AND e.fullyCompleted)), (ccccc) =>
                                  map(filter(ccccc.children, (e) => !(e.task AND e.fullyCompleted)), (cccccc) =>
                                      choice(cccccc.task AND !cccccc.completed, "- [ ] " + cccccc.text, 
                                          choice(cccccc.task AND cccccc.completed AND !cccccc.fullyCompleted, "- [x] " + 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 fullyCompleted 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,  
          choice(L.task AND L.completed AND !L.fullyCompleted, "- [x] " + L.text,  "- " + L.text)) 
      AS "milestones_or_tasks",
    
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.fullyCompleted)), 
          (LC) => choice(LC.task AND !LC.completed, "- [ ] " + LC.text, 
                      choice(LC.task AND LC.completed AND !LC.fullyCompleted, "- [x] " + LC.text, LC.text))
      ) 
      AS "C1.text",
     
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.fullyCompleted)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.fullyCompleted)), (cc) =>
                      choice(cc.task AND !cc.completed, "- [ ] " + cc.text, 
                          choice(cc.task AND cc.completed AND !cc.fullyCompleted, "- [x] " + cc.text, cc.text))	
                  )
      )
      AS "C2.text", 

      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.fullyCompleted)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.fullyCompleted)), (cc) =>
                      map(filter(cc.children, (e) => !(e.task AND e.fullyCompleted)), (ccc) =>
                          choice(ccc.task AND !ccc.completed, "- [ ] " + ccc.text, 
                              choice(ccc.task AND ccc.completed AND !ccc.fullyCompleted, "- [x] " + ccc.text, ccc.text))
                      )                      
                  )
      )
      AS "C3.text", 
      
      map(filter(L.children, (c) => (!c.task) OR (c.task AND !c.fullyCompleted)),
          (LC) =>
                  map(filter(LC.children, (e) => !(e.task AND e.fullyCompleted)), (cc) =>
                      map(filter(cc.children, (e) => !(e.task AND e.fullyCompleted)), (ccc) =>
                          map(filter(ccc.children, (e) => !(e.task AND e.fullyCompleted)), (cccc) =>
                              choice(cccc.task AND !cccc.completed, "- [ ] " + cccc.text, 
                                  choice(cccc.task AND cccc.completed AND !cccc.fullyCompleted, "- [x] " + 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 !L.parent
WHERE (L.task and !L.fullyCompleted) 
      OR (
          !L.task AND length(L.children) > 0 AND 
          filter(L.children, (c) => (c.task AND !c.fullyCompleted))
         ) 
WHERE (length(L.outlinks) > 0 AND length(L.children) > 0)
      OR length(L.children.outlinks) > 0
SORT file.name ASC


```

Screenshots(DQL10)


DVJS10_use_fLists_hide_fullyCompleted_tasks_and_display_uncompleted_tasks

Summary

Main DVJS

Code Name Data type Group By Purposes Remark
DVJS10_use_fLists_hide_fullyCompleted_tasks
_and_display_uncompleted_tasks
list items or task items with outlinks or not yes 1.To hide fullyCompleted sub-tasks
2.To show uncompleted sub-tasks under list items and completed (sub)-tasks
3.To show items that contain note links
4.To list items with the original structure
5.To show any list items if b_SHOW_ANY_LIST_ITEMS
6.To show the amount of incomplete tasks in a note
1.It supports different notes.

2.It supports different headers.

3.It supports the current note.

Developers:
1.jqhoogland@GiHub
2.wenlzhang@GiHub

Notes

Summary_notes

searching for the current file ONLY

Original Example
// M11. define pages: gather all relevant pages
// #####################################################################
//let pages = dv.current();
let pages = dv
    .pages('"100_Project/02_dataview/Q12_Tasks/Q12_test_data" and #Project')
    .where((page) => dv.func.contains(page.file.name, "dic_")); 
Your Example
// M11. define pages: gather all relevant pages
// #####################################################################
let pages = dv.current();
// let pages = dv
//     .pages('"100_Project/02_dataview/Q12_Tasks/Q12_test_data" and #Project')
//     .where((page) => dv.func.contains(page.file.name, "dic_")); 

To show any list items:

  • If you would like to look at any list items, the following is an example.
Original Example
// M10. define b_SHOW_ANY_LIST_ITEMS: 
// true : To show any list items 
// false: To hide any list items (default)
// Q: What do list items mean?
// A1: list items with no children
// A2: list items with list items that have no children
// A3: list items with children that contain fullyCompleted tasks in any generation
// A4: list items with list items whose children that contain fullyCompleted tasks in any generation
// #####################################################################
let b_SHOW_ANY_LIST_ITEMS = false;
The Example
// M10. define b_SHOW_ANY_LIST_ITEMS: 
// true : To show any list items 
// false: To hide any list items (default)
// Q: What do list items mean?
// A1: list items with no children
// A2: list items with list items that have no children
// A3: list items with children that contain fullyCompleted tasks in any generation
// A4: list items with list items whose children that contain fullyCompleted tasks in any generation
// #####################################################################
let b_SHOW_ANY_LIST_ITEMS = true;

code DVJS10_use_fLists_hide_fullyCompleted_tasks_and_display_uncompleted_tasks

Summary_code
title: DVJS10_use_fLists_hide_fullyCompleted_tasks_and_display_uncompleted_tasks => 1.To hide fullyCompleted sub-tasks 2.To show uncompleted sub-tasks under list items and completed (sub)-tasks 3.To show items that contain note links 4.To list items with the original structure 5.To show any list items if b_SHOW_ANY_LIST_ITEMS 6.To show the amount of incomplete tasks in a note
collapse: close
icon: 
color: 
```dataviewjs
// M10. define b_SHOW_ANY_LIST_ITEMS: 
// false: To hide any list items (default)
// true : To show any list items 
// Q: What do list items mean?
// A1: list items with no children
// A2: list items with list items that have no children
// A3: list items with children that contain fullyCompleted tasks in any generation
// A4: list items with list items whose children that contain fullyCompleted tasks in any generation
// #####################################################################
let b_SHOW_ANY_LIST_ITEMS = false;


// M11. define pages: gather all relevant pages
// #####################################################################
//let pages = dv.current();
let pages = dv
    .pages('"100_Project/02_dataview/Q12_Tasks/Q12_test_data02" and #Project')
    .where((page) => dv.func.contains(page.file.name, "dic_"));    


// M21. define function for expanding search to children:
// #####################################################################
const taskAny = function taskAny(t, f) {
    if (f(t)) {
        return true;
    }
    for (let sub of t.children) {
        if (taskAny(sub, f)) {
            return true;
        }
    }
    return false;
};


// M23. define function for hiding children: if b_SHOW_ANY_LIST_ITEMS
// Case_01: Show list items
// Case_02: Show tasks that are not fullyCompleted
// Case_03: Show items that contain note links
// #####################################################################
let hide_fullyCompleted_Subtasks;
if (b_SHOW_ANY_LIST_ITEMS) {
    hide_fullyCompleted_Subtasks = (t) => ({
        ...t,
        children: t.children
            .filter((st) =>
                taskAny(
                    st,
                    (st) =>
                        !st.task ||
                        (st.task && !st.fullyCompleted) ||
                        st.outlinks.length > 0
                )
            )
            .map(hide_fullyCompleted_Subtasks),
    });
}

// M25. define function for hiding children: if !b_SHOW_ANY_LIST_ITEMS
// Case_02: Show tasks that are not fullyCompleted
// Case_03: Show items that contain note links
// #####################################################################
if (!b_SHOW_ANY_LIST_ITEMS) {
    hide_fullyCompleted_Subtasks = (t) => ({
        ...t,
        children: t.children
            .filter((st) =>
                taskAny(
                    st,
                    (st) =>
                        (st.task && !st.fullyCompleted) ||
                        st.outlinks.length > 0
                )
            )
            .map(hide_fullyCompleted_Subtasks),
    });
}

// M31. defined tasks_or_lists : 
// Case_10: Show tasks which are not fullyCompleted
// Case_20: Show list items whose children contain tasks that are not fullyCompleted
// #####################################################################
let tasks_or_lists = pages.file.lists
    .where(
        (L) =>
            (L.task && !L.fullyCompleted) ||
            (!L.task &&
                L.children.length > 0 &&
                L.children.filter((c) => c.task && !c.fullyCompleted))
    );


// M33. Hide completed Subtasks:
// #####################################################################
tasks_or_lists.values = tasks_or_lists.values.map(hide_fullyCompleted_Subtasks);


// M81. tasks_or_lists: groupBy t.path
// #####################################################################
// t.path   : "100_Project/02_dataview/Q12_Tasks/Q12_test_data02/08/dic_20110801.md"
// group.key: "100_Project/02_dataview/Q12_Tasks/Q12_test_data02/08/dic_20110801.md"
tasks_or_lists = tasks_or_lists.groupBy((t) => t.path).sort((group) => group.key, "asc");


// M83. output tasks_or_lists:
// #####################################################################
for (let group of tasks_or_lists) {
    let amount_of_incomplete_tasks_In_Group = group.rows
        .filter((t) => t.task && !t.completed)
        .length.toString();
    let s_file_name = dv.page(group.key).file.name;
    let s_heading = "<hr>" + "(" + s_file_name + ": " ;
    s_heading = s_heading + amount_of_incomplete_tasks_In_Group + " incomplete)";
    dv.header(3, s_heading);
    dv.taskList(group.rows, true);
}


```

Screenshots(DVJS10)

let b_SHOW_ANY_LIST_ITEMS = false; // (default)
Part1/5 : To hide any list items

Part2/5 : To hide any list items

Part3/5 : To hide any list items

Part4/5 : To hide any list items

Part5/5 : To hide any list items

let b_SHOW_ANY_LIST_ITEMS = true;
Part1/5 : To show any list items

Part2/5 : To show any list items

Part3/5 : To show any list items

20220817_DVJS10_output_show_list_items_a03

Part4/5 : To show any list items

Part5/5 : To show any list items


Reference

Summary

1 Like

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?

Oh, anytime!

DVJS10_use_fLists_hide_fullyCompleted_tasks_and_display_uncompleted_tasks

  • 2022-08-17 added DVJS

DQL10_flatten_fLists_to_display_tasks_children_and_grandchildren

  • 2022-08-16 updated DQL and Notes
Summary_Notes

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:
  • Inline DQL: get file.path
    path==[[dic_20110501]].file.path
    //=>“100_Project/02_dataview/Q12_Tasks/Q12_test_data/05/dic_20110501.md”
FROM "100_Project/02_dataview/Q12_Tasks/Q12_test_data/05/dic_20110501.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 ""

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