Dataview - getting value from inline-key through dvjs

Intro
I’ve got my Daily Notes set up with an inline key “Month” which auto-populates through Templater to [[2024-02]] (for february 2024).

I’ve also got some sort of habit tracker setup with toggles, which generates a boolean in the Properties. For example “habit_core_workout: true”

I’m looking for a way to get the total number of times the habit is “true” in a certain month in that month’s review


Setup
Daily Reviews in “Reviews/00 Days” with file-name as YYYY-MM-DD
Monthly Reviews in "Reviews/02 Months with file-name as YYYY-MM
In Daily Reviews an Inline key “Month:: [[YYYY-MM]]”


What I’ve tried so far

  • I’ve succeeded in getting the current file-name (and link) through
    $= dv.current().file.link (and $= dv.current().file.name)
  • I’ve succeeded in getting the number of files in “Reviews/00 Days” through
    $= dv.pages('"Reviews/00 Days"').length
  • I’ve succeed in getting the number of true habits in the folder “Reviews/00 Days” through $= dv.pages('"Reviews/00 Days"').where(p => p.habit_core_workout == true).length

Somehow I can’t get the total number of days in this month, let alone the number of days in this mont the habit is ‘true’. I’ve tried $= dv.pages('"Reviews/00 Days"').where(p => p.Month == dv.current().file.link).length with no results.


Verification done
To check if the type of value I’m looking for is the same, I’ve also tried $= dv.current().file.link == dv.current().Month and $= dv.current().file.name == dv.current().Month but that both results to ‘false’


Question
Can anyone help me get the following to work:
$= dv.pages('"Reviews/00 Days"').where(p => p.Month == dv.current().file.link && p.habit_core_workout == true).length

Could you show how this looks in your final notes? Is it Month: [[2024-02]] or Month: "[[2024-02]]" ? One of these is an array, and one is a link… This has a lot of impact on your queries later on.

If it’s correctly formatted, you might also want to take a look on this post:

Comparing links needs to use aLink.equals(anotherLink) :slight_smile:

It’s set-up as Month:: [[YYYY-MM]], so not an array


When I try to view the file-info via dv.paragraph(dv.current()), I see the inline value for Month (as a link)

But when I query for it, I get nothing:

const m = dv.current().file.Month 
dv.paragraph(m)

Try the following in a file of its own:

---
Tags: f76763
month_array: [[2024-03]]
month_link: "[[2024-03]]"
---
questionUrl:: http://forum.obsidian.md/t//76763

## All fields
`$= const tmp = dv.current(); delete tmp.file; dv.span(tmp) `

## A given field

Month array: `= this.month_array` (`= typeof(this.month_array) `)

Month link: `= this.month_link` (`= typeof(this.month_link) `)

And you’ll see the difference. In reading mode this displays as follows for me:

Notice how the month_array in the properties section displays as a question mark, since this notation is not recognized and it is interpreted as an array of arrays. This is further shown in the type of display at the end for a given field, and with the “double list” bullet points in the “All fields” section.

The month_link on the other hand is shown as a proper link everywhere, and its type is a link, as expected.


This also has effect when you want to access these through dataviewjs, as in the first variant you’d need to do something like `$= dv.current().month_array[0][0]` to get to the month text, instead of doing `$= dv.current().month_link` to get the full link. Also note that in the first variant dataview will intervene and understand that value as a date. Try adding the following to the note above:

## From dataviewjs

month_array[0][0]: `$= dv.current().month_array[0][0])`

month_link:`$= dv.current().month_link`

You should get an output like:
image


All in all, change to the format used for the month_link, and you’ll see your queries will start working nicely, since we’re now dealing with a link. You might although need to do something like dv.current().file.link.equals(p.Month) to compare two links against each other.


On a side note, you could also consider doing dateformats on file.day of your daily notes to match the current file name (of a monthly file) to achieve similar effects. In other words, using something like p.file.day.toFormat(“yyyy-MM”) == dv.current().file.name in your monthly note. :smiley:

Thanks again for your reply!

I don’t think it’s a issue concerning YAML or arrays. It has to do with the inline value (I guess)

I’ve used both your code snippets to create a separate file. I’ve added an extra inline field called month_inline and checked for its type; it’s also a link.

I understand that I must use hyphens inside YAML to create a link. For an inline value that seems not to be the case.

---
Tags: f76763
month_array: [[2024-03]]
month_link: "[[2024-03]]"
---
questionUrl:: http://forum.obsidian.md/t//76763

month_inline:: [[2024-03]]

## All fields
`$= const tmp = dv.current(); delete tmp.file; dv.span(tmp) `

## A given field

Month array: `= this.month_array` (`= typeof(this.month_array) `)

Month link: `= this.month_link` (`= typeof(this.month_link) `)

Month inline: `= this.month_inline` (`= typeof(this.month_inline) `)

When I check in my own environment, both are considered an object (dvjs).

This  file link: `$= dv.current().file.link` (`$= typeof(dv.current().file.link) `)

versus

Month (2024-01-05): `$= dv.pages('"Reviews/00 Days/2024-01-05.md"').Month` (`$= typeof(dv.pages('"Reviews/00 Days/2024-01-05.md"').Month) `)

Result:
This file link: 2024-01 (object)
Month (2024-01-05): 2024-01 (object)

In both the “2024-01” is clickable and referring to the correct file.

In addition to this,

`$= dv.pages('"Reviews/00 Days/2024-01-05.md"').Month == dv.current().file.link`

returns False

There is difference in how you define it as a property versus an inline fields. Properties needs the quotes, but inline fields don’t. In addition the displayed version can display as links even though they’re not considered as such by Dataview.

You also need to use dv.func.typeof() to get the “correct” type for these fields, since it is more precise in our context than the native javascript typeof operator.

Finally, to compare links you need to do something like:

`$= dv.current().file.link.equals(dv.pages('"Reviews/00 Days/2024-01-05.md"').Month)`

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