Inline Query to find note between two dates

Things I have tried

$= dv.paragraph(dv.pages('').filter(p => p.file.path.includes('Calendar/Houses')).where(p => p.type == "House" && p.Date-Moved-In >= moment(tp.file.title, 'YYYY-MM-DD') && p.Date-Moved-In <= moment(tp.file.title, 'YYYY-MM-DD').file.link))

[!error]
Dataview (for inline JS query ‘dv.paragraph(dv.pages(’‘).filter(p => p.file.path.includes(‘Calendar/Houses’)).where(p => p.type == “House” && p.Date-Moved-In >= moment(tp.file.title, ‘YYYY-MM-DD’) && p.Date-Moved-In <= moment(tp.file.title, ‘YYYY-MM-DD’).file.link))’): ReferenceError: Moved is not defined

What I’m trying to do:

I would would like to add a data point to my journal of where I lived at the time of writing the journal post. So I created a folder Calendar/Houses to put notes for each house and school year.

While in a journal post titled YYYY-MM-DD I would like to search for notes in Calendar/Houses where the title of the journal post is between the date I moved into a house and the date I moved out.

House note front matter

---
Type: House
City: Canal
Date-Moved-In: 2007-08-01
Date-Moved-Out: 2007-11-22
Age-Moved-In: 16
Age-Moved-Out: 16
---

I would like it if the query output a link to this house not so that the top of my journal would look like this while reading.

Music:: 
Mood:: 
Tags:: #📓/Journal  
Age:: 16
House:: [[Canal Winchester]]
School-Year:: [[10th Grade]]

The issue you’re facing is related to naming your fields, and javascript parsing. When javascript sees p.Date-Moved-In, it parses it as a subtraction where you want to subtract Moved and In from p.Date.

To get your intended field, you need to use something like p["Date-Moved-In"], or even better change the field name into something which is easier for dataview to parse. Like Date_Moved_In or DateMovedIn or similar.

1 Like

Thanks, moving Date-Moved-In and Date-Moved-out into a [""] block got rid of that error. Changing tp.file.title to dv.current.title got rid of the next one.

$= dv.paragraph(dv.pages('').filter(p => p.file.path.includes('Calendar/Houses')).where(p => p.type == "House" && p["Date-Moved-In"] >= moment(dv.current.title, 'YYYY-MM-DD') && p["Date-Moved-In"] <= moment(dv.current.title, 'YYYY-MM-DD')).file.link)

But this query does not find any files. If I remove the date >= && date <= from the were, leaving behind p => p.type == "House" then I get all of my house files.

Would p["Date-Moved-In"] >= moment(dv.current.title, 'YYYY-MM-DD') be the correct way to get this? I would like to use this notes title to compare the date to.

$= dv.paragraph(dv.pages('').filter(p => p.file.path.includes('Calendar/Houses')).where(p => p.type == "House" && moment(p["Date-Moved-In"], 'YYYY-MM-DD') >= moment(dv.current.title, 'YYYY-MM-DD') && moment(p["Date-Moved-In"], 'YYYY-MM-DD') <= moment(dv.current.title, 'YYYY-MM-DD')).file.link)

A rewrite of your query, just to get a grip on what’s happening:

```dataviewjs
dv.paragraph(dv.pages('')
  .filter(p => p.file.path.includes('Calendar/Houses'))
  .where(p => p.type == "House" 
    && p["Date-Moved-In"] >= moment(dv.current.title, 'YYYY-MM-DD') 
    && p["Date-Moved-In"] <= moment(dv.current.title, 'YYYY-MM-DD'))
  .file.link)`
```

So I’ve reformatted your inline query, into an ordinary dataviewjs query. With queries like this, keep them readable. The output will be the same anyways.

There is one glaring error here, and that is the dv.current.title, to get the file name in a dataviewjs query, do dv.current().file.name. Secondly, no need to convert the date multiple times, so let’s store that variable, and re-use it.

I assume, but would like it verified that your note titles actually are of the format “2023-01-27” (or similar), yes?

And do you really want to do Date-Moved-In >= date and Date-Moved-In <= date? In essence, that means Date-Moved-In == date. I reckon, you’d want to use Date-Moved-Out in one of those, right?

So correcting this stuff, and adding a little more logic related to whether you actually lived somewhere, or if you lived in multiple locations at the same time, I ended up with this script:

```dataviewjs
const myDate = moment(dv.current().file.name, 'YYYY-MM-DD')

const house = await dv.pages('')
  .filter(p => p.file.path.includes('Calendar/Houses'))
  .where(p => p.type == "House" 
    && p["Date-Moved-In"] <= myDate
    && p["Date-Moved-Out"] >= myDate)
  .file.link
  
