Dataview plugin snippet showcase

How does this code look when added on a date? For us not using Templater?

Sorry for the multi-post/deletion.

### Birthdays Today
```dataview
table phone as Phone, email as Email, file.mtime as LastEdit
from ""
where contains(birthday, "05.04")
```

You might need to play with some formatting on the date. That’s today’s date so MM.DD.

1 Like

Hi Rishi,
Many thanks for the reply.
You are right about the age calculation. But in many cases the initial date of the historical facts is not very precise and changes / refines throughout the research. And in these cases, it becomes useful an auto-calculated age field.
I will check the Templater plugin…
Just another question: in table format, in the tag field, if the result is just one tag, that tag appears as an ‘active’ element (auto-search), but in cases with multiple tags, these are presented as a list and are no longer ‘active’ elements. This is a limitation of the plugin?
Thanks.

I think so yeah. I had noticed it myself but since I don’t list tags in my tables, never really paid attention. Raising an issue on GitHub regarding this.

The latest version of Templater 1.6.0 adds support for dynamic commands which instead of rendering in text only execute and render in preview mode ie essentially they run like Dataview. With their support for moment() and ability to run JS, most of my problems with dates seem to be solved!

With the update to Templater 1.6.0 adding support for Dynamic Commands, we can now have certain templates set to only render in Preview mode and never replace itself in edit view. This, along with file.frontmatter, makes it work very much like Dateview (except not being inside code blocks). I don’t think we can access YAML from other files like Dataview, but for my purposes getting fields from the same file works well.

I finally figured how to calculate a Days until next birthday using this

Next Birthday: <%+ moment(tp.frontmatter.birthday).add(moment().diff(tp.frontmatter.birthday, "years")+1, 'years').diff(moment(), "days") %> Days

This assumes this in the front matter,

birthday: 1985-03-03

and produces this result,

Next Birthday: 312 Days
5 Likes

Are there any plans to make a regex extract function? That would make dealing with dates a lot easier.

WARNING: Dataview seems to return wrong or at least somehow rounded date calculation results:

Today is 2021-05-04, and a person’s birthday is on 1997-05-17. So she should be 23 years of age today, turning 24 on May 17th.

But when using

`= (date(today) - this.birthday)`

Dataview shows 24 years, instead of 23 years, and something.

I opened an issue on GitHub: Wrong date calculation? · Issue #181 · blacksmithgu/obsidian-dataview · GitHub

I’m assuming @blacksmithgu is using some “forum-like-date-display” code here, where it might be okay to say “someone commented roughly 24 years ago” but I would prefer exact results when working with a database-like system. Otherwise we won’t get good and exact birthday-type calculations, right? :wink:

Hint: If using moment.js, relative time thresholds can be customized: Moment.js | Docs

2 Likes

Is it possible to create nested groups?

table DueDate, rows.file.link
from #todo 
group by DueDate

works like a charm, but

table Project, DueDate, rows.file.link
from #todo 
group by Project, DueDate

fails with a parsing error:

  1 | table Project, DueDate, rows.file.link
  2 | from #todo 
> 3 | group by Project, DueDate
    |                 ^
  4 | 

Expected one of the following: 
'(', '*' or '/', '+' or '-', '.', '>=' or '<=' or '!=' or '=' or '>' or '<', '[', 'and' or 'or', /FLATTEN/i, /FROM/i, /GROUP BY/i, /LIMIT/i, /SORT/i, /WHERE/i, EOF, whitespace

Would this work?

table file.outlinks
from #emotion  

Good catch! Perhaps, we should default to using Templater for dates for the time being. With the new update, it works much similarly to Dataview.

No :man_shrugging:

Thank you!

I can’t get this to work, so I need to study the contains formula.

I have birthdays in front matter like this:


birthday: 2000-12-31

But using your code:


Birthdays Today

table phone as Phone, email as Email, file.mtime as LastEdit
from ""
where contains(birthday, "05-05")

…gets me nothing, even if I made several dummy contacts with “1980-05-05”, "“1985-05-05” etc as birthday.

