Yaml frontmatter objects

What I’m trying to do

Overall, I am trying to create a meaningful yaml front matter structure for easy querying. In more detail, I would like to generate a yaml structure to query nested key-value pairs. The pairs correspond to dates (year, month, day) and a value.
E.g.,

---
year:
  1990: 3
  1991: 4
  1992: 5
---

Things I have tried

I would like to create the following table:

1990 1991 1992
3 4 5

I have managed to fetch a single year using:

TABLE year["1990"] as "1990"
WHERE file = this.file

I assume I need to FLATTEN my metadata, but I don’t manage to create the desired output.

TABLE year
FLATTEN year
WHERE file = this.file

I’m very open to suggestions how to structure my yaml frontmatter, e.g., not using key-value pair objects. Is there any good practice how to structure yaml frontmatter containing dates (years, months, days) and corresponding values, to make them easily queryable using dataview?

Give this a try:

---
years:
- year: 1990
  value: 3
- year: 1991
  value: 4
- year: 1992
  value: 5
---

`=this.years`

like this?

TABLE year
FLATTEN year
WHERE file = this.years

I get no results to show.

the dataview would look like this:

TABLE WITHOUT ID y.year AS year, y.value AS value
FROM "Untitled"
FLATTEN years AS y

this looks like the table you want, though transposed. To transpose this you would need dataviewjs instead I believe

(the "FROM “Untitled” is just me directly linking to the file I’m working on)

1 Like

I would rather use DataviewJS because the goal here is relatively complicated.

Note A.md

---
year:
  1990: 3
  1991: 4
  1992: 5
---

Note B.md

---
year:
  1990: 10
  1992: 40
  1996: 50
---

Query:

```dataviewjs
const pagesWithYear = dv.pages().where((page) => page.year);
const years = [].concat(...pagesWithYear.year.map((obj) => Object.keys(obj))).unique();
dv.table(
	['File', ...years],
	pagesWithYear.map((page) => {
		return [
			page.file.link, 
			...years.map((year) => page.year[year])
		]
	})
)
```

Result:

4 Likes

that was very insightful, thank you @ush and @cheezopath
i will look into both solutions and play with them according to my needs!

2 Likes

@cheezopath as a follow up question, how should i structure my yaml front matter and query if i would like to add another level? let’s say i would like to add a countries layer:

---
countries:
  A:
    - year: 1990
      value: 3
    - year: 1991
      value: 4
    - year: 1992
      value: 5
  B:
    - year: 1990
      value: 10
    - year: 1991
      value: 40
    - year: 1992
      value: 50
---

i was hoping it would work like this, which it does not:

TABLE WITHOUT ID country, c.year as year, c.value as value
WHERE file = this.file
FLATTEN countries AS country
FLATTEN country as c

hmm, this is about the best I can do:

---
countries:
  - name: A
    years:
    - year: 1990
      value: 3
    - year: 1991
      value: 4
    - year: 1992
      value: 5
  - name: B
    years:
    - year: 1990
      value: 10
    - year: 1991
      value: 40
    - year: 1992
      value: 50
---

```dataview
TABLE WITHOUT ID country.name as name, country.years.year as year, country.years.value as value
WHERE file = this.file
FLATTEN countries AS country
```

depending what you’re doing, it might make sense to have one note per country, and if you want to get much more complex it’s definitely worth looking at dataviewjs

1 Like

works like a charm, many thanks! now i understand better how to structure the yaml frontmatter. but let’s see if i manage to get to where i want, it’s trial and error. i have even less knowledge of dataviewjs, so i try to avoid it…

thanks for your support!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.