Dataview list based on PREVIOUS month

Things I have tried

Dozens of different dataview codes.

What I’m trying to do

In the template for my daily notes, I have the following dataview code:

list from #journal 
where contains(file.name, "{{date:YYYY-MM}}")
sort file.name asc

When the new daily note is created, it produces the following, with the date field having the correct year and month:

list from #journal 
where contains(file.name, "2021-08")
sort file.name asc

This means I get a linked list at the end of every daily note of all the notes written in the current month. This helps with navigation.

I add a second a set of code to also list the previous month, but I have to add this manually. The following, for example, lists all the daily notes for July 2021:

list from #journal 
where contains(file.name, "2021-07")
sort file.name asc

What I would like to do is to find a way to automate this so that the…

where contains(file.name, "{{date:YYYY-MM}}")

…in the template generates a date based on the previous month. I was hoping something simple like the following would work:

where contains(file.name, "{{date:YYYY-MM}}, -1")

But, of course, it doesn’t.

Anyone know if it is possible to make this work?

Thanks for reading.

Angel

For now I don’t work with dates. But I think you don’t need to use the template date format {{data}}.
Why don’t you use the implicit field ‘file.cday’ (the date that the file was created)?
Please test this for second query:

```dataview
LIST FROM #journal
WHERE file.cday.month = this.file.cday.month - 1
sort file.name asc
```
2 Likes

I am very grateful to you for your time and help. Pure genius, and much better than anything I tried by myself.

When I add your query, it returns three days in July:

2021-07-29-Thursday
2021-07-30-Friday
2021-07-31-Saturday

It only returns three days because this journal was originally created in Bear, and all of the other days in July 2021 were created in February 2021. The modified date doesn’t work either as it captures lots of files in the rest of the journal that were modified in July.

But I feel as though I am getting closer.

If I work out a solution, I will post it here.

Many thanks.

Angel

But if you use “date” in note title, maybe you can work with file.day. As I said before, I’m not comfortable with recommendations about “dates”, but see this (again, by @Moonbase59 ):

```dataview
LIST FROM #journal
WHERE file.day.month = this.file.cday.month - 1
sort file.name asc
```

OR

```dataview
LIST FROM #journal
WHERE file.day.month = this.file.day.month - 1
sort file.name asc
```
3 Likes

Yes, I have been thinking along the same lines. And I was just reading and trying out some of the queries in the post by @Moonbase59 that you referenced. Feeling optimistic.

Many thanks for sharing your ideas. I am truly thankful.

Angel

1 Like

Hint: file.day returns a valid date when either of these conditions are true:

  • an “understandable” date in the file title (=file name), for instance “YYYY-MM-DD” (Daily Notes format), or “YYYYMMDD” (à la Zettelkasten date),
  • a date: YYYY-MM-DD (or longer, ISO format) in the note’s YAML frontmatter.

In all other cases, the result is undefined, which Dataview will substitute with the replacement character(s) you have set in its settings.

2 Likes

My file names are in YYYY-MM-DD-dddd format, but I can’t find a way to identify the MM from file name and then get file.day to return a valid date minus one month.

Instead of returning 2021-08 for a daily note created today, I want the template to return 2021-07. This works as a handwritten dataview query:

list from "journal"
where contains(file.name, "2021-07")
sort file.name asc

But I have to edit the ‘YYYY-MM’ each month, rather than just have the template create the previous month automatically.

This query…

LIST FROM "journal"
WHERE file.day.month = this.file.day.month - 1
sort file.name asc

…kind of works, but it lists every note created in every July (if added to notes from August 2021) in every single year. I’m trying to narrow it down to July 2021 (in this example).

It also doesn’t work for notes in January. Presumably, with the minus one syntax, it is looking for a month called 00, and it returns no records at all.

I think I will look hard at your hint tomorrow and try to follow the clew. My brain is frazzled.

Thanks to you both.

Angel

Ignoring the January problem, you can add a new condition to your query to solve the multiple years:

```dataview
LIST FROM "journal"
WHERE file.day.month = this.file.day.month - 1 AND file.day.year = this.file.day.year
sort file.name asc
```

To play safe, go for

WHERE file.day.month = (date(today) - dur(1 month)).month
    AND file.day.year = (date(today) - dur(1 month)).year

This

  • compares
    • the month from the file.day (in turn from file title or date: YAML)
  • against
    • the month calculated by subtracting a duration of 1 month from today’s date.
  • and then also compares the year since we don’t want files from last month in all years.

You could also use a string compare (by building “YYYY-MM” strings and then comparing these). Which to choose is personal style, mainly. (What is more logical to you.)

WHERE dateformat(file.day, "yyyy-MM") = dateformat(date(today)-dur(1 month), "yyyy-MM")

Problem solved! (Hopefully.)

Those who knows, know!
But I think @Angel is interested in get a list, in every daily note, of all the notes written in the same month. Not a “dynamic” list from “today” date.

Ah okay, maybe I missed that. Then just change all

date(today) against this.file.day

Et voilà!


Since Daily Notes tend to accumulate and changing all of them because, say, you wanted now to display files for the last three months instead, is a lot of work, you could even employ a “Daily Note Parameters” file that would have (in its YAML) the “standard parameters” to be used with Daily Notes … You’d then only have to change one file, and the Daily Notes would kind of “auto-adjust”.

But let that be a lesson for another day. We don’t want to over-awe a newcomer. :wink:

2 Likes

Thanks to both of you.

Right now, when by brain is in pre-breakfast mode, I am bewildered. But I will eat, drink, read again, and then see where your kindness leads me.

In the end, I hope to add three dataview queries to the end of every daily note.

  1. That queries and provides links to this day in previous years
  2. That creates links to every daily note in the current month
  3. That creates links to every daily note in the previous month (the subject of this thread)

As my daily notes have travelled from Day One to Scrivener to Bear to Obsidian, they have been through a lot of changes. Each old note currently ends with the hashtag “#journal” and then occasionally some other random tags and detritus that I won’t mind losing.

I hope to use regex (in VS Code or Atom) to find the #journal tag + detritus in every daily note and to replace everything with the three dataview queries. This should mean that every old note will be transformed into Obsidian loveliness.

I think I should back my journal up first. :thinking:

If I get this to work, I will post the queries as an md file so that others can copy and use them if, like me, they have badly designed daily notes and odd whims.

Super grateful.

Angel

2 Likes

Thanks to @mnvwvnm and @Moonbase59 for being kind enough to share their time and wisdom. I am awed and grateful.

For details of the dataview queries used in the journal, see the markdown file attached.

Angel

journal_dataview_queries.md (2.2 KB)

Edit: I marked this post as the solution as it contains the final code. The solution was provided by @mnvwvnm and @Moonbase59 in the posts above.

2 Likes

Well done! Any reason you sort “Z→A” in “On this day”?

SORT file.name desc

Hello.

I did nothing. The plaudits and applause belong to you and @mnvwvnm alone.

I sort descending for that query as I enjoy the sense of going back in time as I meander down the list.

Many thanks.

Very grateful.

Angel

1 Like

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