Dataview: Querying dynamic inline keys?

I’m building a system where I have a single file called Log.md, that contains a rolling list of log-entries. Every day when I enter a new entry there, it would automatically prepend something like this to the top of the note:

2022-12-29:: 21.45: Some piece of text here

2022-12-29:: 18.30: Previous entry here

2022-12-28:: 14.23: Entry from previous day here

Now, I would have a separate daily note, and I would like to fetch the entries from Log.md that match the date of the daily note using dataview. Ideally without having to hardcode the date to every daily note. How would I go about doing that, or is it possible at all? So in my example, 2022-12-29 daily note (Daily note is named YYYY-MM-DD) would contain two entries from the Log.md, 2022-12-28 would contain one. Also, in a perfect world, the date in the Log.md would be a link to the relevant daily note. But I’m not sure of dataview-key can be a link.

Things I have tried

I have checked the Dataview documentation, without any obvious solution. I have also checked the forums and various other sources, without any luck.

No, it can’t.

Usually, there’s no “obvious” solutions in plugin documentation. :slight_smile: Not a criticism, but it’s normal compared with our use cases.

For example, you use a not-so-normal field key. And you want to compare a key with a value (a key in date format but not a date a value which is a date!

In DQL (regular dataview queries) you can’t do that (because you need to define the key as a variable); in JS I don’t know (but I guess it is possible).

You would make it a lot easier for yourself if you change the format within Log.md to something like:

- [[2022-12-29]] 21.45: Some piece of text here
- [[2022-12-29]] 18.30: Previous entry here
- [[2022-12-28]] 14.23: Entry from previous day here

This would lend itself for various queries a lot easier. Would this be something you would be interested in doing?

Sure, I would be willing to change the format. The most important thing is that is shows the date and time of the entry, everything else is negotiable :). Using the example you used, how could I query the relevant dates in Dataview? I notice that they are not key::value pairs anymore.

The trickery to this is that now we’re able to process the items of the lists more easily, and can work the metadata of the lists. Please do execute each of them from within one of the daily notes to follow the progress, as you’ll hopefully also then see how to develop similar queries.

```dataview
TABLE WITHOUT ID item.text, item
FLATTEN file.lists as item
WHERE file.name = "Log"
```

(Update: Removed typo with this.file.name to file.name)

This will list each of the items of the lists from the file Log.md, and in addition it’ll list all the metadata associated with each item. In that lists you’ll see the outlinks item which is the one we’re particularly interested in. One vital portion of this query is that I’m using FLATTEN on the lists, to split them into the separate items of the list.

```dataview
TABLE WITHOUT ID item.text, item.outlinks
FLATTEN file.lists as item
WHERE file.name = "Log"
```

(Update: Removed typo with this.file.name to file.name)

Now the query shows the “list” of outlinks, which in our case should be only one, and we’ll enforce that using the [0] in the next query, and that link we can ensure is equal to the current link, which is then our answer. Let us switch over to a LIST in the same step.

```dataview
LIST WITHOUT ID item.text
FLATTEN file.lists as item
WHERE file.name = "Log"
  AND this.file.link = item.outlinks[0]
```

Now we’ve lost the extra column, and we’re limiting the output to just those actually linking to our example. A slightly different, and possibly more robust way to write the outlink test is the following:

```dataview
LIST WITHOUT ID item.text
FLATTEN file.lists as item
WHERE file.name = "Log"
  AND contains(item.outlinks, [[]])
```

This would allow for multiple outlinks in your lists, and it uses the [[]] to denote the current file link.

Finally, if you don’t want the date to show you could add some regex to the output to hide the link part in the daily note. So here is another version (showing multiple paths to achieve your goal):

```dataview
LIST WITHOUT ID text
FLATTEN file.lists as item
FLATTEN regexreplace(item.text, "^\[\[.*?\]\]", "") as text
WHERE file.name = "Log"
  AND contains(item.outlinks, this.file.link)
```

The regex matches from the start of the text, ^, two literal square brackets, \[\[, before the shortest amount of random characters, .*?, before two literal square brackets, \]\]. If found within text.item, it replaces the match with nothing, "". In essence, this removes any link at the start of the text.

Thank you for your help, really appreciated :). And your solution does provide exactly what I need. But there is one weirdness. Namely, the first two table-views provide zero results. The later list view-examples provide the relevant results and seems to work perfectly.

Typo on my behalf when in the daily note… Change them to file.name = "Log", and you’ll see the results.

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