bud write me a dataview for obsidian to give me 2 columns: first is tags and second is…
- User
bud write me a dataview for obsidian to give me 2 columns: first is tags and second is the total count of files with those tags
- Assistant
Certainly! You can use the Dataview plugin in Obsidian to create a table showing each tag and the total count of files that contain that tag.
Here is a DataviewJS code snippet that does that:
dataviewjs// Get all pages const pages = dv.pages(); // Create a map to count tags const tagCounts = new Map(); for (const page of pages) { if (!page.file || !page.file.tags) continue; for (const tag of page.file.tags) { // Normalize tag (remove leading #) const cleanTag = tag.startsWith("#") ? tag.slice(1) : tag; tagCounts.set(cleanTag, (tagCounts.get(cleanTag) || 0) + 1); } } // Convert map to array and sort by count descending const tagArray = Array.from(tagCounts.entries()) .sort((a, b) => b[1] - a[1]); // Render a table with tag and file count dv.table(["Tag", "Count"], tagArray); - User
tf, cant davaview by itself do this?
- Assistant
Great question! Dataview's standard query language (DataviewQL) doesn't have built-in aggregation functions like
group byorcount by tagdirectly. That’s why for tasks such as "show tags with counts across all files," using DataviewJS (a JavaScript API inside Obsidian for custom queries) is usually necessary.
What DataviewQL can do:
You can list files with their tags, but you can't directly group and count tags with a simple Dataview table query, like this:
dataviewTABLE file.tags AS Tags FROM ""This shows files and the tags per file but not an aggregated count of how many files have a tag.
Why DataviewJS is needed:
DataviewJS allows you to run JavaScript over the metadata so you can: