Comparing to multiple values in Dataview (i.e. myField = a | b)

What I’m trying to do

I want to conditionally change how some data displays in a Dataview table–I have a table of deadlines in my daily note template, which has the difference between the deadline and today as “Days Remaining”. I’ve been using choice() to evaluate how to display it, but I want the boolean being fed to choice to evaluate multiple criterion at once (i.e. days = 1 | 2) rather than having a separate choice() statement for days = 1, days = 2, etc.

The goal is for it to display a “:red_circle: Today” for anything due today, “Overdue” for anything past due, and conditionally append “:orange_circle:”, “:yellow_circle:”, or “:green_circle:” to the calculated number of days remaining, depending on how many there are.

For example, I would want 1 or 2 days to be orange, 3 or 4 to be yellow, and 5 or above to be green.

Things I have tried

I’ve been extensively scouring the dataview documentation for anything about its boolean comparisons, and haven’t been able to find anything about comparing multiple values. The closest I’ve found is contains() and friends, but even that only accepts a singular parameter to compare to the larger object or list.

My code and its output are below:

table without id 
file.link as Assignment,
deadline as Deadline,

choice(
	(date(deadline) - date(today)).minute > -1,
	
	choice(
		((striptime(deadline) - striptime(date(today)))).days = 0,
		
		"🔴 TODAY",
		
		choice(
			(striptime(deadline) - striptime(date(today))).days = 2,
		   "🟠 " + (striptime(deadline) - striptime(date(today))),
		   striptime(deadline) - striptime(date(today))
		)
	), 
	
	"Overdue"
	
	) as "Days Remaining"

where deadline != null
and complete != true
SORT deadline asc

If anyone has any input on this, it would be much appreciated! In the meantime I’m going to continue building the clunky version.

For anyone interested, I got it working with the clunky method. In any Assignment note, deadline is just a date property and complete is a boolean.

table without id 
file.link as Assignment,
deadline as Deadline,

choice(
	(date(deadline) - date(today)).minute > -1,
	
	choice(
		((striptime(deadline) - striptime(date(today)))).days = 0,
		
		"🔴 TODAY",
		
		choice(
			(striptime(deadline) - striptime(date(today))).days <= 2,
			"🟠 " + (striptime(deadline) - striptime(date(today))),
			   
			   choice(
				   (striptime(deadline) - striptime(date(today))).days <= 4,
				   "🟡 " + (striptime(deadline) - striptime(date(today))),
				   "🟢 " + (striptime(deadline) - striptime(date(today)))
		   )
		)
	), 
	
	"Overdue"
	
	) as "Days Remaining"

where deadline != null
and complete != true
SORT deadline asc

I find the trick to stuff like this is to format the multiple choices in a way that makes sense to you, and is somewhat easy to understand. One variant of this is to do the following query:

```dataview
table without id
  file.link as Assignment,
  deadline as Deadline,
  daysLeft, 
  daysRemaining as "Days Remaining"

FLATTEN (striptime(deadline) - date(today)).days as daysLeft
FLATTEN choice(
  daysLeft < 0, daysLeft + " days ago!!!", choice(
  daysLeft = 0, "🔴 TODAY", choice(
  daysLeft < 3, "Hurry up!" + daysLeft + " days left", 
  "Go back to sleep, " + daysLeft + " days left"))) as daysRemaining
  
where deadline != null
and complete != true
SORT deadline asc
```

Here I’m using a chain of choice() like you’ve done, but I’ve visually formatted them slightly against standard choices so that I can see one choice per line, and what to do if that’s true. Then as the false statement, I start another choice(). The last line is used if all the other fails. Also note that I do the calculations of daysLeft until deadline just one time, to make the logic below easier to follow.

A similar setup over the same theme:

FLATTEN
  choice( daysLeft < 0, daysLeft + " days ago!!!", 
  choice( daysLeft = 0, "🔴 TODAY", 
  choice( daysLeft < 3, "Hurry up!" + daysLeft + " days left", 
    "Go back to sleep, " + daysLeft + " days left" ))) as daysRemaining

The logic is the same, but the visual appearance is slightly different. Some like this format better. In both cases the logic depends on the previous statements being false so for the “Hurry up!” choice it already knows that there are days left and its not today, so it’s enough to check whether it’s less than 3 days left until it should be complete.

I didn’t realize you could declare a variable of sorts with FLATTEN like that! That makes it so much more readable. It’s a shame that Dataview doesn’t seem to support a | b booleans natively, but this is much more concise and readable than my implementation. Thank you @holroy!

I forgot about that part in your request, but dataview does indeed support AND and OR wherever you want boolean responses. I’ve not seen any language allowing days = 1 | 2, but you can write days = 1 OR days = 2.

Given a numerical value in pri and some random test cases each line below is valid as input to choice():

daysLeft = 1 OR daysLeft = 2
(daysLeft < 10 AND pri > 3) OR (daysLeft < 5 AND pri <= 3)
contains(list(3, 5, 7, 11, 13), daysLeft)

The first is a given, I reckon. The second checks whether it’s less than 5 or 10 days, depending on the priority given. The last case is if you want to do check against a list of values. Here we define the list of values, and check whether daysLeft is in that list using contains().

So dataview does indeed support both AND and OR, and also various list constructs and comparisons. See Objects, arrays and string operations for a multitude of other options, as well.

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