# Better option to use fields in where clause?

**URL:** <https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308>\
**Category:** Help\
**Tags:** dataview\
**Created:** [November 25, 2022, 5:03pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308 "2022-11-25T17:03:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 25, 2022, 5:03pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308/1 "2022-11-25T17:03:37Z")

</div>

### 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:

```md
---
todoPeriod: Year/2022/11
---

```

And each note for that month has a corresponding:

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

```

The following two (simplified) queries do work:

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

dv.taskList(tasks, false)
```

````

```dataviewjs
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.

---

<div class="post-metadata">

**Author:** ![mnvwvnm](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mnvwvnm/32/9766_2.png) [@mnvwvnm](https://forum.obsidian.md/u/mnvwvnm)\
**Post date:** [November 25, 2022, 5:30pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308/2 "2022-11-25T17:30:19Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 25, 2022, 5:44pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308/3 "2022-11-25T17:44:29Z")

</div>

I’ve been contemplating doing:

```javascript
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.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 25, 2022, 7:28pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308/4 "2022-11-25T19:28:08Z")

</div>

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:

````auto
```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?

---

<div class="post-metadata">

**Author:** ![mnvwvnm](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mnvwvnm/32/9766_2.png) [@mnvwvnm](https://forum.obsidian.md/u/mnvwvnm)\
**Post date:** [November 25, 2022, 7:47pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308/5 "2022-11-25T19:47:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 25, 2022, 7:51pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308/6 "2022-11-25T19:51:09Z")

</div>

~~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.

---

<div class="post-metadata">

**Author:** ![mnvwvnm](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mnvwvnm/32/9766_2.png) [@mnvwvnm](https://forum.obsidian.md/u/mnvwvnm)\
**Post date:** [November 25, 2022, 8:01pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308/7 "2022-11-25T20:01:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [December 2, 2022, 8:02pm UTC](https://forum.obsidian.md/t/better-option-to-use-fields-in-where-clause/48308/8 "2022-12-02T20:02:00Z")

</div>

Closed on December 2, 2022, 8:02pm UTC

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