Dataview plugin snippet showcase

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

@mnvwvnm I like solving mysteries but I gotta admit, that’s a tough one!

If I understand well the issue, I think that it is only necessary to remove the “-”.

2 Likes

Thanks! That worked. It was probably my poor understanding of YAML syntax.

It’s the difference between a YAML sequence and a map, elsewhere also called array and dictionary.

You can easily debug your YAML using the Online YAML Parser.

1 Like

Hey thanks for sharing that! I did run it through a validator since my YAML was being generated using a script and since it showed fine, I didn’t bother much further. I checked Dataview’s documentation and couldn’t find an example of something like this, so decided to jump in here. Bookmarking the parser as I’m sure I’ll be needing this again.

Re duration. Does anyone know if it is possible to get the Number used in a duration from a different field? So instead of dur(18 days) it’s dur(“DaysToHarvest” days)?

Is there any possibility to create a database of images?
How to add metadata to images?
In the github page of dataview plugin we can find in the roadmap the ‘future’ feature of “Gallery view (primarily for images)”… This means that is possible to add some metadata to images… Or it will only work with the ‘implicit atttributes’ (file.name, etc.)?

Any ideas how to display subtags (#tag1/tag2) as a single (and hence clickable) tag rather than two tags - one parent of the other tags in dataview tables?

Hooray for Dataview 0.3.0+ and the new dataviewjs code blocks!

To continue Dataview plugin snippet showcase - #218 by Moonbase59 and others, here’s a “family birthday list” as an example, with formatted dates, age calculation and sorting:

// Family birthdays using DataviewJS
let pages = dv.pages("#family").where(p => p.birthday);
dv.table(["Name", "Birthday", "Age", "This year"],
  pages.sort(p => moment(p.birthday.toString()).format("MM-DD"), 'asc')
  .map(p => [
    // The name
    p.file.link,
    // Formatted birthday from YAML frontmatter
    moment(p.birthday.toString()).format("ddd, YYYY-MM-DD"),
    // Current age in years
    moment().diff(moment(p.birthday.toString()), 'years'),
    // This year’s birthday as formatted date
    moment(p.birthday.toString().substring(5, 10), "MM-DD").format("ddd, DD MMMM")]
  )
);

Result:

Since I’m using moment.js, the dates shown are locale-aware and should adapt to whatever language you set Obsidian to.

Happy experimenting!

Docs on @blacksmithgu’s new page: https://blacksmithgu.github.io/obsidian-dataview/docs/intro


EDIT 2021-05-09: This has now been superseded by Upcoming Birthdays (robust & flexible) which is much more robust and YAML-configurable!

7 Likes

Is there a way to put an ellipsis maximum character length limit using CSS in File Name?

Well, you could go for a CSS snippet like the following, that shortens only links to the available column width in Dataview tables:

/*
    dataview-shorten-links.css snippet

    Shortens long filename links in Dataview tables with an ellipsis.
    2021-05-07 Matthias C. Hormann (Moonbase59)
*/
.dataview.table-view-table > tbody > tr > td a {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

Voting for this Dataview issue could maybe also help: Tables: Better column width adjustment depending on content? · Issue #186 · blacksmithgu/obsidian-dataview · GitHub

5 Likes

Do you know if we are able to find the headings in backlinks? I’ve been trying to automate MOCs. Dataview seems to treat [[YouTube Channels#Coding]] the same as [[YouTube Channels#Obsidian]].

If Note A has [[YouTube Channels#Coding]] and Note B has [[YouTube Channels#Obsidan]]. A Dataview table searching for only [[YouTube Channels#Coding]] will show both. I’ve tried different where statements and contain statements.

In the table or list preview, it will display as “YouTube Channels” and then when I hover over it, it will show only the “Coding” section or only the “Obsidian” section.