Improve my habit tracker dataviewjs query

Hello people! :smiling_face_with_three_hearts:
I created a unique habit tracker/diary. I know there are different, maybe more efficient way to create habit trackers, but I really like this way, and I like the benefits of this method. So what I do is that I create a task with a link (example: [[exercise]]) in the daily note. And in the exercise note I collect these tasks with dataviewjs.

So in the first version I can see the incompleted tasks, so I can see what left to do, and in the second version I can see the completed tasks, so I can track what I did. The only thing I wish for is to show only a few tasks, maybe 7-10, or in the last week/month and not all of them. But I have no idea how to do itā€¦ and I know dataview, but I am not familiar with dataviewjs very much. But I want to do it with dataviewjs, because it shows the date, so I can see which day I did the tasks. I also very thank you if you can send me some information about where can I find exact solutions or explanation about this problem, because I did not. I found things about dataviewjs, but not so much about filtering, sorting or limiting tasks.
Do you have any ideas?

so this is the dataviewjs query:

dv.taskList(dv.pages("").file.tasks
.where(t => t.text.includes("[[exercise]]") && t.completed)

)

(I also hope that this system can inspire someone to do something similar.)

First off, great work coming up with that query, it looks excellent for what youā€™re wanting to do.

There are a couple of ways to improve this.

Firstly, I see that you have a ā€œJournalā€ folder. Would it be safe to say that your daily entries are under there? If so, rather than having Dataview search through your whole vault, you can limit it to the daily pages.

Secondly, if we can assume that your daily notes are named like they appear to be with YYYY-MM-DD, then you can sort that descending to show your most recent dayā€™s tasks first.

Finally, you can slice the result to show only the last 7 or 10 or whatever tasks.

```dataviewjs

const tasks = dv.pages('"Journal"')
  .sort(page => page.file.name, 'desc')
  .file.tasks
  .where(t => t.text.includes("[[exercise]]") && t.completed)
  .slice(0, 10)

dv.taskList(tasks)
```

You could also sort by file created time rather than name page.file.ctime, but if you have a consistent file naming structure, then thatā€™s going to be more reliable.

2 Likes

Thank you so much! This is what I was looking for! :pray: It is perfect!

1 Like

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