Dataview: Display Table From Parent Folder

Hello!
Currently new-ish to obsidian but I am picking it up quickly after switching from OneNote and I am attempting to setup an environment for my DND note usage.

What I am trying to do here and what I want to be able to do is pull NPC “locations” from their folder names (which are locations in my DND setting), essentially all I have to do is drag and drop NPC’s to the respective folder of where they are in my setting as that’s easier than editing.

What I’d like to do with Dataview is have a table set up to where the entries read the folder it’s in as part of the TABLE , rather than using FROM on a specific folder.

That way, I have a list of all tagged NPC’s without having to change internal data and can simply just move them from one “Location” folder to another “Location” folder. Essentially all it needing to take is list it’s parent folder somehow in Dataview.
Is this possible at all? Are there better ways to do this?

So a file in this context has three interesting variables:

  • name - in your case the NPC name
  • folder - the folder, aka location in your case
  • path - the combination of folder and name

So doing this.file.folder, file.folder or dv.current().file.folder should depending on context give you the location you want.

How do I apply this in full context as code?

I dont have experience in query type code so I dont know how to apply those as tables. Pardon me for my lack of knowledge…


I have this, but is there a way to only list the parent folder?

Try with regexreplace(file.folder, ".*\/([^\/]+)$", "$1") instead of file.folder. It’s using regex to remove the other parts of the folder structure. It’s kind of greek to many, but it should do the job.

If you show the entire query, someone could possibly also beautify all of it, so it looks a little nicer.

1 Like

That worked like a charm!

The current full query is just

TABLE regexreplace(file.folder, ".*\/([^\/]+)$", "$1")
FROM #NPC 
  • At the moment but I am experimenting and was just trying to find a nice and clean to be able to index things better and essentially have all my NPC’s listed and where they were at. Not sure what else could be good tables to call for ease, havent set up too many tags in my templates yet.

Quick update~

TABLE without id file.link as "", regexreplace(file.folder, ".*\/([^\/]+)$", "$1") as "##### Current Location"
FROM #NPC 

3 Likes

Just for the fun of it, try out this variant:

```dataview
TABLE WITHOUT ID file.link as NPC, Location
FROM #NPC
FLATTEN regexreplace(file.folder, ".*\/([^\/]+)$", "$1") as Location
SORT Location
```

This shows some interesting concepts which might come in handy down the road:

  • TABLE WITHOUT ID - This is just to remove the first column, so that we can add it later on with a different name :slight_smile:
  • file.link as NPC – And here we add that column using as NPC to rename the column from “File”
  • FLATTEN .... as Location - This a way to avoid having those pesky operations in the column definitions, and it also allows us to use Location elsewhere in the query for either limiting or output, or sorting it and so on
  • SORT Location – Can be followed by asc or desc, to sort in either direction

Have fun moving forward!

2 Likes

Thanks tons!!!
Wracking my brain for all the possibilities but this definitely helps thanks so much.

Im so sorry but after some work and experimenting, and attempting to look at the documentation-

How do you implement a filter for a portion of nested tags?
Currently just displaying file.tags but dont know how to filter how by certain tags.
For instance, essentially having the field display what faction they belong to.
IE: #faction/KingdomofArdens or any subtag starting with #faction/*

TABLE WITHOUT ID file.link as " ", Location as "##### Current Location", file.tags as "##### Allegiance"
FROM #NPC FLATTEN regexreplace(file.folder, ".*\/([^\/]+)$", "$1") as Location
SORT Location

There’s no way to glob a filter for nested tags is there? I just wish to display a certain nested tag and this will help for other fields later on too.

As a bonus, how would I also just clip the end of the tag to ONLY display the last half of the nested tag…?

Im so sorry to bug you again.

NEVERMIND! I found several methods and was learning a bit on the way. I may make a post about this so others who search for the same will be able to figure out more and at the very least do some reverse engineering of their own when they are new like me.

EDIT: I was typing when you posted the message above. Leaving this here just in case it helps anyone else.


This works in local tests:

```dataview
TABLE WITHOUT ID 
	file.link as " ", 
	Location as "##### Current Location",
	filter(file.etags, (x) => contains(x,"#faction/KingdomofArden")) as "##### Allegiance"
FROM 
	#NPC 
	AND 
	#faction/KingdomofArden  
FLATTEN 
	regexreplace(file.folder, ".*\/([^\/]+)$", "$1") as Location
SORT 
	Location
```

This works too! Thank you. I had developed a method where it actually may be useful to have multiple tags still showing as well but yours is great and more simpler than mine!

(The only thing wrong was the AND #faction/KingdomofArdens as that limited the queried files to only those with that tag but still better method than I had.

```dataview 
TABLE WITHOUT ID file.link as " ", Location as "##### Current Location", filter(file.etags, (x) => contains(x,"#Faction")) as "##### Allegiance" 

FROM #NPC 

FLATTEN regexreplace(file.folder, ".*\/([^\/]+)$", "$1") as Location 
SORT Location

1 Like

Great to have options. :+1:

1 Like

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