Dataview duration function does not display anyting for zero durations, what can I do?

What I’m trying to do

I’m trying to get the duration function to display zero durations but it doesn’t display anything.
= dur(15m) → 15 minutes
= dur(0m) → # nothing, what I want here is “0 minutes”

Things I have tried

I thought I would be able to use something like default, but it doesn’t work. I can’t find another function that could possibly work.

= default(dur(0m), "0 minutes") → # nothing, I was expecting “0 minutes”
= default(dur(1m), "0 minutes") → 1 minute

So clearly not returning “null” when the resulting duration is zero.

Then I thought I should try the “durationformat” function. I thought if I can ask it to format less than a minute into minutes it might give me “0 minutes” but I’m getting these lovely errors.

for =durationformat(dur(1s), "m 'minutes' ")
“Dataview (inline field ‘durationformat(dur(1s), “m ‘minutes’”)’)”: Error
and for = durationformat(dur("1s"), "m minutes")
"Dataview (for inline query ‘[object Object]’): Unrecognized function name ‘durationformat’ "

Then I thought I would check the settings and see I could change the null return to actually be “null” and I did in Settings → Dataview → Render As Null → “enter value”, but this just proved one thing = dur(0m) is not returning “null”

I need something, that display a value, when either, no value or null value passed to it. Is there a way to do this?

this is super hacky but does technically work:

[foo::0 minutes]
=choice(dur(this.foo),dur(this.foo),"0 minutes")

If you change foo to some value that dur likes, it’ll return that instead

Thanks, you are right this very hacky. Not exactly what I was looking for, but I think I might be able to make this work.

Like this:

= choice(false, dur(1m), "0 minutes") → 0 minutes

= choice(true, dur(1m), "0 minutes") → 1 minute

I guess I just have to make sure that I always use true with non-zero durations.

Quite frankly, I just wish the would update or fix the default function to handle this.

I guess, I’m going to have to visit the repo and open a feature request.

Ah I assumed you wanted to solve this for some variable that could be passed to dur

I’m not entirely following what your actual use case is, but I found that while dur("0 m") indeed returns an empty string, the expression dur("0 m").minutes returns the number 0. This can also be utilised in stuff like the choice() functions, or similar.

Furthermore, I don’t get the same error messages you get, but then again your post is slightly botched by the forum post formatting rules.

Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like in code blocks or queries) is properly shown.

This is the best I can do for an explanation.

I’m comparing dates and getting the duration is days. I don’t want to see blanks in the results. That about boils it down. I want consistency in the delta results, I want to see “1 day”, “10 days” or “0 days” not a blank cell.

I have this query

```dataview
TABLE  a-date AS Applied, dur(a-date - p-date) AS Delta
FROM "/"
WHERE  (p-date AND a-date) AND dur(a-date - p-date).days <= 10
SORT Delta ASC
LIMIT 5
```

Currently I get these like this results, and it just looks like data is missing.
I have to think about what I’m seeing to really make sense of it, instead of quickly getting what I need at a glance.

File (3) Applied Delta
Job 1 January 26
Job 2 January 26 3 days
Job 3 January 26 1 week, 3 days

That’s all that is behind this question.

But I can live with your suggestion of using the unit method for the duration.

TABLE  a-date AS Applied, dur(a-date - p-date).days AS "Delta (days)"
File (3) Applied Delta (days)
Job 1 January 26 0
Job 2 January 26 3
Job 3 January 26 10

So, yeah, this works too, but I really wanted the units displayed. I guess I can close this now.

Thanks

Oh and thanks for the tip about the 4 backticks.

1 Like

You could kind of add back the units by doing:

TABLE  a-date AS Applied, dur(a-date - p-date).days + "day(s)" AS "Delta"

Or if it’s not happy about adding number and text, then use:

TABLE  a-date AS Applied, string(dur(a-date - p-date).days) + "day(s)" AS "Delta"

It’ll not be entirely correct since it doesn’t counter for 1 day…

That works too!! Thanks.

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