Dataviewjs query to find files with a date

Things I have tried

I have tried to look at the community help to figure out what I need to find the answer.

What I’m trying to do

I am trying to create a dataviewjs query that finds a field that contains a date and another field that does not contain a date. so, I want the query to show all of the files that contain a started date, but where the finished date is empty.

Here is what I have so far

for (let group of dv.pages("#project").for (let group of dv.pages("#project").where(p => p["dateFInished"] == null &&  p["dateStarted"].includes("YYYY-MM-DD")) {
    dv.header(4, group.key + ": " + group.rows.length);
    dv.table(["Project", "Date Started", "Progress"],
        group.rows.sort(k => k.dateStarted, 'desc').map(k => [k.file.link, k.dateStarted, k.progress,
             
           ])) 
}

Hi Saorsa,
Are you trying to groupBy something? You don’t describe that in your query but your code is rather complicated!

Linebreaks before the .where etc. are optional, here for readability in the forum.

const result = dv.pages("#project")
.where(p => p.dateStarted && p.dateFinished === null)
.where(p => dv.date(p.dateStarted) !== null)
.sort(p => p.dateStarted, 'desc')
.map(p => [p.file.link, p.dateStarted, p.progress] );
// console.log(result); // uncomment the beginning of this line to check things
dv.table(["Project", "Date Started", "Progress"], result);

The first .where checks that there is a dateStarted but no date finished (based on the logic from your code). (Have you checked using console.log() to make sure that part is working?)
The second .where checks to see whether the dateStarted can be successfully turned into a date, which will hopefully work for what you want to check there. If not, can you please share an example of something in the dateStarted field that you want to include that isn’t showing up or exclude that is showing up?
Then I just sort and map as you did in your example code.

Hope this gives you some steps in the right direction!

Thank you that works just like I was hoping. Your explanation is so helpful too as I am not a programmer and am just learning by doing and trying to copy other people until I get it. I was actually using some code from someone else that grouped by categories that I thought I removed, so that is what that was.

I am now having some trouble with my progress bar. I am using some code that works on a different table, but can’t get it to work here. I have fields in my note that gives a number for projects and total projects, but I am not sure what is breaking it. The error says Unexpected token ‘]’

Here is what I have

const result = dv.pages("#project")
.where(p => p.dateStarted && p.dateFinished === null)
.where(p => dv.date(p.dateStarted) !== null)
.sort(p => p.dateStarted, 'desc')
.map(p => [p.file.link, p.dateStarted, && k["projects"] / k["Total"] < 1 ? "<progress value=" + k["projects"] / k["Total"] + " max=1></progress>"]] );
// console.log(result); // uncomment the beginning of this line to check things
dv.table(["Project", "Date Started", "Progress"], result);

I was using the following code as my guide.
It would be great to be able to add the button as well, but I know its a lot and if I can just get the progress bar working I will be happy.

const { 
	update 
} = this.app.plugins.plugins["metaedit"].api 
const buttonMaker = (pn, pv, fpath, text) => { 
	const btn = this.container.createEl('button', { 
		"text": text, 
		"class": "button-55" 
	}); 
	const file = this.app.vault.getAbstractFileByPath(fpath)
	btn.addEventListener('click', async (evt) => {
		evt.preventDefault(); 
		await update(pn, pv, file); 
	}); return btn;
} 
const 
	{ createButton 
} = app.plugins.plugins["buttons"] 