if (house.values.length == 0) {
  dv.paragraph(`Were you homeless on ${ myDate.format('YYYY-MM-DD') }?`)
} else if ( house.values.length == 1 ) {
  dv.paragraph(`On ${ myDate.format('YYYY-MM-DD') } you lived in ${ house[0] }.`)
} else {
  dv.header(3, "Which one did you really live in?")
  dv.list(house)
}
```
Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks) is properly shown.

1 Like

Not sure why you want this as a inline query, but here is the script transformed to be an inline query. I had to transform away from the string template literals, `…`, to ordinary string concatenations.

`$= 
const myDate = moment(dv.current().file.name, 'YYYY-MM-DD');
const house = await dv.pages('')
  .filter(p => p.file.path.includes('Calendar/Houses'))
  .where(p => p.type == "House" 
    && p["Date-Moved-In"] <= myDate 
    && p["Date-Moved-Out"] >= myDate)
  .file.link;
if (house.values.length == 0) {
  dv.paragraph("Were you homeless on " + myDate.format('YYYY-MM-DD') + "?") 
} else if ( house.values.length == 1 ) {
  dv.paragraph("On " + myDate.format('YYYY-MM-DD') + " you lived in " +  house[0] + ".") 
} else {
 dv.header(3, "Which one did you really live in?");
 dv.list(house)
}`

Do note that there are some changes to the actual DOM elements which are output from dataview when you change to/from inline or ordinary queries. In some cases, this can affect styling options and how the final result is rendered.

Lastly, this is a fairly complex query, which indicates it might be needed to change it every now and then, and for that purpose I would look into using dv.view() for inclusion in your notes, so that you’ll not end up with having to change a bunch of files because of some of these future changes.

Bonus tip: How to do dv.view()

In case your query is to be included into a template, like a daily note template, it’s often better to call a dv.view() from the template/daily note. This allows for the daily note to get a fixed reference to call the query, and allows for future you to change the query if needed.

To do this change you need to have a folder to hold the query in, like vault/_js, and you then need to create a file like myView.js in that folder. This will then hold your javascript query.

In place of your original query, you can now insert the following:

```dataviewjs
dv.view("_js/myView")
```

If your original query was an ordinary DQL query, you could transform that into a dataviewjs query by doing:

await dv.tryMarkdownQuery(`
... your original query ...
`)

A dv.view() will behave as if it were in the original file, so stuff like dv.current() and field references, will refer to the original file where you called dv.view(). For more information see: Codeblock Reference - Dataview

Finally, dv.view(), also allows for some custom CSS to be included, if instead of vault/_js/myView.js you create vault/_js/myView/view.js and vault/_js/myView/view.css

2 Likes

Yeah I agree inline querys are hard to write because you can’t format them like you did. Thanks for this suggestion.

Yes all notes that this querry will go into are formatted like that.

Oh good catch, that must be a copy past error.

I wanted this in an inline query so that I could add text before or after it… Though rereading your query you already took care of that with dv.paragraph("On " + myDate.format('YYYY-MM-DD') + " you lived in " + house[0] + "."). I guess it really does not need to be an inline query.

Thanks for the bonus tips, I am defiantly going to play around with the dv.view as I have a lot of dataview queries in my daily notes.

So I think based on your tip about views this is how I could write this daveview query.

in Extras/Datavew Queries/Where I lived.js I would put

await dv.tryMarkdownQuery(`
	const myDate = moment(dv.current().file.name, 'YYYY-MM-DD'); 
	
	const house = await dv.pages('')
		.filter(p => p.file.path.includes('Calendar/Houses'))
		.where(p => p.type == "House"
			&& p["Date-Moved-In"] <= myDate
			&& p["Date-Moved-Out"] >= myDate)
		.file.link;
	
	if (house.values.length == 0) {
		dv.paragraph("Were you homeless on " + myDate.format('YYYY-MM-DD') + "?")
	} else if ( house.values.length == 1 ) {
		dv.paragraph("On " + myDate.format('YYYY-MM-DD') + " you lived in " + house[0] + ".")
	} else {
		dv.header(3, "Which one did you really live in?"); dv.list(house)
	}
`)

Then in my template/daily note where this is going I can put.

```dataviewjs
dv.view("Extras/Dataview Queries/Where I Lived")
```

or

`$= dv.view("Extras/Dataview Queries/Where I Lived")`

In this case the inline query is really nice as it makes this only one line.

However this does not seem to work. I am not getting any errors just a blank line.

Loose the first and last line of Where I Lived.js. Remove these two lines:

await dv.tryMarkdownQuery(`
...
`)

Then you should get the output, I reckon.

And note that even though the last version with `$= ... ` looks the nicest, it doesn’t produce the nicest html, as it then adds a multiple <div>'s and <span>'s around stuff. So if you want to style stuff at some point the full code block is to be preferred.

1 Like

That worked! Thank you so much. So then what was the point of the awate dv.tryMarkdownQuery?

Interesting, Ok I will keep that in mind.

This was if, and only if your original query was an DQL query, like:

LIST 
FROM "somewhere"

Then you could use those lines around it, to make it into the javascript needed by a dv.view() call. Sorry for the confusion. This doesn’t apply to your case, and the explanation was/is trying to be a general introduction to how to use dv.view().

1 Like

Would it be possible to get front matter data from house[0]? Like could I say house[0].City to output the value of the city field within that notes formatter?

P.S. @holroy I really like your profile picture.

1 Like

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