Dataview: Note is not included in table if "date" property is present but value has been cleared

What I’m trying to do

I have notes in a folder with properties as follows:

---
chapter: 1
status: Waiting
words: 300
date: 2024-12-10
---

I want to make a dataview table to display these notes and their properties as such:

TABLE
	chapter as "Ch.", 
	status as Status,
	date(today) - date as Since,
	words as Words
FROM "Example folder"
WHERE category != "overview"
SORT status

My problem is that if a note has the date property but the property has been cleared (using the “clear” button in the date picker), it does not show up in the table.

Clearing a date value from the date property looks different in source mode than an empty date property.

An empty date property looks as follows: date:.

Clearing a date value looks as follows: date: ""

Those quotation marks seem to be causing problems for dataview that causes the note to not be listed in the table.

Things I have tried

I tried putting the line of the table code about the date property in a choice function as follows:

choice(date != "", date(today) - date, "none") as Since,

or

choice(date != "\"\"", date(today) - date, "none") as Since,

or

choice(date != null, date(today) - date, "none") as Since,

None works.

I have also looked at lots of forum posts, asked ChatGPT, and poked around the Dataview documentation.

I’ll look into it later tonight or tomorrow, but does choice(date, date(today) - date, "none") as since work? Or date(today) - default(date, date("2000-01-01")) as Since ?

Thanks for replying. No, neither work, unfortunately. They just give the alternative value if the file does not have the date property in YAML. The file still doesn’t show when the date property is present but listed as “”

For now my workaround is to just not clear the dates using that button. I also might consider always displaying Properties as source going forward to avoid similar issues.

I believe what’s happening is that even when you’re using either choice or default, as long as you do date(today) - date is actually calculated, and you can’t do subtraction with a data and a string (text).

Here is one option which at least shows all files:

date(today) - choice(date, date, date(tomorrow) ) as Since

This time we’re sure that we’re always doing date arithmetic with actual dates. Either a defined date, or the date of tomorrow or something similar. If you use date(today) the overall field is cleared out.


As a sidenote, if you want to do some other tests, you could also do something like the following:

```dataview
TABLE 
chapter as "Ch.", 
status as Status, 
date(today) - choice(date, date, date(tomorrow) ) as Since,
choice(SinceDays = dur("-1 day"), "n/a", SinceDays) as "Since alt 2"
FROM "Example Folder"
WHERE category != "overview"
FLATTEN date(today) - choice(date, date, date(tomorrow) ) as SinceDays
SORT status
```

Here we store the since duration in the SinceDays variable, and if that matches the negative one day duration until tomorrow, we set the text to be n/a. You could of course use other dates, and texts, and so on.

Thank you! That does work, and I can also do something akin to SinceDays to clean it up as you noted. I still don’t understand why it works though when choice(date, date(today) - date, "none") doesn’t. It seems like they would both evaluate in the same way since the condition is if date is present.

The reason it doesn’t work is that before the choice() does any branching based on date it tries to calculate both cases and you’ll get the date(today) - date which leads to a date - "" evaluation which most likely causes an exception.

In my variant of the choice() all options are valid expressions, allowing the function to choose one of the valid expressions.

Ohhh now I understand. And if the field value is “” it always evaluates as false.

I guess my intuition for how the language works is not that good. I would think that calculating both cases before evaluating the condition would be computationally inefficient, but I guess it’s worth it.

Seems like properties in Obsidian could be improved if clearing the date didn’t introduce “” and instead kept the value blank.

1 Like