Obsidian Dataview plugin

Dataview(external link) is a plugin for Obsidian that enables you to treat your vault of notes as a database, which you can query and create displays from in various ways. It offers a lot of different options, especially if you use the “data­view­js” code block to write up your queries in Javascript, and I’ve only just started to delve into some of these more advanced features. The main thing that I use Dataview for is creating “index pages” for the subfolders in my vault. This is a page for the tips and tricks I have learned so far.

Basic Usage

At its simplest, you can use Dataview just to list (either in a table or an actual list) all your notes that match a certain query. For example, on my “index page” for my Obsidian vault wiki, I have used to have this (it’s not much more complicated now but I really want to keep this “Basic Usage” section BASIC):

	status AS Status,
	date AS Created
FROM "Wiki"
SORT file.name ASC

A line-by-line explanation:

  • “TABLE” tells Dataview that I want the results displayed as a table. If I wanted a list instead, I could’ve put “LIST”. However, it seems to me that if you want a list, you can only display one field in addition to the default link/title. Listing two, comma-separated like I did here, causes errors.
  • This specific snippet is going to create a three-column table:
    • The first column is the default one, titled “File”, which consists of links to each note.
    • The second column will be called “Status”, and will display the contents of the “status” field in my notes’ front matter. If I’ve forgotten to include status frontmatter in a note, it displays a dash.
    • The third column will be called “Created”, and display the contents of the “date” field in my notes’ front matter (except it will reformat them to Obsidian’s default format, which is like “8:37pm - 16 September 2018”).
  • The next line tells Dataview to only query files in the “Wiki” folder (I don’t want it listing all my other notes too here).
  • The final line tells it how to sort: by file name in ascending order (because for some reason if I set it to descending order it gives me reverse-alphabetical order instead, which I don’t understand).

Reformatting Dates

You can use dateformat() to reformat a date from one format to another. If you don’t like Obsidian’s default, you can build your own format using the building blocks as listed on this page(external link).

For example, for most of my index pages now, where I used to just have date, I now have:

dateformat(date, "yyyy-MM-dd")

…which will display like “2018-09-16”. On a different index page, I want a format more like “Sunday 13 March 2022”. To get that, I can put in the format cccc d MMMM yyyy. With one less c and M (i.e. ccc d MMM yyyy) you can get the shorter format “Sun 13 Mar 2022”. You can also include hours, minutes, seconds and even timezones… just go check that page I linked for more details 😛

Creating Your Own Link/Title Column

By default, Obsidian links to your notes with the filename of the note (minus .md extension) as the link text. But what if you want something else to be your link text?? Well, the good news is that you can tell Dataview not to create the default link/title column (or text, in the context of a list) and substitute your own. This is currently what I have on my “Recipes” index page:

	link(file.link, title)
FROM "Recipes"

Again, to go through it line-by-line:

  • “WITHOUT ID” is the bit that tells Dataview not to generate the default link/title.
  • The next line basically means “make a link, with the link target being file.link, and the link text being title”. If a note doesn’t have a “title” field set in metadata, it seems to just grab the filename (minus extension) anyway. If you do set a “title” field in metadata, though, it takes pre­ce­dence.
  • The third line just tells it to only query the notes in my “Recipes” folder.
  • The final line tells it to sort by title in descending order (which is what gives me alphabetical order in this case).

Reformatting A Date That’s the Filename of a Note

So, the filenames of all my daily notes are in the format yyyy-MM-dd. The individual notes all contain an H1 that consists of the date in a longer format, but Dataview can’t query within notes to check for H1s (as far as I know). Because this longer-format date basically is the title of my daily notes, and because I don’t want to go back and edit hundreds of entries to copy the H1 into a new “title” metadata field, I would like to reformat the date-based file name to create better link text, instead. Can I do that? You bet!

So, this is the contents of my “Daily” index page right now:

	link(file.link, dateformat(date(file.link), "cccc d MMMM yyyy")) AS Date,
	weather AS Weather,
	description AS Description
FROM "Daily"

The new technique here is just that you can use date() to tell Dataview, “Hey, you know this string that you think is completely arbitrary (file.link)? It’s actually a date. Go ahead and treat it like one.” If you don’t explicitly tell Dataview it can treat that string like a date, it’ll throw errors. Once you do, it’s happy to reformat it just like any other date, and you can create a link with file.link as the link target and the reformatted date that is file.link as the link text and it’ll work great. Yay!

Using Regex to Cut a UUID Or the Like From a Title

I came across this discussion question(external link) where someone appended a Zettelkasten-like date-based UUID to all their note filenames/titles, and wanted to cut that out from the filename that displayed in a Dataview table. Until I saw this thread, I had no idea this was possible but once I saw it I instantly knew I wanted to do something like it myself: all my notes in my “Other Chronological” and “Posts from Blog” sections have filenames in the format “2020-02-20 Title of my Note” and it seemed like such a good idea for the link text to just be “Title of my Note” with a separate column for the date!

This can be achieved with a function called regexreplace(). I had only had very minimal prior experience with regex but I didn’t need to know very much to achieve what I wanted to here. The following is currently the contents of my “Posts from Blog” index page:

	link(file.link, regexreplace(file.name, "\d{4}-\d{2}-\d{2} ", "")) AS Title,
	status AS Status,
	dateformat(date, "ccc d MMM yyyy") AS Date
FROM "Posts from Blog"

As I learned today, \d is regex shorthand for [0-9], which itself means “a single character which is a digit between 0-9”. regexreplace() there is searching file.name for instances of the pattern “four digits, hyphen, two digits, hyphen, two digits, space” and replacing all instances of that pattern with nothing (that’s the "") for display. That leaves only the part of the filename after the first space, e.g. “Title of my Note”.

The other cool thing about this is that if I ever had a note that didn’t have a title – like the filename was “2022-03-13.md” and nothing more – it woul­dn’t match the regex because of no space. So if I ever made a macro-index that included all my daily entries too, I could include that same line and the daily entries wouldn’t get shafted with links to them that had no link text.

Why Are My Date Columns So Thick

Finally, one thing I noticed when working on these Dataview displays is that, a lot of the time, the different columns would be too close to equal in width: the “Date” column would be almost twice as wide as the actual longest date, and the “Title” or “Description” columns would be much narrower than they needed to be. I’m not sure why this issue occurred but I found a solution in this Github issue comment(external link). Basically, you can add a CSS snippet to your vault (in the .obsidian/snippets folder within the vault) that includes:

table {
  table-layout: auto !important;
  width: unset !important;
  max-width: 100%;

Fair warning: this snippet will probably mean your Dataview tables don’t take up even nearly 100% of the viewing area (unless you have lots and lots of columns displayed)… but at least your “Date” column will no longer be bizarrely huge!