Dataview plugin snippet showcase

It’s the same with getting aliases too so [[Youtube|YT]] only returns [[Youtube]] too. I don’t know if the new Dataviewjs exposes some way to get more details from a link, I haven’t played with it enough. I’d suggest raising an issue about this.

1 Like

Anyone knows how to cross table via dataview? I want to create some view and reuse that view. “JOIN TABLE”

Let’s say I created a custom YAML field (called Type) that I used for some notes only.

I want to see on which notes I have filled that YAML field and the value of the field respectively.

I used this:

    ```dataview
    table Type
    sort file.name desc
    ```

but this returns all notes and it’s a huge list (with only a few notes having the field “Type” has a value). I just want to see these notes that the “Type” field has a value.

Any ideas how to setup the query?

I found it:

   ```dataview
    table Type where Type
    sort file.name desc
    ```

Adding “Where Type” means show files where Type field has a value :slight_smile:

2 Likes

With some help from @Moonbase59 and @blacksmithgu, I was able to put together the following dataviewjs table for my daily notes (which are in YYYY-MM-DD format): it displays all the notes that were either created or last modified on the day in question. This gives me a good overview of (much) of what I was up to on that day.

(This is a revision of my earlier note Dataview plugin snippet showcase - #264 by AutonomyGaps)

// default dateformat in case it’s forgotten in front matter
var dateformat = "YYYY-MM-DD";
if (dv.current().dateformat) { dateformat = dv.current().dateformat; }

dv.table(["File", "Last Modified", "Date Created"],
  dv.pages()
  .where(p => p.file.mday.equals(dv.current().file.day) || p.file.cday.equals(dv.current().file.day))
  .sort(p => p.file.mtime, 'asc')
  .map(p => [
    p.file.link,
    moment(p.file.mtime.toString()).format(dateformat),
    moment(p.file.ctime.toString()).format(dateformat),
  ])
);
7 Likes

I thought I would share my book list. I had a similar table in notion, but wanted it in obsidian. Here is what it looks like:

Here is my new book template

# title
---
Cover:: 
Creator:: 
Priority:: 
Purpose::
Referrer::
Status:: To Read
Subtitle:: 

Here is my book index page:

```button
name Add Book
type note(Books\NEWBOOK, split) template
action New Book
```
^button-AddNewBook


```dataview
TABLE Subtitle, ("![coverimg|100](" + Cover + ")") as Cover, Creator, Referrer, Priority, Purpose, Status from "Books"
where file.name != "_Book Index"
SORT file.name
```

I got the idea of using the button plugin to create a new book here: Replicating Notion's tables with a couple of Markdown files and Obsidian plugins

39 Likes

What is the exact syntax (including the dataview part) to make this work?

Also, do we need to change the default date format?

  1. Just put the code shown into a dataviewjs code block (instead of dataview).

  2. No. But there are cases where it is extremely useful.

Be aware that the code shown by @AutonomyGaps will pick up any file in the vault that has something in its title that looks like a date to Dataview. If you don’t want that, you can specify a folder name inside the dv.pages() line, in the form of dv.pages('"Daily Notes"').

Docs on Dataview here: https://blacksmithgu.github.io/obsidian-dataview/docs/intro

1 Like

I am getting this error message:

Evaluation Error: TypeError: Cannot read property ‘isValid’ of undefined
at DateTime.equals (eval at (app://obsidian.md/app.js:1:1153594), :7732:34)
at eval (eval at (eval at (app://obsidian.md/app.js:1:1153594)), :9:27)
at Array.filter ()
at Proxy.where (eval at

Hi, this is great.
I’m trying to do a similar database but for research purposes.
In the image below, a database of images.

I tried to add the image-preview in the table using the HTML format in the frontmatter or in inline field (something like <img src="file:///Users/......"> ) and it works… But the table turns unstable and when it does an automatic refresh the table presents some glitches (a kind of ‘shakes’, as a graphical issue).
Now I’ll try to use your format ("![coverimg|100](" + Cover + ")") and verify if the graphical issues gone away. One question: what kind of link you put in “cover” property?

1 Like

Didn’t try @AutonomyGaps’ new code, but let me check in my vault. What version of Dataview are you using, and which version of Obsidian?

EDIT: RESULTS

I found that I got the same error as you, had to leave the note and reopen it a few times and wait some minutes until it worked. The error occurs when file.day can’t be populated (because the file has no recognized date in the title, and no date: YYYY-MM-DD frontmatter field).

I have this now, and it works (the file is also called 2021-05-23 Test DataviewJS AutonomyGaps, to provide a date to file.day):

# 2021-05-23 Test DataviewJS AutonomyGaps

file.day: `=this.file.day`

file.mday: `=this.file.mday`

file.cday `=this.file.cday`


```dataviewjs
// default dateformat in case it’s forgotten in front matter
var dateformat = "YYYY-MM-DD";
if (dv.current().dateformat) { dateformat = dv.current().dateformat; }

dv.table(["File", "Last Modified", "Date Created"],
  dv.pages()
  .where(p => p.file.mday.equals(dv.current().file.day) || p.file.cday.equals(dv.current().file.day))
  .sort(p => p.file.mtime, 'asc')
  .map(p => [
    p.file.link,
    moment(p.file.mtime.toString()).format(dateformat),
    moment(p.file.ctime.toString()).format(dateformat),
  ])
);
​```

(Note the last line ``` is faked for the forum—remove it if copying, and use real 3 backticks.)

Result:

1 Like

all the latest versions :man_shrugging:

Saying “latest” is always bad if people come here months after … using numbers is preferred. :wink:

Btw, I used Obsidian 0.12.3 and Dataview 0.3.9 for this test.

2 Likes

@yalcin @AutonomyGaps It seems Dataview sometimes doesn’t “see” the latest, probably due to Obsidian’s rather effective caching. In my case, I sometimes had to wait a few minutes, or close & reopen Obsidian until all was seen again (especially when renaming the note that has the code).

To make it more user-friendly, even if the current note doesn’t have a valid file.day, you could change the code like this:

// default dateformat in case it’s forgotten in front matter
var dateformat = "YYYY-MM-DD";
if (dv.current().dateformat) { dateformat = dv.current().dateformat; }

dv.table(["File", "Last Modified", "Date Created"],
  dv.pages()
  .where(p => dv.current().file.day && (p.file.mday.equals(dv.current().file.day) || p.file.cday.equals(dv.current().file.day)))
  .sort(p => p.file.mtime, 'asc')
  .map(p => [
    p.file.link,
    moment(p.file.mtime.toString()).format(dateformat),
    moment(p.file.ctime.toString()).format(dateformat),
  ])
);

Study the where clause: We only do it if file.day is valid.

On a note that has no date, you would now of course also get no results. But also no error message. (I removed the date from the note’s title for this test):

3 Likes

I just use a regular link in the cover property. I copy the image URL from Amazon and stick it in there.

2 Likes

Thanks so much for your fine-tuning of this!

1 Like

Thanks. Regarding the mentioned graphic issues, this seems to indicate that there is a difference between links to local files and URL links.

I have a simple table to collect my journal entries related to a given book. I’m trying to have one of the columns show a link to the heading where I wrote the entry:

table Chapters, file.link+"#Reading:: The Lost Symbol" as Link
from #my/journal where Reading="The Lost Symbol"
sort file.ctime asc

And it results in this
image

Instead of resulting in a [[2021-05-23#Reading The Lost Symbol]] link, it gives me a separate text ([[2021-05-23]] reading …)

I’m not very literate in codes, but I’ve been studying the documentation to see if there’s a way to achieve this. Do you guys know if it’s even possible?

Nevermind: found the answer here: Dataview plugin snippet showcase - #158 by jyrodgers

I want to do this (extract data from a field for use in the query) as well.
Context: I want to make a query that shows people I haven’t contacted in a defined amount of time. The people have a field ‘desired contact frequency’ and in it is ‘2 weeks’ or ‘2 months’, whatever. The query:

TABLE 
context, file.mday AS "Last Contact"
FROM "3 - Resources/People" 
WHERE desired-contact-frequency AND file.cday <= date(now) - dur(1 week) 
SORT file.mday ASC

^ Works

\/ Doesn’t work:

TABLE 
context, file.mday AS "Last Contact"
FROM "3 - Resources/People" 
WHERE desired-contact-frequency AND file.cday <= date(now) - dur(desired-contact-frequency) 
SORT file.mday ASC

There’s probably a way to do this in the js version, but 1) I’m not capable of that and 2) it seems like if there’s a way to access “desired-contact-frequency” this is a much simpler format.

Instead of this, could you try,

"[[" + file.name + "#Reading:: The Lost Symbol]]"

You can even add alias to the link if it gets long like,

"[[" + file.name + "#Reading:: The Lost Symbol|The Lost Symbol]]"

4 Likes