Dataview: List related notes grouped by tags

Hey dataview experts, I am kind of desparate…

I am trying to produce a dataview table showing me all notes that share at least one tag with my current note - but grouped by tags.

Let’s say I have a note (the one where the dataview table is located) which contains 3 tags (#Tag1, #Tag2 and #Tag3/any-subtag). The end result shoúld be something like this:

Tag Notes
#Tag1 * Note 1
* Note 4
#Tag2 * Note 3
#Tag3/any-subtag * Note 1
* Note 3

The following code kind of works:

FROM "folder"
WHERE any(econtains(file.tags,this.file.tags))
FLATTEN file.tags AS Tag
WHERE any(econtains(Tag,this.file.tags))

BUT it also lists (in the first column) #Tag1xyz, #Tag1/subtag.

If I change the the code to

FLATTEN: this.file.etags AS Tag

Then only my 3 tags are shown - but for every tag a complete list of notes is listed that shares at least one of my 3 tags (means: under #Tag1, a note is listed that only shares #Tag2, for example…)

Despite several days of searching, trial and error I haven’t found out why… Is there anybody who could give me a hint what is going on or how I have to change my code in order to see the expected result?

Thanks in advance for any help!

For anyone else trying to do this: @mnvwvnm (who else? :laughing: ) was able to solve the problem - the correct dataview code should look like this:

TABLE WITHOUT ID Tag, map(rows, (r) => link(, r.title)) AS Notes
FROM "folder"
FLATTEN file.tags as Tag
WHERE !contains(file.path,this.file.path) AND econtains(this.file.tags, Tag)

Just one more question:

I just realized that the !contains(file.path,this.file.path) does not work in combination with flatten (or, maybe, it’s also the GROUP BY) - does anybody know why or how to adapt the command?

Yep, after the group command you only access to the fields with the prefix rows.
But in your case, if you want to exclude the current file (WHERE !contains(file.path,this.file.path) means «exclude this file») do that before any other complex command… (because after the FROM command order matters).
So, maybe:

TABLE WITHOUT ID Tag, map(rows, (r) => link(, r.title)) AS Notes
FROM "folder"
WHERE file.path != this.file.path
FLATTEN file.tags as Tag
WHERE econtains(this.file.tags, Tag)
1 Like

Great, thanks!

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