Help with Dataview inside template

Here’s my query… fairly basic Obsidian user so please bear with me if this is easy and I’ve missed an obvious point, or if I don’t explain things properly.

Also, please tell me if you think my set up is too clunky and needs refinement. I’m always happy to take on board the views of others.

I use Obsidian as my central note taking application for work. Each client has a “hub” page and I link all other notes about that client from that hub.

I do this manually normally, so I’ll be in the client’s hub page and then create meeting notes, for example.

But just in case I miss any of them, as the bottom of the hub page I have a dataview table that catches all notes relating to that client. It does this by filtering based on their unique client reference number, which I store within properties of each note.

At the moment, I am manually adding in that dataview query every time I create a new hub. My hub template is generated by the Templates plugin. It includes the dataview query on the hub page but at the moment I am manually typing in the unique reference number.

Is there any way I can automate the process of the unique reference number appearing in the dataview formula?

Thanks so much.

It should be possible, but please show examples of how you define that custom number in your hub pages and/or the notes linking to that client. Could either of the notes also link to multiple clients? If so, how does that look?

Hi there,

Thanks very much for your response.

At the moment, I set up the metadata in the properties section as follows:

The Dateview query looks like this:

I put the unique reference number (called a case_number hearing, due to the system that generates the number) between the quotation marks in the penultimate line.

The customer number is truly unique and is two letters, followed by two numbers, then a further letter and 5 five numbers. For example: BT23A56049. The number seems random, but I inherit that from another source. It can’t possibly link to two different clients. I never need to link the note to two different clients.

I hope all of that makes sense.

The simplest variant of this could be:

```dataview
TABLE file.ctime as "Date created", file.size as Size
WHERE case_number = this.case_number
SORT file.ctime
```

This requires that your hub-page also has that unique case_number. If you’d want to exclude the hub page from the result you could either use a different name for this variable in the hub page, or exclude the current file from the result. In the next query we do both (just to save a little space, but you can choose either variant):

```dataview
TABLE file.ctime as "Date created", file.size as Size
WHERE case_number = this.hub_case_number
  AND file != this.file
SORT file.ctime
```

On a sidenote, I’m an advocate for not relying on the creation date of files as my experience is that it can be changed to easily when syncing, backup/restore, copying, …, so I always set the date property to be the day of creation from my templates (and use the YYYY-MM-DD format). This allows for the use of file.day in dataview queries, and is in my opinion a better option. file.day gets its value either from a properly formatted date within the file name, or the date field of that file.

Your query would then look like:

```dataview
TABLE file.day as "Date created", file.size as Size
WHERE case_number = this.case_number
  AND file != this.file
SORT file.day
```

( or the other version using something like hub_case_number )


In either case, the main thing in all of these queries is that prefixing the variables with this. in a DQL query makes it look at the current file’s information instead of the files you’re querying on.

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