WHAT:
A dataview query that allows one to display files along with the last subtag of a tagging hierarchy
WHY:
I have a tagging hierarchy for my notes which is #science/{topic}/{subtopic} and I want to be able to display my files (e.g. those in {topic}) organised by {subtopic} without having to manually separate all subtopics.
HOW:
Based on the excellent write-ups from @holroy about nested tags here and here, i’ve come up with a solution to my problem which may be of interest to others.
In this example, I’m using the test file structure from the first article cited above
dataview
TABLE string(slice(split(string(flat(substring(rows.Tags,1))),"/"),-1)) as "Domain" /*lots of trial and error to get this ugly hack working */
FROM #f51705 /* replace this with whatever source you want
FLATTEN array(filter(file.etags,(t)=>
startswith(t, "#aa") )) as Tags /* replace #aa with whatever top level tag structure you need - I use #science */
WHERE length(Tags) > 0 /* filter only files with the given tags */
GROUP BY file.link as "Title"
DISCLAIMER: there is probably a simpler, cleaner way to do this, but I’m not a programmer and I’ve already spent way too much time procrastinating researching this as it is ![:smiley: :smiley:](https://forum.obsidian.md/images/emoji/apple/smiley.png?v=12)
1 Like
Is it likely that your files have multiple tags matching for a given #science
or topic? If not I don’t quite understand why you’ve used that expression on the first line in combination with the GROUP BY
statement.
As I don’t understand that section, I’m just going to give you a slight variation which might accomplish some of the same output:
```dataview
TABLE tag, TopicAndSubtopic
FLATTEN array(filter(file.etags,(t)=>
startswith(t, "#aa") )) as Tags // replace #aa with whatever top level tag structure you need - I use #science */
WHERE length(Tags) > 0 // filter only files with the given tags */
FLATTEN Tags as tag
FLATTEN join(slice(split(tag, "/"), 1), "/") as TopicAndSubtopic
WHERE TopicAndSubtopic
SORT TopicAndSubtopic
```
Here is the output of your query combined with the same criteria in the query above:
Thanks for taking a look! Your solution is clearly much cleaner.
The first line was simply a way for me to get the last topic of any chain (e.g #science/mathematics/topology gives me topology, and #science/archeology gives me archaeology)
Ideally, but i’m not sure how to get that i’d like it organized like this:
archaeology
line 1
line 2
mathematics
line 4 … topology
line 5 … topology
line 6 … calculus
I’m going to take your example and play with it. Thanks !