Filtering dataview query by tag

I have this query:

LIST WITHOUT ID InLinks
WHERE file.path=this.file.path
FLATTEN file.inlinks AS InLinks
SORT InLinks.file.name desc

It works perfectly. But I’d like to filter the results by tag. But it breaks when i try to do that. I get no results.
Things I have tried:

LIST WITHOUT ID InLinks
**from #tag**
WHERE file.path=this.file.path
FLATTEN file.inlinks AS InLinks
SORT InLinks.file.name desc
LIST WITHOUT ID InLinks
WHERE file.path=this.file.path **and #tag**
FLATTEN file.inlinks AS InLinks
SORT InLinks.file.name desc

Could you show some examples of the notes you would like to see included in these lists?

The queries you’re presenting is limited to the current document, is that intentional? You might want to consider shifting the focus of your queries to notes having an outlink to the current document, with the given tags.

Or else you need to potensially pull the tags from the inlink’ed note to query on them? The use case is a little convoluted due to the limitation on your queries for the current file.

Remember that file.path is the complete path including the note name. It’s not just the folder it resides in. For that you’d rather user file.folder or similar constructs (like startswith or some regex).

I forgot to mention I already tried changing file.path=this.file.path to file=this.file.
I’m using the path part because I was advised that I’ll get better performance.

I have one note for each client, and one note for each time I serve the client.
I also have some other notes and links to each client. I’d like to make separate queries for the service and the other kinds of notes, because some clients These differences are already appropriatedly marked and can be fetched either by tags or fields in the frontmatter.

I’m not code savvy, so I don’t understand the difference from querying outlinks and inlinks. I think I can try that.

OK, let’s analyse this query:

  • LIST WITHOUT ID – You don’t care about the file to be listed
  • InLinks – You only want the InLinks
  • WHERE file.path = this.file.path – And the source for your entire query is only the current file
  • FLATTEN file.inlinks as InLinks – Split the inlinks into their separate entries, and store that as InLinks
  • SORT InLinks.file.name desc – Sort the result according to the name

In essence, given this query is in Note A, you’ll only get the list of the files linking to Note A, and your main focus is on the current file of Note A.

The latter means that if you just check for tags, you’re referring to the tags of Note A. You’re not checking the files which link in towards Note A, for the sake of the example say you’ve got Note B and Note C having links to Note A. And every one of these files just have the tag corresponding to their title, so Note A has #tagA, Note B has #tagB, and so on…

So doing “FROM #tagB” or WHERE ... AND "#tagB" (which is illegal syntax) would try to match the tag from another file whilst you’ve already removed the other files from the equations by doing file.path = this.file.path.

Doing FROM #tagA could be sort of a replacement for the file.path = this.file.path, since it would allow for other files having that tag to be listed. But having both is kind of redundant.

So let’s assume you want to list all files which links to the current file, and which has a given tag. Using syntax from here: Sources - Dataview , the simplest variant would then be:

```dataview
LIST 
FROM [[]] AND #tagB
SORT file.name desc
```

Hope this helps clarify some of the mystery around queries, and that it’s not just even more confusing.

1 Like

It did work.
Very simpler syntax too… thanks!

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