First of all apologies if this is incredibly simple…but it has cost me the better part of one day and I still haven’t figured it out.
What I’m trying to do
In my weekly note, I want to have a simple line entry of some habits. Examples.
Exercise:: type, duration, distance
Exercise:: Walk, 20 min, 2.0 Km
Exercise:: Run, 60 min, 10.0 Km
Ideally, I would like to add other types, and instead of distance the number of repetitions.
In my monthly overview, I would like to have a dataview table showing the exercises duration and distance per type.
Things I have tried
TABLE exercise
FROM "2.0 CALENDAR/2.3 Periodic Notes"
WHERE exercise != null
SORT file.name DESC
Yielded:
and
TABLE Type, Duration, Distance
FROM "2.0 CALENDAR/2.3 Periodic Notes"
WHERE exercise != null
FLATTEN split(exercise, ", ") AS parts
WHERE length(parts) >= 2
SORT DESC
efforts like:
TABLE Type, Duration, Distance
FROM “2.0 CALENDAR/2.3 Periodic Notes”
WHERE exercise != null
FLATTEN regexmatch(“^(.*?), (\d+ min), (\d+ K)$”, exercise) AS parts
LET Type = parts[0]
LET Duration = parts[1]
LET Distance = parts[2]
SORT file.name DESC
only provided error message. I also tried a ‘slice’ command but haven’t been able to create anything useful. Hopefully the community can help/
Note the difference between FLATTEN split(...) as parts and FLATTEN list(split(...)) as parts. The former would perform the split, and then expand each row to every part it split out into. The latter will keep the list, allowing for using parts[0] as type, parts[1] as duration, and so on.
So try the following:
```dataview
TABLE parts, Type
FROM "2.0 CALENDAR/2.3 Periodic Notes"
WHERE exercise != null
FLATTEN list(split(exercise, "\s*,\s*")) AS parts
WHERE length(parts) >= 2
FLATTEN parts[0] as Type
SORT DESC
```
In addition to just splitting on the comma, I also ignore the whitespace characters surrounding the comma. The table should produce the parts list and just the Type as proof of concept.
```dataview
TABLE Type, Duration, Distance
FROM "2.0 CALENDAR/2.3 Periodic Notes"
WHERE exercise != null
FLATTEN list(split(exercise, "\s*,\s*")) AS parts
WHERE length(parts) >= 2
FLATTEN parts[0] as Type
FLATTEN parts[1] as Duration
FLATTEN parts[2] as Distance
SORT DESC
```
@holroy I did notice that when I have multiple entries for exercise in a file, it doesn’t pick them up, only when there is only one entry. Any suggestions on how to correct this?
```dataview
TABLE Type, Duration, Distance
FROM "2.0 CALENDAR/2.3 Periodic Notes"
WHERE exercise != null
FLATTEN flat(list(exercise)) as oneExercise
FLATTEN list(split(oneExercise, "\s*,\s*")) AS parts
WHERE length(parts) >= 2
FLATTEN parts[0] as Type
FLATTEN parts[1] as Duration
FLATTEN parts[2] as Distance
SORT DESC
```
The flat(list(...)) ensures that it’s always considered a list which is then reliably flattened into each oneExercise.