So I’m obviously missing something.

1 Like

For troubleshooting, here is what my frontmatter looks like on a person

---
aliases: ["xxx"]
created: ["2021.03.02 10:17 AM"]
birthday: 2021.04.30
email: "xxx"
phone: "xxx"
---

And here is my dataview codeblock

### Birthdays Today
```dataview
table phone as Phone, email as Email, file.mtime as LastEdit
from ""
where contains(birthday, "04.30")
```

See if you can get the combination of those two working.

1 Like

Got it! When writing dates with dots (2021.05.05), it works.
But not in ISO format (2021-05-05), which I thought Dataview needed for dates.
Weird.
Thanks for helping!

Is this because 2021.04.30 is seen by Dataview as a text, not a date?
And that one can’t use a contains command on a date, because it’s a date, not a text?
Just thinking out loud here.

1 Like

Why not?

table file.outlinks
from #family

works fine here.

You might want to try something like

table birthday
from ""
where (birthday.month=5 and birthday.day=5)

Theoretically even better because you could have a date like 2005-05-01 which would otherwise be caught in a text match of 05-05.

My dates are stored as:

---
birthday: 1997-05-17
---
3 Likes

Perfect! I had lots of birthdays entered in ISO format already (and also other dates in my genealogy research, so that’s super useful. Thank you!

1 Like

In terms of integrating dataview queries in my daily notes, there are two things that are most important to me, and I’ve built them into my Daily Notes Template:

  1. A query that will reveal all the notes created on or last modified on that day. For this, I use “file.day” to generate a query based on the YYYY-MM-DD title of any given daily note. The advantage is that this never changes.
  2. A list of note titles that reflect every file that I’ve touched during that day. For this, “Last Modified” won’t work, since I might have created a note yesterday, work on it today, and then modify it again tomorrow. So I’ve resigned myself to doing a bit of manual review of the day (probably good to do anyway):
    1. At the end of the evening, run the query (by switching to Preview mode) for a LIST of “last modified in the past 18 hours”
    2. Copy the full list
    3. Switch back to Edit mode.
    4. Delete the dataview query
    5. Paste-as-text the clipboard
    6. Weed out unimportant files or modifications
    7. Wrap as links particularly key notes

This gives me static, persistent record of the notes I worked on in a particular day. I could use the “Text Expander” plugin for this, but it relies on Obsidian search, which doesn’t yet include dates (I believe).

Here are the dataview queries I use, and include in my Daily Note Template:

For the query for everything created or last modified on the YYYY-MM-DD date in the Daily Note title, and that remains as a dataview query:

dataview
TABLE file.mday AS “Last Modified”, file.cday AS “Date Created”
WHERE (file.mday = this.file.day) OR (file.cday = this.file.day)
SORT file.mtime asc

The improved version is below: Dataview plugin snippet showcase - #285 by AutonomyGaps

For the end-of-the-day query (which I then replace with the text):

LIST
FROM "" 
WHERE file.mtime >= date(today) - dur(18 hours)
SORT file.mtime asc

See also: @ryanjamurphy’s slightly different approach: Dataview plugin snippet showcase - #139 by ryanjamurphy

7 Likes

Does anyone know how to access sub fields in YAML using Dataview?

I just spent a lot of time adding metadata to my movie notes using the OMDB API which already had some fields. It looks something like this now,

---
aliases: ["Exam"]
tags: [film]
rating: 6
people: [[]]
dateCreated: 2021-04-06T18:37
info: 
  - title: Exam
  - year: 2009
  - rated: Not Rated
  - released: 2010-06-17
  - runtime: 101

Since I added a lot of new fields, I added them all under the field info because it was my understanding that we can do info.title and so on. Turns out, when I do that, it returns the entire list without any text but returns text for the one selected.

When I do just info in a table, this is what I get

And when I do info.year, this is what I get

Am I missing something very basic and silly or is this just a limitation?

1 Like
info: 
  title: Exam
  year: 2009
  rated: Not Rated