Inline Query: Count Tasks in a Given Folder

So I installed Dataview literally today and am trying to figure out how to make it work for me; I tried answering this question through searching old forum posts, but still ended up stuck. (Please overexplain things and assume basic syntax can be a stumbling block.)

What I’m trying to do

I’m trying to figure out how to do an inline query (regular or JavaScript doesn’t matter) to count how many tasks are in a given folder.

(I then want to modify it to count how many tasks in a given folder are planned on a specific day, and how many are planned on a specific day and complete so that I can display these tasks and their count in my daily note.)

Things I have tried

I’ve copied and modified code a code snippet to allow me to count the tasks in a file, and it works; what I’m not managing and what would be ideal is modifying “t” so that it’s the tasks in a folder instead of the current file.

Specifically, I copied:

and removed the last bit because it wasn’t needed, to get this:

$= const t = dv.current().file.tasks; const tTotal = t.length; const tComplete = t.filter(t => t.completed).length; dv.paragraph("Tasks done: " + tComplete + " of " + tTotal)

I think the solution could involve dv.pages from trying to read through the documentation, but I have no idea how to format this properly even should my idea be correct…

This should get you the counts for all tasks and completed tasks in a specific folder.

```dataviewjs

// Replace with your folder path
const folderPath = "your_folder_path";

const pages = dv.pages(`"${folderPath}"`);

let tTotal = 0;
let tComplete = 0;

for (const page of pages) {
    if (page.file.tasks) {
        for (const task of page.file.tasks) {
            tTotal++;
            if (task.completed) tComplete++;
        }
    }
}

dv.paragraph(`Tasks done: ${tComplete} of ${tTotal}`);
```

Thank you very much! This worked and did precisely what I asked for.

I’ve modified it to give me the functionality I mentioned I wanted in brackets; this now displays how many tasks I’ve scheduled on the current day and how many out of those are completed:


const folderPath = "Tasks";

const pages = dv.pages(`"${folderPath}"`);

let tTotal = 0;
let tComplete = 0;


for (const page of pages) {
    if (page.file.tasks) {
	  let scheduled_tasks = page.file.tasks.filter(a=>(a.scheduled===dv.current().file.date));  
        for (const task of scheduled_tasks) {
            tTotal++;
            if (task.completed) tComplete++;
        }
    }
}

dv.span(`Tasks done: ${tComplete} of ${tTotal}`);

(I used dv.span instead of .paragraph because I didn’t actually want an entire paragraph, formatting-wise)

… and turns out I messed this up somehow and didn’t notice because it gave me the number of tasks without a scheduled date, which was coincidentally the same as the number of tasks scheduled today.

My attempt at troubleshooting is… not working.

Bug 1: it’s supposed to be dv.current().file.day, so far, so good.

Bug 2: … only, that now gives me 0 tasks that this applies to.

Attempting to check the scheduled dates with the code below confirms: yes, they’re the same as dv.current().file.day, yes, their object type is the same, no, no they still don’t show up.


const folderPath = "Tasks";

const pages = dv.pages(`"${folderPath}"`);

let sTotal = 0;
let sComplete = 0;

dv.paragraph("This file's day is "+dv.current().file.day +". Its type is "+ typeof dv.current().file.day)
for (const page of pages) {
    if (page.file.tasks) {
	  let scheduled_tasks = page.file.tasks;  
        for (const task of scheduled_tasks) {
            sTotal++;
            dv.paragraph("this task's date is " + task.scheduled+". Its type is "+ typeof task.scheduled)
            if (task.completed) sComplete++;
        }
    }
}

dv.span(`Tasks done: ${sComplete} of ${sTotal}`);

… while I’m clearly doing something wrong, I’ve got no clue what. Oh well, either I’ll figure it out on my own, or I’ll have to start another forum thread.

So, some vague memories about “Java Script is Weird about equalities” popped into my head very late last evening, and turns out, objects only are considered the same if they’re the exact same instance of the object…

So, lesson learnt: stringify dates before comparing them.

This now works precisely as intended and shows how many Tasks in a given folder, scheduled on this file’s date, there are, and how many of these are complete.

(Not quite sure whether I’m supposed to post in a solved question, but… idk, I didn’t want to leave this without a solution given for what I wanted this for, because hey, maybe someone else wants to do the same thing)


const folderPath = "Tasks";

const pages = dv.pages(`"${folderPath}"`);

let sTotal = 0;
let sComplete = 0;


for (const page of pages) {
    if (page.file.tasks) {
	  let scheduled_tasks = page.file.tasks.filter(a=>(JSON.stringify(a.scheduled) === JSON.stringify(dv.current().file.day)));  
        for (const task of scheduled_tasks) {
            sTotal++;
            if (task.completed) sComplete++;
        }
    }
}

dv.span(`Tasks done: ${sComplete} of ${sTotal}`);

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