How can I use Dataview to group my daily notes index list by year/month?

What I’m trying to do

I generate daily notes with filenames like YYYY_MM_DD_work_journal.

I have a query like this:

LIST FROM #work_journal and -#hub
SORT file.ctime desc

So I get the following, as expected.

image

I’m wondering if I can have this list grouped by month/year, like

* 2022
  * January
    * 2022_01_27_work_journal
    ...

Things I have tried

LIST FROM #work_journal and -#hub
SORT file.ctime desc
GROUP BY file.ctime.month as Month

But I just get the number 1.

Any suggestions?

Hi @adarsh

When you use the GROUP BY functionality, everything is returned in a rows array, so your list is only showing the group indicator (1 as it appears all your notes have been created this month).

What you want to do now is to play around and see what data is in the grouping. Like I say that data exists in an array called rows.

If you change your dataview query to the following, you should see all your data nestled inside:

```dataview
LIST
		rows
FROM
		#work_journal and -#hub
GROUP by
		file.ctime.month as Month
```

This will return a lot of data so you will need to dig down to find what you want.

Based on your screen shot from the non-grouped query, we can return the link to the note as follows:

```dataview
LIST
		rows.file.link
FROM
		"Personal/Journal/Daily"
SORT
		file.ctime desc
GROUP by
		file.ctime.month as Month
```

This will return something like the following:

image

There might be a way to convert the month number that file.ctime.month returns to make it prettier and more readable, but I’ve just started working with dataview over the past couple of weeks and have not looked into that. I’m also not sure if you can group multiple times like you would like for year and month. Well, you can group multiple times, but again I’m not skilled enough in the plugin to get it to return meaningful data from the multiple groupings.

I’ve tried multiple groupings like the following:

```dataview
LIST
		rows.rows.file.name
FROM
		"Personal/Journal/Daily"
SORT
		file.ctime desc
GROUP BY
		date(file.name).year as Year
GROUP BY
		date(rows.file.name).month as Month
```

And the result is not what I would expect

image

Note that I don’t see a year value, but based on the groupings we can see that they are indeed grouped by year. I don’t know if this is a shortcoming of the plugin or just my not understanding the plugin at a deep enough level to make this work. If you figure it out, I would love to hear how you did it.

Hopefully this gets you a little bit further down your path to getting what you’re looking for.

1 Like

Hi.
About the date output format I suggest this:

LIST rows.file.link
FROM #work_journal AND -#hub
WHERE file.ctime.year = 2022
GROUP BY dateformat(file.ctime, "yyyy-MM") AS MON
SORT MON DESC

Using the function dateformat(date, "format")
You can replace “yyyy-MM” by “MMMM” to get month as a string.
See luxon formats: luxon/formatting.md at master · moment/luxon · GitHub

I add WHERE file.ctime.year = 2022 to filter the year, but you can remove it.

About the daily note title format, I suggest the use of the format “YYYY-MM-DD” (or “YYYY-MM-DD with other words”) because you can get date info from this format (in some cases people create the daily note before the “real” day… and in cases like that file.ctime isn’t the best source).
For titles with the suggested format is possible to use the implicit field file.day.

2 Likes

Thanks for showing me how to format the date. This uncomplicates a lot of things that I like to try to overly complicate. I’ve still got a lot to learn.

2 Likes

And I have a lot to learn with you. :slight_smile:

2 Likes

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