Dataview JS Query - Limit function not showing most recently modified

I use the following Dataview JS query to show all Client files in a specific folder. I’ve tried to limit the query to show the 10 most recent client files (as per file modification date) by using the “.limit(10)” and “.sort(k => k.file.mtime, ‘desc’)” functions, but the results are not displaying correctly.

The final results only display the 10 oldest client files that I have. When I remove the “.limit(10)” function it works perfectly, but is slow to load given the large number of client files in my vault.

Is there any way to limit the query to display only 10 results, while also showing the most recently modified files?

for (let group of dv.pages('"_7_Contacts/_7.0_Contact List" and !#dashboard and !#sub-dashboard').limit(10).groupBy(p => p.contact)) {
  dv.table(["Name", "Email", "Phone", "Client", "Modified"],
    group.rows
      .sort(k => k.file.mtime, 'desc')
      .map(k => [
        k.file.link,
        k['email'],
        k['phone'],
        k['Client'],
        k.file.mtime
      ]))
}

Hello Npop,

To limit the number of records to 10 in your JavaScript snippet, it appears that you’re already using the .limit(10) method correctly. However, the groupBy function might affect how the results are grouped and displayed. If your goal is to display only 10 records in total, regardless of the grouping, you might need to apply the limit after the grouping.

let allPages = dv.pages('"_7_Contacts/_7.0_Contact List" and !#dashboard and !#sub-dashboard');
let groupedPages = allPages.groupBy(p => p.contact);

for (let group of groupedPages) {
  let sortedGroup = group.rows.sort(k => k.file.mtime, 'desc').slice(0, 10);
  dv.table(["Name", "Email", "Phone", "Client", "Modified"],
    sortedGroup.map(k => [
      k.file.link,
      k['email'],
      k['phone'],
      k['Client'],
      k.file.mtime
    ]))
}

In this version, after grouping, each group is sorted by the modification date and then limited to the first 10 records using `.slice(0, 10). This ensures that only the first 10 records will be displayed for each group.
I hope this solution works well.
Bye

Works perfectly! - Thank you

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