Help with groupBy cdate.weekyear

What I’m trying to do

Trying to display a table grouped by weekyear with columns [weekyear, row.length].

Something like this:

| weekyear | row.length |
| 2024-01 | 4 |
| 2024-02 | 7 |
| 2024-03 | 5 |

Things I have tried

Tried searching through the doc and forum. Not understanding what I’m doing wrong. Outputs ’year’ or ‘day’ when I change the group variable. Currently providing ‘-‘ for weekyear and total number of notes in dv.pages folder instead of desired weekyear and total number of notes per weekyear.

```dataviewjs
let group = dv.pages('"Daily Notes"')
    .where(n => n.activity != null)
    .groupBy(n => n.file.cday.weekyear); 
dv.table(["weekyear", "count"],  
    group.map(n => 
        [n.key, n.rows.length]
    )
);
```

Try grouping by:
DateTime.fromISO(n.file.cday).toFormat("yyyy-WW")) or moment(n.file.cday.toString()).format("yyyy-WW")

```dataviewjs
let group = dv.pages('"Daily Notes"')
    .where(n => n.activity != null)
    .groupBy(n => DateTime.fromISO(n.file.cday).toFormat("yyyy-WW")); 
dv.table(["weekyear", "count"],  
    group.map(n => 
        [n.key, n.rows.length]
    )
);
```
```dataviewjs
let group = dv.pages('"Daily Notes"')
    .where(n => n.activity != null)
    .groupBy(n => moment(n.file.cday.toString()).format("yyyy-WW")); 
dv.table(["weekyear", "count"],  
    group.map(n => 
        [n.key, n.rows.length]
    )
);
```

Please be aware that using that format of yyyy-WW will be understood by dataview as a year/month combo. To follow ISO 8601 it’s better to use yyyy-[W]WW (or if I git the quotes wrong yyyy-'W'WW. I always confuse which format is correct where :slightly_frowning_face: ). That should give to something like “2024-W01”.

1 Like