Creating a tag index table with Dataview?

I want to build a table with “all tags & nested tags” listed in a column, the folder on the second column & the file name on the third column.

ps: Is there a function that count the number of files with particular tag and list inside a bracket beside the tag name ?

eg:

Tag Folder File Name
file.tags(count) file.folder file.name
#123/abc(14) Areas/Finance abcd

If your “tags” are in frontmatter I know how to solve this. But with implicit field file.tags I found some limitations in using data commands “GROUP BY” and “FLATTEN” in simple dataview queries (maybe these limitations have a solution in dataviewjs, but I’m not able to do that).
Just some points:

  • your “file.name” is a list of files (if tag is used in more than one file) - a list equal to the count of files with that tag
  • if you want not a string but a link, you need to use “file.link”

Considering what I said before, I think I have found a way to do that with file.tags.
But first I need to clarify the points about “Folder” and “File Name”
For each tag you want:

a) The total (number) of the files using that tag?
b) The list of files? (It’s a list, not an ‘single’ object - a list with the files counted in file.tags)
c) The list of folders? (It’s a list, because if you have a list of files in b), then you have a list of folders - even if the files are all in the same folder)

About the table:
d) Do you want the table sorted by the total in a)?

1 Like

I just need the total number of files using that tag.

Should the tag be listed in the front matter or just #tag is enough?

Listing files & folder are easy.

TABLE 
file.folder AS "Folder"
WHERE contains(file.folder, this.file.folder)
SORT file.folder ASC

But listing all tags as a table is where it gets complicated.

Maybe I was not very clear about the issue of files and folders: was on how these elements appear in the desired table - lists of files and lists of folders for each tag.
But the best way to clarify this is advancing with hypotheses for your table.

  1. Table with “folders” and “files” presented as lists:
```dataview
TABLE WITHOUT ID (tag + "(" + length(rows.file.link) + ")") AS Tags, (rows.file.folder) AS Folder, (rows.file.link) AS Files
FROM ""
WHERE file.tags
FLATTEN file.tags AS tag
GROUP BY tag
SORT length(rows.file.link) DESC
```
  1. Table with “folders” and “files” presented as joined lists:
```dataview
TABLE WITHOUT ID (tag + "(" + length(rows.file.link) + ")") AS Tags, join(rows.file.folder) AS Folder, join(rows.file.link, ", ") AS Files
FROM ""
WHERE file.tags
FLATTEN file.tags AS tag
GROUP BY tag
SORT length(rows.file.link) DESC
```
2 Likes

YES :blush:, This Works. Thank You @mnvwvnm :1st_place_medal:.

Great!

@mnvwvnm

Is there a way to sort the items inside the “file column” each row in ascending order ?

I added sort which does not work.

SORT Files ASC

like this

TABLE WITHOUT ID (tag + "(" + length(rows.file.link) + ")") AS Tags, (rows.file.link) AS Files
SORT Files ASC
FROM "" WHERE file.tags 
FLATTEN file.tags AS tag 
GROUP BY tag SORT length(rows.file.link) DESC 

which gives error

Dataview: Unrecognized query operation 'folder'

I want rows.file.link in ascending.

Try this:

```dataview
TABLE WITHOUT ID (tag + "(" + length(rows.file.link) + ")") AS Tags, sort(rows.file.link) AS Files
FROM ""
WHERE file.tags 
FLATTEN file.tags AS tag 
GROUP BY tag
SORT length(rows.file.link) DESC
```
1 Like

I dont need folder, only 2 columns tag & notes.

TABLE WITHOUT ID (tag + "(" + length(rows.file.link) + ")") AS Tags, sort(rows.file.link) AS Files
FROM ""
WHERE file.tags 
FLATTEN file.tags AS tag 
GROUP BY tag
SORT length(rows.file.link) DESC

This :point_up: does the job but…

sort(rows.file.link) AS Files

this sorts all files in alphabetic order inside a Folder, but not across all folders.

Well, I never used some similar to this query. But I accepted the challenge as a way to learn more about the potential of Dataview.
Because many of the hypotheses are found by the “trial-error” method (not any special knowledge in code), in some cases things seems to work in my limited test vault. But, applied to a more complex vault, the issues becomes more specific.
In this particular issue, I deduce the problem is in file.link. What is a link? Is a file path… rendered with the file name. So, I deduce the function sort in file.link is applied to the file path (something like Folder1/Foucault.md, Folder2/Adorno.md > all files in Folder1 are sorted before files in Folder2).
How to overcome this limitation?
The immediate answer is use file.name instead of file.link.

sort(rows.file.name) AS Files

But (now is my turn), you want “links”, not a simple list of names.
To do that, let’s try the function link presented in the plugin documentation:

link(sort(rows.file.name)) AS Files

In conclusion, try this query and tell me if my empiric deduction works:

```dataview
TABLE WITHOUT ID (tag + "(" + length(rows.file.link) + ")") AS Tags, link(sort(rows.file.name)) AS Files
FROM ""
WHERE file.tags 
FLATTEN file.tags AS tag 
GROUP BY tag
SORT length(rows.file.link) DESC
```
```
1 Like

@mnvwvnm Awesome :ok_hand: ! That works perfectly well.

As a computer science student, I was also curious.

Now it is easy to track all the tags sorted. I use Zootelkeeper Plugin to index folders. But for tags, there is no plugin.

Thank you very much :pray: also for explaining how you got the solution. :blush:

1 Like

Great! One more dataview ‘tip’ found.

1 Like

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