Is this possible? Assign scores by tag ocurrence, querying on scores

Im assuming that would need dataview (wich i understand the basics but im a newbie) - but i failed to find any examples that would fit this:

Im looking for some way to assign scores to notes (i presume a property), but dynamically set by the ocurrence of certain tags- in way that i could use said scores in formulas to print queries.

My initial ideia:

  • nested tags as the tags to be counted
  • a formula as property that would count +1 for each tag under the target root tag, giving the sum total

But i dont even know if properties could be dynamic like that in the first place, or if they can how to do so. Perhaps possible via advanced querying?

If that were possible it would probably be easier, but even if it isnt- would there be any other alternatives? Maybe some outside script directly on .md files? This could open so much possibilities and make some things easier for me it would be worth the trouble of setting up

Any pointers to where i might find solutions would be apreciated

PS: i have tought about doing it manually- looking at the tags and setting manual scores- but ive been doing quite a bit of appending text to notes (in Obsidian and from outside) and my goal setup will lean even more heavily on that

Unclear use case

Where do you want to see this dynamic score? Within the file, in another filer?

How are you imagening your markup to look like? Will it be one tag per file, or one tag for every “interesting” sections?

What, and in which context, do you want your nested tags to be counted?

What do you mean with “each tag under the target root tag”?

What’s doable

Having a formula as a property, seems a little sketchy. You can define a field to be a query, and as such see it’s computed value when you display that field. Not sure whether that is what you’re looking for.

You can’t re-use such a “computed query” in another query. You’ll then under most circumstances need to re-compute in that other query. What I mean here, is that if a given file calculates some score dynamically, you can’t access that value from another file, you’ll have to recalculate that score. (This is under the assumption you don’t use meta-edit (or Templater) trickery to store the value of the initial query)


In short, we need to see more of your use case, and what you expect to get out of it. Even if it’s not working (or built manually), it would be helpful to see what you’re hoping to achieve.

1 Like

Sorry for not being clear enough, thanks for trying to understand

The dynamic score could be for displaying in the file but mostly for using in queries - so my guess that wouldnt be possible, at least not with a property as formula (the query would be printed on another file, so the score wouldnt be calculated, if i understood you well)

I plan to use that in a number of ways, but the easiest way to ilustrate would be pros and cons.

Say one root tag is cons- all tags under it considered negatives. Another pros, same thing the other way around.
The dynamic score would be total of one or the other, or a Pros-Cons total (say for every pro tag +1, for every con tag -1) - different ways to slice it, same goals

What im hoping to do is being able to print a query based on said score not the tags. From ordering by score to whatever other finer criteria using it (say listing any notes under Y tag with more then 3 pros score, less then 5 negatives). Just an example.

I know i can do this manually per note- me counting how many X or Y tags and typing the score- and that was my initial plan… then i figured out appending text to notes was possible and how much better that would fit my ‘workflow’ so to speak, i get lost in tabs/windows swapping easily and appending from other sources is handy (like a chrome extension)… as im also sending tags with appends that means any typed score based on amount of tags present wouldnt be updated

Im not yet sure if such sort of score would be worth it unless i could reach this sort of ‘automation’/dynamism- i dont know if the time to review/update notes manually be smaller then any time saved by using the score in the first place…

Im also open to any ideas- im more interested in the outcomes, any aproximation to that, dataview and a ‘dynamic property’ were just the only guesses i had for reaching it

Given a file like the following:

Something [pro:: good], and something [con:: bad], also tagged with #pro/good & #con/bad.

Or even a little list:
- (pro:: Nice) #pro/nice
- (pro:: Simple) #pro/simple
- (pro:: Elegant) #pro/elegant
- (con:: Expensive) #con/expensive

Multiple:  #pro #pro #pro  #pro #pro #pro 

Multiple: [pro::] [pro::] [pro::] [pro::] [pro::] [pro::] [pro::]

Using fields: `= length(this.pro) + " pros, and " + length(this.con) + "cons" `

Listing all tags: `$= dv.span(dv.func.join(dv.current().file.etags, ", "))`

## In table format

### Using properties
```dataview
TABLE proCount, conCount
FLATTEN length(pro) as proCount
FLATTEN length(con) as conCount
WHERE pro OR con
```

### Using tags
```dataview
TABLE proCount, conCount
FROM #pro OR #con
FLATTEN length(filter(file.etags, (f) => startswith(f, "#pro"))) as proCount
FLATTEN length(filter(file.etags, (f) => startswith(f, "#con"))) as conCount
```

### Strange field length

[longField:: Just one value]

If only one definition: `= length(this.longField) `
Correction to get count of the field: `= length( choice(this.longField = "array", this.longField, list(this.longField) ))`

Put the code above in a note, and see if it makes sense. It should showcase some of the advantages and disadvantages with either method.

The output of the tables are:

Two key differences:

  • If using tags, the count will be wrong if you use the same tag, like just #pro, since the query doesn’t showcase how many times it’s listed within the file.
  • When using fields, you can attach extra values to the field. However, there might me some trickery needed if there it’s only one definition of that field in your file

In either case, you can build queries related to sub tags or values, for various filter operations. Personally, I think I would opt for the fields variant, as it’ll allow for various syntax and visual formatting depending on your desire, and it’ll allow for counting (as long as you counter the “just-one” thingy) and calculation of dynamic scores rather easily.

1 Like

It worked, incridebly well!! Thanks so much, i was afraid it would be a no go, no field formula shenanigans needed at all! :blue_heart::blue_heart::blue_heart:

I tried both methods, tag worked just as expected, but fields got 1 count wrong and i dont understand why it would do that.

I made a test note and copy pasted that same list you used as example, as it already have the same pros/cons as both fields and tags

Wich would be 3 pros 1 con. The tag query shows that count, but the field query

```dataview
TABLE proCount, conCount
FLATTEN length(pro) as proCount
FLATTEN length(con) as conCount
WHERE pro OR con

Insists on printing 3 pros 9(?) cons- and i cant figure why it would do that. Even if it were reading the entire file(not just the properties), counting both fields and tags, that wouldve returned 2 cons (and 6 pros)… 9? its a headscratcher for me.

Fields could posibly be interesting/useful in some situation (being able to count each appended con or pro, even if repeats)- but for my main usecase im more interested in weighting qualities in binaries (have, have-not) so the tags limitation with repeats wont be an issue. Solved it all

But im still curious as to what could have turned that 1 con into 9, its puzzling

Thanks a lot Holroy!

Update: i figured out the ‘length(con) as conCount’ is counting the characters instead of how many notes (in the list its ‘expensive’ 9 charcter length). But the same doesnt happen to pros.
I tested adding another con to the file and it got corrected- so i assume thats the ‘trickery needed’ with 1 definition you mentioned

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