How many times has this hashtag (or other word) been used in this note

What I’m trying to do

Hi everyone,
I am trying to create an inline dataview query that provides a number of the number of times #tag has been used in the current note.

Things I have tried

I have tried to figure it out how to modify this dataview query but haven’t had any joy:

I have tried replacing dv.pages() with dv.current() however, it just gives me “-” as a result, no matter how many times the tag appears in the note.

Any help would be greatly appreciated, as I have only just started trying to figure out how to use dataview!

1 Like

Counting occurrences of a word/tag within a document isn’t really the strength of dataview. To achieve that you need to read the full content of the file, aka you need to use dataviewjs and read the content and traverse it looking for your word/tag manually.

An embedded search will show the number of matches in each file, and you can restrict the search to 1 file.

#exampleTag file:"Example note"

You could make a template so you don’t have to type the current note’s name.

#exampleTag file:"{{title}}"

Thanks for getting back to me, I was using dataviewjs, sorry I forgot to mention that, is there a way of doing it though where it is automated?

Thanks for the help, that provides a table of all the results, do you know of a way of modifying it so that only the number is displayed? It is commpletely functional for what I want to do, thank you!

What exactly are you looking for, and in which set of files? What do your current query attempts look like?

So ideally I would like the output to look similar to this:

at the moment I have used the code that you sent me and this is what it looks like:

with this code:

#trx file:"Patient CP"

The only file I would like involved is the one that the code is on, your previous message showed me how to put it into a template and that appears to be working

Tap or click the down-pointing arrowhead to the left of the file name and it will hide the result previews.

1 Like

@InnesC if you haven’t already figured it out, this works:

Total number of treatments: `$= const file = app.workspace.getActiveFile(); const tags = app.metadataCache.getFileCache(file)?.tags?.map(a => a.tag); const length = tags?.filter((b) => (b === "#trx"))?.length; dv.span(length)`

(I don’t know why, but dv.current() doesn’t work)

The only downside of this method is that you need to click on the code to get it to update.

1 Like

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