Can I get the number of per tags within notes?

What I’m trying to do

I want to get the number of per tags within notes.

I’m scraping and writing headline news every week.

And I’m using tags to organize each news.

like

(the name of this note file is “T 2024-W14”)
image

And in other notes, we use dataview to manage notes written every week.

As I said above, I would like to add the number of each tags I wrote in each note as a column.

In fact, if I search with tags within obsidian, I can see how many tags exist for each note.

So is there a way to show the number of each tag with dataview or dataviewjs?

Things I have tried

I tried referring to the following links

Official Document
Can I use the Dataview to COUNT the number of tags within a note?
How to COUNT number of times a tag appeared in notes from my Journal?
Count each tag in a single document
(and more…)

I got the results,

image

However, it wasn’t what I expected, because this is not the number per tag, it’s just the number of unique tags.

1 Like

You can do an inline query with Dataview!!

You can write
image
you have to put ` around it so it recognizes the code and remember the .length otherwise it shows the notes individually. Replace `#important’ with whatever tag you like, it outputs a number.

For example:
image

Edit: Just realized, I misunderstood what you were asking for. Sorry!

Oh, thank you for your reply.

It’s not what I wanted, but that’s a pretty good idea.
I think it may be possible to use the dataviewjs with your suggestion.

I don’t know the grammar of dataviewjs properly, so I roughly wrote it in pseudo code lol

```dataviewjs
dv.table(
    ["Number of tags"],
    for page in dv.pages("directory")
        for tag in page.tags
            page.map(p => p."tags".length)
);
```

How is it?
Doesn’t it look a bit plausible?

Is there anyone who can help me?

The ordinary approach using file.tags or file.etags will only report any tag used in a given page just once, so you’ll need to go deeper and use some variant of app.metadataCache.getFileCache().tags as illustrated in this post:

I’m a little busy a few days now, but are you able to take it from there, or do you need help with the coding?

Ok, I’ll give it a try.

I referred to the this post and used the following code,

```dataviewjs
const topicFiles = dv.pages('"Some pages"')
	.flatMap(p => {
		const tFile = app.vault.getAbstractFileByPath(p.file.path)
		const allTags = app.metadataCache.getFileCache(tFile).tags
		return allTags
	})
	.map(t => t.tag)
	.groupBy(t => t)
	.map(t => {
		return [ t.key, t.rows.length ]
	})
	
dv.table(["Title", "Numtags"], topicFiles)
```

I got this

image

and this results matches the number that comes out when searched with tags!

We’re almost there…

finally, I would like to see the calculated value for each note, rather than flattening all the notes together.

Is it possible?

1 Like

What do you mean with calculated value for each file?

As I said at the beginning, there are several tags in a single note.

image

And there are a lot of these files

This result is to count the tags in all these files together.

image

I want to see the number of tags separately for each file

(for example…)
image

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