Better option to use fields in where clause?

What I’m trying to do

I’m trying to find the best way to limit (multiple) dataviewjs queries depending on a field (from the frontmatter) in the current file. I want this dynamic, so as not having to change a set of 5-10 queries on these pages.

Things I have tried

My index/MOC note has the following field defined:

---
todoPeriod: Year/2022/11
---

And each note for that month has a corresponding:

---
Tags: Journal, Year/2022/11
---

The following two (simplified) queries do work:

```dataviewjs
const tasks = dv.pages('"z Journal" and #Year/2022/10').file.tasks

dv.taskList(tasks, false)
```
const tasks = dv.pages('"z Journal"')
         .where(p => p.tags?.includes(dv.current().todoPeriod))
         .file.tasks
dv.taskList(tasks, false)

However, the first one requires rewrites every time I move to a new month, and the second one I just kind of don’t like, so I’m wondering if you’ve got any suggestion to fix the last variant where it does look up the todoPeriod field, but the query looks a little nicer?

PS! These are simplified queries, and I do manipulate the tasks more to filter of statuses, sorting, and even some post-processing in some of the queries. So I do need to keep a similar structure with first doing the query into tasks, and then a presentation step, like with dv.TaskList(tasks, false) afterwards.

I don’t know if this is less “ugly” :slight_smile:

const tasks = dv.pages(`"z Journal" and #${dv.current().todoPeriod}`).file.tasks

I need to understand better this js side: why “`”; the abilities of ${}; etc.

1 Like

I’ve been contemplating doing:

const todoPeriod = dv.current().todoPeriod
const tasks = dv.pages(`"z Journal" and #${todoPeriod}`).file.tasks

It’s an extra line, but it reads a little easier (at least when you know how to read the javascript template literals, aka `… ${…} …` stuff.

Maybe this should be in another post, but I just reviewed some of the other queries in my page, and whilst most of them are indeed dataviewjs query, there are some ordinary dataview queries too.

So, how would I use the this.todoPeriod as a tag, or similar in the following query:

```dataview
TASK FROM #Year/2022/11
WHERE completed
SORT completion
```

Will I then need to resort to some WHERE file.tags.include(this.file.todoPeriod) (untested code here) shenanigans, or do I have better option here as well?

In regular dataview you can’t use “variables” or similar in FROM.
Best bet: WHERE contains(file.etags, this.todoPeriod)

The only sad thing is that it doesn’t work. Not sure how to debug this query either to see whether the file.etags is actually set or not. Or whether to use this.todoPeriod or just todoPeriod.

The metadata documentation doesn’t mention how to get the file’s metadata outside of the task. So stuff like tags refers to tags inside the task.

I’m a bit stumped by this. I know I can change everything into some dataviewJS query, but it should be possible to do this in a plain DQL-query, as well.

Update: I’m a bit of an idiot. I changed to todoPeriod: #Year/2022/11, which failed. With the original todoPeriod: Year/2022/11, your solution, @mnvwvnm with contains(file.etags, this.todoPeriod) does indeed work.

Tasks queries in DQL are confusing because they work at same time in both levels: page/file level and tasks level. That’s why we need to pay attention about a potential conflict with field keys: tags is one example.

1 Like

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