Using inline Datview with Obsidian

I have been using Obsidian for close to three (3) years now. Like many other users I was drawn to Obsidian for its simple core (everything is a Markdown file 1), it’s extreme extensibility and it’s extreme speed. I use obsidian to store information, concept ideas and even write this blog post you’re reading

Writing about Obsidian using Obsidian

In this post I’m sharing some in-line examples of one of the most popular Obsidian plugin’s called Dataview. Dataview allow you to Treat your Obsidian Vault as a database which you can query from. Provides a JavaScript API and pipeline-based query language for filtering, sorting, and extracting data from Markdown pages.

A popular use for Dataview is to generate tables of pages. You can see a lot of examples of this on the Dataview overview page. In this blog, we will be looking at inline queries. By this, I mean query’s that go in-between text to render counts or other things. I had great difficulty finding examples of this at first, and when I got it working I vowed to share some examples for anyone out there. But first! The why

Why in-line queries

I use in-line queries to get count’s of pages or links. I use this to show me how many Items I have left in my inbox folder, to count the number of files I have containing the tag #meeting and to count how many books I read.

Setting up

The things you will need to do are:

  1. Install Obsidian
  2. Install the Dataview plugin
  3. Enable Inline Queries in the Dataview settings

To enable inline Queries go to the settings for Dataview. Here, you can enable Inline Queries. My queries didn’t work for half an hour before I found this option, so I’m hoping to spare anyone this issue:

Inline Queries settings in the Obsidian Dataview plugin

Queries

Now to the good stuff. Here are the queries I use:

Count all files in a folder

This is a great one if you want to know how many files are in a folder regardless of any other metadata. It also doesn’t require any custom Metadata 2. We do this with:

dv.pages('"0 INBOX"').length`

In you markdown this could look like:

You have `$= dv.pages('"0 INBOX"').length` in your "0 INBOX" folder

Which will render:

You have 10 in your "0 INBOX" folder

This works on files that link to another file in Obsidian. In this case, we will be using the link to [[blog]] for an example. To get a count of all files that link to the [[blog]] we can use:

dv.pages("[[blog]]").length

Again, to use it in markdown:

`$= dv.pages("[[blog]]").length`

Count all files with tag

Same for tags:

dv.pages("#blogpost).length
`$= dv.pages("#blogpost).length`

Combining queries

Now it doesn’t stop there. You can easily combine and filter queries. Here are some examples:

This gets you all pages linking to [[blog]] with the tag #blogpost

`$= dv.pages("[[blog]] and #blogpost")length`

Filtering on metadata

You can also add filters on top. For example, let’s get all pages linking to [[blog]] with the tag #blogpost put that also have a custom metadata fields called status. Metdata is outside the scope of this post, but you can read about it here. In example, we will use it to indicate the state of the article:

Let’s get a count of all draft’s:

`$= dv.pages("[[blog]] and #blogpost").where(p => p.status == "draft").length`

And get all our published blog’s:

`$= dv.pages("[[blog]] and #blogpost").where(p => p.status == "published").length`

SUM

Inline queries can be used to insert dynamic data in to your Obsidian notes. It’s works great in cases where you want to get a count of things in your vault. You could use this to count the books you read, the number of journal entries written or the amount of notes you made regarding a topic.

  1. https://daringfireball.net/projects/markdown/ 

  2. https://help.obsidian.md/Editing+and+formatting/Metadata 


Found this useful? Consider sponsoring me or buying me a cup of coffee at KoFi