for (let group of dv.pages("#book").where(p => p["time-read"] == null && moment(luxon.DateTime.now().toISODate()).diff(moment(p["release-date"])) < 0).groupBy(p => p.dateStarted == null ? "Not dateStarted" : "Currently Reading")) { 
	dv.header(4, group.key + ": " + group.rows.length);
	dv.table(["Title", "Author", "Series", "dateStarted On", " "],
		group.rows.sort(k => k.dateStarted, 'desc').map(k => ["[[" + k.file.path + "|" + k["Title"] + "]]", k["Author"], k["Series"] ? k["Series"] + ", " + k["number-in-series"] : "N/A", k["dateStarted"] ? k["dateStarted"] : "Untouched", k["dateStarted"] && k["pageprogress"] / k["pagecount"] < 1 ? "<progress value=" + k["pageprogress"] / k["pagecount"] + " max=1></progress>" : 													  
            createButton({ 
	             app, 
                 el: this.container, 
                 args: { 
                   name: k["dateStarted"] ? "Completed" : "Begin"
                }, 
			    clickOverride: { 
                    click: update,
                    params: [k["dateStarted"] ? "time-read" : "dateStarted", luxon.DateTime.now().toISODate(), k.file.path] 
                } 
			}) 
		])) 
}

It would be great to add the button as well, but I

That error almost always means that your begin and end brackets [ ] aren’t matched up. I suspect you’ve got one too many ] at the end of your really long line, but I’m not on a device where it is easy to check.
I also notice you have an &&, which is the equivalent to the word AND in saying “this AND that are both required”, that’s hanging out with nothing on its left side, which doesn’t make much sense to me. That is a separate thing to check.

Sorry, I am not familiar with the buttons plugin! I think there are some other folks on the forum who are, though, hopefully one of them can help!

So I tried changing it to this by trying the things that you suggested to see if I could get the progress bar to work.

const result = dv.pages("#project")
.where(p => p.dateStarted && p.dateFinished === null)
.where(p => dv.date(p.dateStarted) !== null)
.sort(p => p.dateStarted, 'desc')
.map(p => [p.file.link, p.dateStarted, k["projects"] / k["Total"] < 1 ? "<progress value=" + k["projects] / k["Total"] + " max=1></progress>"
// console.log(result); // uncomment the beginning of this line to check things
dv.table(["Project", "Date Started", "Progress"], result);

but i am now getting a different error.
Evaluation Error: SyntaxError: Unexpected identifier

Would it be easier to try to do this with a regular dataview js table? Or is it necessary because of the parameters I am trying to achieve.

I’m still a little worried about your [ matching.

Just for testing, can you try this simplified version that displays the progress as a decimal number rather than with the progress bar? I don’t think the progress bar is causing the problem, just that making the code simpler should help with finding the problem.

const result = dv.pages("#project")
.where(p => p.dateStarted && p.dateFinished === null)
.where(p => dv.date(p.dateStarted) !== null)
.sort(p => p.dateStarted, 'desc')
.map(p => [p.file.link, p.dateStarted, (p.projects / p.total).toFixed(2)]);
// console.log(result); 
dv.table(["Project", "Date Started", "Progress"], result);

If you use the code above, does it work without an error? Do you see a number in the “Progress” column? Does it seem correct? One error I could imagine happening with this code is if “Total” is somehow 0. That’s not the error you are getting currently, though, so maybe it won’t be an issue for you.

What do you mean here?

So yes, I can see why that was confusing. So what I meant to ask is, can the query be done in a basic dataview query or does it need to be dataviewjs because of the complexity. I do get a correct number in the progress column, but just can’t seem to figure out the code for making it show as a progress bar. I have had success with progress bars in a basic query, but have not been able to replicate it in dataviewjs.

Thank you for taking so much time to help me out with this. I really appreciate it.

Oh, that’s a great idea! No, the progress bar itself was the only part that I would have worried about being too complex, since it has raw HTML. If you know how to do that bit in regular dataview, the rest translates pretty easily:

TABLE 
...
FROM #project
WHERE dateFinished = null AND dateStarted AND date(dateStarted) != null
SORT dateStarted DESC

… in the above is for your columns AS columnName, including your progress bar, which it sounds like you know how to do!

Really good idea!

I tried this and it looks like it will work great, except it is only finding one file. I tried to see what is different about it, but can’t see anything. Any ideas?

Oooops! Likely I made an error in one of the “WHERE” bits. You could take out each of the different pieces and see if the results start to make sense.

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