Revised Obsidian Dataview Substring Queries in Monthly Notes
A couple of weeks ago, I posted about using Obsidian dataview queries to populate sections of my monthly notes. As I found out today, my queries were wrong.
Background
Most of the sections in my monthly notes use dataview queries to pull entries from my daily notes. For example, the following dataview query pulls data out of my journal folder that are prefaced by the inline YAML tag ‘::entry’. Before today, that query looked like this:
TABLE WITHOUT ID
file.link AS "DailyNote", entry AS "Entry"
FROM "001-Journal"
WHERE entry != null
AND file.day.year = number(substring(string(this.file.name), 0, 4))
AND file.day.month = number(substring(string(this.file.name), 6, 7))
SORT file.day
Of key importance are the AND statements that select the year of note to be displayed in the table (first AND statement), and the month of the note (second AND statement). The monthly note is in the format YYYY-MM. If the query is working properly, the query would display all daily notes that had instances of lines containing the inline YAML tag of ‘::entry’ in the first column of the table, and display the entry following the inline YAML tag in the 2nd column of the table. That selection would be narrowed down by only selecting daily notes with the year of YYYY and a month of MM as referenced by the monthly note filename. Ex: if the monthly note was named ‘2024-08’, only daily notes which had a year of 2024, and a month of 08 (August) with the inline YAML tag ‘entry::’ would be selected.
The Issue
The query worked well for August 2024 and September 2024 monthly notes, but when I created the October 2024 monthly note, none of the monthly dataview queries populated properly. The issue was that the second AND statement index was wrong, and was pulling only the last digit of the month. Instead of pulling 08, 09, and 10, the query was pulling 8, 9, and 0. 8 and 9 are valid months, but 0 isn’t.
The Fix
Changing the second AND statement to the following solved the issue:
AND file.day.month = number(substring(string(this.file.name), 5, 7))
I’ll be honest, the indexing numbers don’t make a lot of sense to me in this context, but you can read more about it here. Many thanks to discord user ipshing in the Obsidian Members Group #dataview channel for quickly pointing out my error.