Dataview - Metadata inheritance to Tasks from Page

Hey, folks. I’ve a question about metadata inheritance specifically as it applies to tasks inheriting metadata from a page.

I would like to create a table of tasks where the metadata defined on a page is reflected unless the metadata field is overwritten for that specific task.

eg.

With an example page of:

---
month: May
"Cloud Cover": ".35"
tags: may
---

- [x] 1st
- [x] 2nd
- [x] 3rd [cloudCover: .80]
- [x] 4th [cloudCover: 1]
...
- [ ] 30th
- [ ] 31st

I would expect to be able to use this query

Table without id
    sDay.text as Day,
	sDay.cloudCover as "Cloud Cover"
FROM #may 
FLATTEN this.file.tasks as sDay

to get

Day (6) Cloud Cover
1st .35
2nd .35
3rd cloudCover.80 .80
4th cloudCover1 1
30th .35
31st .35

Where all but the cloudCover value defined on the 3rd and 4th is inherited from the page’s metadata.

Instead, I get

Day6 Cloud Cover
1st -
2nd -
3rd cloudCover.80 .80
4th cloudCover1 1
30th -
31st -

What am I missing about list items or tasks inheriting from pages?

Even simpler, using an inline:

= this.file.tasks.cloudCover returns

-,-,.80,1,-,-

when if inheritance worked as the documentation implies it does, it should return

.35,.35,.80,1,.35,35

There are some minor issues, which I’ll come back to, but the main thing you missed out on is to use of the default() function. This allows you to pick a value, and if that’s not defined use the alternate parameter.

In addition, you’ve got some strangeness with using this.file.tasks in the FLATTEN command instead of just file.tasks which will make a major difference when doing the query on all files tagged #may.

In general, from my limited experience, I would advice against using spaces in the field names, as it tends to cause issues. I almost always either use CamelCase or lowercase_with_underscores. (Please don’t use lowercase-with-dashes, as that’ll break in some cases where the - is treated as the actual minus sign and a calculation is attempted to be done)

With that being said, one can (often) reach field names with spaces using the something["Cover Cloud"] instead of the obviously faulty something.Cover Cloud. And in this case, that something needs to be row. Just as a proof of concept, try the following query to show some different ways to address a field name with space in it:

```dataview
TABLE WITHOUT ID
  cloud-cover,
  row["Cloud Cover"],
  row.cloud-cover,
  row["cloud-cover"],
  row["Cloud Cover"],
  file.frontmatter["Cloud Cover"]
WHERE file.name = this.file.name
```

Ok, back to the real task at hand, here is a modified script with some smaller adjustments:

```dataview
TABLE WITHOUT ID
  sDay.text as Day,
  cCover as "Cloud Cover"
FROM #may 
FLATTEN file.tasks as sDay
FLATTEN default(sDay.cloudCover, row["Cloud Cover"]) as cCover
```

Which produces this output from the first four elements in your example:

As can be seen from the last line, it’ll use the sDay.cloudCover field, but if that don’t hold a value, it’ll pick the second option, row["Cloud Cover"]. In any case this value is stored in cCover which is now available for sorting and grouping and where clauses, and so on.

3 Likes

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