Count specific value with dataviewjs inline query?

Hello!

I want to customize Dashboard file to count specific value across all notes.

In dashboard file there is an inline query that counts favorite tags in all files,

-   🔖 Tagged: favorite `$=dv.list(dv.pages('#favorite').sort(f=>f.file.name,"desc").limit(4).file.link)`

but I want to count another value, status: closed.

How I should add WHERE command that I could specify contains,closed status for example?

Thanks a lot,
Best

If your pages have Dataview status fields like this:

status:: closed

…then this might do the trick:

- 🔖 Tagged: favorite `$=dv.list(dv.pages().where(page => page.status == "closed").sort(f=>f.file.name,"desc").limit(4).file.link)`
2 Likes

That query doesn’t count anything, it shows the first four elements in your list. So to get the first four closed tasks, you can do as the other post suggest, but if you want to count elements, you need to change the query a little more.

Maybe something like the following:

- Closed page count: `$= dv.span( dv.pages().where(page => page.status == "closed").length )`
2 Likes

Hello!

Thank you so much! both codes works for another two different fields on my vault.

But I’m still confused to how use the WHERE and FROM in these inline queries :melting_face: there is any documentation that explain them?

Some field have multiple values and in different folders so it’s hard to count specific value in these inline query. For example one of my fields are book subjects which every book may have multiple subject and set an inline query to count one of the subjects is the issue.

Thank you so much,
Best

Are you able to list those subjects in an ordinary DQL query?

Either way you’d need to use flatten to split those fields having multiple values, in order to access/count them as separate entities.

The where and from clauses, and some of the other constructs in dataviewjs uses a function construct which takes some getting used to, but hopefully when seeing enough examples and playing around with them you’ll get the hang of it.

1 Like

Hello!

I was able to make LIST and TABLE to extract and count specific subjects using ```dataview query but still unable to do same for inline queries :frowning:

I’m not sure if I write the YAML wrong even regular DQL query should failed.

Here is my current setup:

YAML for subject:

subjects: world_history, africa_history, egypt, pyramids, 

Dataview Table:

TABLE WITHOUT ID id as "Book ID"
WHERE contains(subjects, "africa_history")

Create a table of all books about History of Africa not just Egypt and sort them by Book ID.

Now, when I try to use inline query the result is zero:

`$= dv.span( dv.pages().where(page => page.subjects == "africa_history").length )`

Any help appreciated,
Best

Try with page => page.subjects? .includes("africa_history") as the where clause. It’s the multiple values which most likely are setting you up for failure.

Do also test either query when there is only one value in the subjects field, to see/feel the paindifference.

Thank you so much for your help!
I try this line but there is a syntax error:

Dataview (for inline JS query ‘dv.span( dv.pages().where(page => page.subjects? .includes(“africa_history”).length )’): SyntaxError: Unexpected token ‘.’

I reloded plugins and check the inline settings but still same error.

Best

It should be:

`$= dv.span( dv.pages().where( page => page.subjects?.includes("africa_history") ).length )`

You had a misplaced parentheses. :slight_smile:

2 Likes

My bad :see_no_evil:

Thank you so much @holroy! :smiling_face:

Best

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