Can I use the Dataview to COUNT the number of tags within a note?

With the purpose of organising my writing, I’d like to query a list of notes with a count of different types of tags within each note. This would provide me a snapshot of the type and amount of work that needs to be done in each note, and overall to complete the writing project.

I’m new to Dataview and haven’t tried any code yet, but hopefully this explanation provides enough context. I’d also be curious to read about different approached developed around similar use cases.

enjoy & thanks for reading

What I’m trying to do

Hi @paoloap I don’t see an example of what you’re wanting for a final output, but the Dataview plugin should do what you’re looking for.

If you want a table of pages with the counts of tags on each page you can do the following:

```dataview
TABLE
		length(tags) as numtags
	, "#" + join(tags, ", #") as tags
FROM
		#docker or #kubernetes 
```

Which will give output similar to the following:

image

If on the other hand you’re wanting a table of tags and the number of pages that they are on, then you need a little more involved dataview query:

```dataview
TABLE
		length(rows.file.name) as numfiles
	, join(rows.file.link, ", ") as files
FROM
		#docker or #kubernetes 
flatten
		file.tags as tag
group by
		tag
```

This will give you something like the following:

image

Let us know if you have any questions.

3 Likes

Hey Dug, thanks a lot for taking the time, it is exactly what I was looking for.

I could implement the 2nd solution straight away but something is going on with the 1st one. The snippet could correctly filter the note but fails to count the number of tags.

The syntax seems correct to me, I’m sending a screenshot to help you debug what’s going on.

Can you show how your tags are set up?

For me, I have the following frontmatter:

---
tags: [ "kubernetes", "docker" ]
…
---

Since your dataview query is searching on tag #important I would assume that there is at least one tag on the page. In testing I was able to get similar results as you if I use an inline tag. Based on that, I have an updated query for you to try:

```dataview
TABLE
    length(file.tags) as numtags
  , join(file.tags, ", ") as tags
FROM
    #docker or #kubernetes 
```

This appears to work as expected for me with tags in either the frontmatter or inline in your note. Doing this also removes the need to append the # on your tags to make them searchable.

It looks like tags just looks at the frontmatter, where file.tags looks at the entire document.

Thank you for teaching me something new by making me try new things. That helped in making my understanding of the tool and plugin better. Seems I have much left to learn about things. :smile:

1 Like

I think the issue is in the difference between “tags” and “file.tags”:

  • “tags” is a field (used, for example, in yaml) for tags;
  • “file.tags” is the implicit field for tags

Try this:

```dataview
TABLE length(file.tags) as numtags, join(file.tags) as tags
FROM "your-folder"
WHERE file.tags
SORT length(file.tags) DESC
```
4 Likes

this worked as well, thanks!

if anybody is learning… believe it’s me. You stepped up my Obsidian game considerably :pray:

I’m excited by what this plugin makes possible and went through the documentation so many times, but I feel I’m missing some preliminary knowledge needed to fully understand the queries and what’s possible.

Can you direct me to some useful resource where I can learn?

1 Like

What I’ve learned is from reading posts here and playing around. The docs are a good place to start, but I don’t feel that they give enough examples to understand how best to utilize thing to their fullest potential. If I come across any good sites with examples I’ll send you a DM.

thanks, I assume that Dataview is built upon Java? I may be totally wrong but maybe that would be a good place to start

Dataview is written in TypeScript which is a superset of javascript.

You can find the code for the plugin on github.

1 Like

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