--- author: SkepticMystic aliases: - tags: - seedling publish: true --- # An Introduction to [[dataview|Dataview]] %% Add a description below this line. It doesn't need to be long: one or two sentences should be a good start. %% This is a reformatted, web-compatible version of the very first [[Obsidian Community Talks|Obsidian Community Talk]], an overview of the [[dataview|Dataview]] plugin, held by [[SkepticMystic]]. The original slides can be found [[An Introduction to Dataview Slides|here]] and are best viewed with Obsidian and the [[obsidian-advanced-slides|Advanced Slides]] plugin or converted to [[revealjs]] with [[YT - Pandoc and Obsidian - Create slideshows, PDFs and Word documents|Pandoc]]. Make sure to also take a look at the [[YT - An Introduction to Dataview|YouTube video]] for in-depth explanations. By [[SkepticMystic]] ## Overview 1. [[An Introduction to Dataview#Introduction|Broad Intro to Dataview]] 2. [[An Introduction to Dataview#Metadata|Metadata]] 3. [[An Introduction to Dataview#Dataview Queries|Dataview Queries]] - [[An Introduction to Dataview#List|List]] - [[An Introduction to Dataview#From|From]] - [[An Introduction to Dataview#Where|Where]] - [[An Introduction to Dataview#Task|Task]] - [[An Introduction to Dataview#Table|Table]] - [[An Introduction to Dataview#Sort|Sort]] - [[An Introduction to Dataview#Flatten|Flatten]] - [[An Introduction to Dataview#Group by|Group by]] ## Introduction [[dataview|Dataview]] is a plugin which lets you query data from your vault. The *official documentation* which you can use for reference is available here: https://blacksmithgu.github.io/obsidian-dataview ## Metadata ### What is it? Metadata provides information _about_ your data. It is _extra_ info that is either already _inherent_ in your data, or info that you **manually add**. ### Examples of Metadata 💡 #### Photographs - Date & time ⏰ - Location 🌍 - People 🧑‍🤝‍🧑 My display picture has the following _inherent_ metadata: ![SkepticMystic Mandala dp|400](https://i.imgur.com/UrwAWzI.jpg) ![Image metadata|400](https://i.imgur.com/dWmowLU.png) But it could also be given other properties: ![Edit image metadata|400](https://i.imgur.com/YPT6Gzb.png) #### Markdown Notes We can also add metadata to **Markdown notes** using a language called <abbr title="Yaml Ain't Markup Language"><em>YAML</em></abbr>. - YAML is a **structured** way of adding _arbitrary_ metadata to a file. ### Adding YAML Metadata 1. At the **top** of your note, add 3 dashes `---` 2. Underneath that, you can start adding key-value pairs in the form `key: value` 3. To finish the metadata block, close it off with 3 dashes again `---`. In the photo example, this would look as follows: ```yaml --- dimensions: 1200x1200 bit depth: 24 title: "Mandala Display Picture" rating: 5 --- ``` #### YAML Lists You can add more than one `value` to each `key` using _lists_. There are two notations you can use: ##### 1. Inline ```yaml foods: [apples, pears, oranges] ``` ##### 2. Indented ```yaml foods: - apples - pears - oranges ``` **Note the spacing!** ### Types of Metadata You can find this information on the [Dataview reference page](https://blacksmithgu.github.io/obsidian-dataview/#/README). ```yaml number: 3.6 string: "Foo all the bars" list: - 1 - -2 - 3.5 - 1.61803 date: - 2021-04 - "2021-04-09" - 2021-04-09T12:12:54 link: [[2021-04-09 Daily Note]] ``` ### Implicit Metadata in all notes | Property | Value | Type | | ------------ | ---------------------------------------- | -------- | | `file.name` | File **title** | `string` | | `file.path` | Full file **path** | `string` | | `file.link` | **Link** to the file | `link` | | `file.size` | **Size** (in bytes) of the file | `number` | | `file.ctime` | Date that the file was **created** | `date` | | `file.mtime` | Date that the file was last **modified** | `date` | | `file.day` | The **date** contained in the note title | `date` | | `file.tags` | An `array` of all **tags** in the note. | `array` | Subtags are broken down by each level, so `#Tag/1/A` will be stored in the array as `[#Tag, #Tag/1, #Tag/1/A]` ## Dataview Queries ### `List` Creates a _list_ of the specified notes ```dataview list ``` ### `From` Determines **where** to get notes _from_. #### From \#Tag You can get all notes _from_ a specified **tag**: ```dataview list from #MOC ``` #### From "Folder" All notes from a **folder**: ```dataview list from "1. Projects" ``` #### From \[\[Links\]\] And even all notes with links coming _into_ a note: ```dataview list from [[Yoga MOC]] ``` Or going _out of_ a note: ```dataview list from outgoing([[yoga MOC]]) ``` - This syntax may change in an upcoming release. #### Combining Sources You can use the 3 basic logical operators to create more complex `from` queries: - `list from #A and #B` - `list from "University" or "Work"` - `list from -#Personal` - `list from [[CSS]] and -#HTML` #### String Concatenation In the results of a `list`, you can include metadata fields joined with strings ```dataview list "File Path: " + file.path + " :)" from #SN ``` #### Lists of lists A `list` can also display indented sublists of metadata: ```dataview list authors from #SN/Blog ``` ### `Task` `Task` searches for all checkboxes `- [ ] ` in your vault. It returns a list of all tasks, grouped by their parent note ```dataview task from #MOC ``` ### `Where` After choosing _which_ notes to use, you can narrow down the list further using a `where` block. This lets you use the various _comparison operators_ on the metadata fields in your notes. `>`, `>=`, `<`, `<=`, `=`, `!=` `where {condition}` #### Examples - `where file.size > 1000` - `where file.name != "2021-04-09 Daily Note"` - `where file.mtime >= date(today) - dur(1 day)` - `where !complete` ### `Table` `Table` can show you a _table_ of various metadata fields linked to each note. `Table {field 1}, {field 2}, ...` #### Examples ```dataview table intensity from #Uni/2021/Asg ``` ![Dataview table](https://i.imgur.com/OnEoP7J.png) ```dataview table title, type from #SN ``` ```dataview table file.tags from Kw/Yoga ``` ### `Sort` You can use `sort` to define which order to list the results in, and which `field` to sort by: `sort field asc/desc` Give multiple fields to decide ties `sort field1 asc/desc, field2 asc/desc, ...` ### `Flatten` Use `flatten` to "unroll" lists into their own rows. ```dataview table authors from #SN ``` <center>versus</center> ```dataview table authors from #SN flatten authors ``` ### `Group by` `Group by` lets do gather together results based on the value of a field. You may group: - tasks by `completed` - games by `rating` - assignments by `intensity` First, gather all the assignments: `from #Uni/2021/Asg` Then, group by `intensity`: `group by intensity` #### `rows` Object By grouping the notes, we've created a **new object**. This is a **nested list** of all the assignments grouped by intensity. Something like: ```js [ [A1, A2, A6], // Green [A3, A4], // Yellow [A5, A7], // Red ]; ``` To access this new list, we use the `rows` object. - Get the file name of every note in the array: `rows.file.name` - Get the due date of every note: `rows.dueDate` ```dataview table intensity, rows.title from #Uni/2021/Asg group by intensity ``` #### Group by tags ```dataview table rows.file.tags, rows.file.link from #Fi/Yoga group by file.tags ``` ##### Limitations It will only consider two notes to be in the same group if they have **exactly the same tags**. - So even if two notes have `#Note/Author`, if the one has a tag that the other doesn't, they won't be grouped together. ## Functions ### `Contains()` Used to see if: - a `string` _contains_ a substring - a `list` _contains_ a value `where contains(file.name, "Daily Note")` `where contains(authors, "Robert Lamb")` ### `Length()` Returns the _length_ of a `string` or `list` `where length(file.name) > 10` ### `Sum()` Returns the _sum_ of the numbers in a `list` `where sum(minutesStudied) < 60` ### Many other Functions For further info on implemented functions, head over to [the official documentation](https://blacksmithgu.github.io/obsidian-dataview/#/functions). ## Neat Examples People have come up with a lot of interesting ways of leveraging Dataview. Many have been shared in the central [Dataview Snippet Showcase](https://forum.obsidian.md/t/dataview-plugin-snippet-showcase/13673) on the Obsidian Forum. ### Untagged Notes For example, finding all untagged notes in your vault is as simple as: `list where length(file.tags) = 0` ### Birthdays If you track \#People in separate notes and add their Birthday as a dataview property, you could compile a list of everyone whose birthday is today like this: ```dataview list from #People where Dates.Birthday = "<% tp.date.now(format = "YYYY-MM-DD") %>" ``` %% Hub footer: Please don't edit anything below this line %% # This note in GitHub <span class="git-footer">[Edit In GitHub](https://github.dev/obsidian-community/obsidian-hub/blob/main/04%20-%20Guides%2C%20Workflows%2C%20%26%20Courses/Guides/An%20Introduction%20to%20Dataview.md "git-hub-edit-note") | [Copy this note](https://raw.githubusercontent.com/obsidian-community/obsidian-hub/main/04%20-%20Guides%2C%20Workflows%2C%20%26%20Courses/Guides/An%20Introduction%20to%20Dataview.md "git-hub-copy-note") | [Download this vault](https://github.com/obsidian-community/obsidian-hub/archive/refs/heads/main.zip "git-hub-download-vault") </span>