Dataview: enhanced choice()-function

Hi all,

I need help with more complex dataview-functions…

My current workaround

I have a property-key with different value-options.
Let’s say the property-key is “sensor_type” and it could have one or multiple of the following values:

  • temperature
  • humidity
  • CO2
  • surface
  • flush-mounted

Now, I can create a dataview with 5 columns like this:

```dataview
Table choice(contains(file.frontmatter.sensor_type, "temperature"), "🌡️", "") AS "Temp.", choice(contains(file.frontmatter.sensor_type, "humidity"), "💦", "") AS "rH", choice(contains(file.frontmatter.sensor_type, "CO2"), "😤", "") AS "CO2", 
choice(contains(file.frontmatter.sensor_type, 
	"surface"), "✅", 
	choice(contains(file.frontmatter.sensor_type, "flush-mounted"), "❌", "")) AS "surface?"
From #note/Thing
```

What I need

But if I want to create more complex dataviews, I would like to have a some kind of more complex function, in which I can case-query for these values and to only have one column in the dataview. … => something like this imaginary case-function:

```dataview
Table case(sensor_type, "temperature", "🌡️", "humidity", "💦", "CO2", "😤", "surface", "✅", "flush-mounted", "❌")
From #note/Thing
```

the dataview result for the following Sensors:
Sensor 1:

sensor_type: 
   - temperature
   - humidity
   - surface

Sensor 2:

sensor_type:
   - temperature
   - surface

Sensor 3:

sensor_type:
   - CO2
   - surface

should then look like this:

File sensor_type
Sensor 1 :thermometer::sweat_drops::white_check_mark:
Sensor 2 :thermometer::white_check_mark:
Sensor 3 :triumph::white_check_mark:

Is there already an existing function, to create these kind of dataviews?

Thank you very much in advance!
kind regards,
Silias

The thing you’re looking for is a combination of map() and object(), where we first build a lookup object, and then map each value of the sensor_type according to the object.

Try the following query:

```dataview
TABLE sensor_icons as "Sensor type"
FROM #note/Thing
WHERE sensor_type
FLATTEN join(map(sensor_type, (m) =>
  object(
    "temperature", "🌡️",
    "humidity", "💦",
    "CO2", "😤",
    "surface", "✅", 
    "flush-mounted", "❌")[m] ), "") as sensor_icons
```

Which could produce output like this:
image

2 Likes

very nice, thank you very much! :slight_smile:

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