10 mars 2024 - u/_sopa0405 https://www.reddit.com/r/Notion/comments/1bboj7v/

So, im making a notion class organizing dashboard for my mom who is a teacher. I have two databases linked with a relation property: One for each lesson (different days) and one for each classgroup (group of students). The lessons database is a calendar view and has some templates for each classgroup that repeat in the days she gives lesson to them. If she gives class A lessons every day of the week, it would create a new page in the lessons database in each day of the week at midnight so she can make some registration in class. I want to make it so it counts every lesson and adds it as a property. I'm having difficulties making a formula for it. It would be like "Lessons 20 and 21" or if that day we only have one class "Lesson 20"

This is what i have come up with. It doesnt work tho.

I need it to be able to show the diferent lesson numbers for each classgroup. To sum up, I need a formula that counts each lesson number for each classgroup. Some of the days will have 2 lessons depending of the day of the week. It should be able to handle that too.


Quick answer

Initially, the title of this page was "Show the last two lessons of the day", since this was what seemed to be requested initially. The “quick answer” provides a solution to this initial question. The Advanced answer provides another solution to another question and the Even more advanced answer goes even further.

<aside> <img src="https://prod-files-secure.s3.us-west-2.amazonaws.com/9210293a-6b0f-4638-8e8d-3c40b40ed42b/b8366055-a446-4861-aa67-1e926814ca74/Invisible.png" alt="https://prod-files-secure.s3.us-west-2.amazonaws.com/9210293a-6b0f-4638-8e8d-3c40b40ed42b/b8366055-a446-4861-aa67-1e926814ca74/Invisible.png" width="40px" />

<aside> ℹ️ **Information text

  1. Retrieves the lessons associated with a class, then filters them to keep only those of the day (regardless of the time) and sorts them in reverse chronological order

  2. Creates a list containing only the last 2 courses (the list will contain nothing if there are no courses, otherwise only one or two elements)

  3. If the list is empty, indicate this with a dedicated message, otherwise display the day's lesson or the two lessons of the day (in chronological order, with an index specifying whether it is the first or second lesson)**

</aside>

lets(
	today , parseDate("2024-03-11").formatDate("YYYYMMDD"),
	/* Simulate the actual date. Replace by now().formatDate("YYYYMMDD") to get the true date of today */
	
	lessonsOfTheDay , prop("Lições").filter(current.prop("Data").formatDate("YYYYMMDD") == today).sort(current.prop("Data")).reverse(),
	lessons_twoLast, [lessonsOfTheDay.at(0), lessonsOfTheDay.at(1)].filter(current),
	
	lessonsOfTheDay
		? lessons_twoLast
				.reverse()
				.map(
					current.format() + (lessons_twoLast.length() > 1 ? "-" + (index+1) : "")
				)
				.join("\n")						
		: "No lesson today"
)

Lições

Grupos de turma - DB

</aside>


Advanced answer

<aside> <img src="https://prod-files-secure.s3.us-west-2.amazonaws.com/9210293a-6b0f-4638-8e8d-3c40b40ed42b/e2194ab3-d449-4a4a-9531-fc7fcf62410f/Invisible.png" alt="https://prod-files-secure.s3.us-west-2.amazonaws.com/9210293a-6b0f-4638-8e8d-3c40b40ed42b/e2194ab3-d449-4a4a-9531-fc7fcf62410f/Invisible.png" width="40px" />

<aside> ❗ ****

It seems that the basic construction is more complex than described. The desired formula must be in the lessons database ("Turmas"), several classgroups can be associated with a lesson, and lesson names include a relative date.

**Here's the desired basic structure and various solutions.

It requires two new properties : Criado em (creation Date) and ID (Identifier)**

</aside>

lets(
	lessons ,
	prop("Turmas")

 /* Retrieves all the lessons from each "Turmas" and sorts them in order to get the oldest first. If the date is the same, the creation date tells you which is the oldest */
	.map(
		current.prop("Aulas")
		.sort(current.prop("Criado em"))
		.sort(current.prop("Data"))
		),
lessons
/*  Find, among the groups of lessons previously generated, the indexes corresponding to the lesson. */
	.map(	
		"Lição" + " n° " + (current.findIndex(current.prop("ID")==prop("ID"))+1).style("b,blue")
		+ (lessons.length() > 1 ? " " + prop("Turmas").at(index).format().style("c,grey") :"")
		)
		
	.join("\n") /* Display results on different lines if required. Allow "Wrap column" */
)
prop("Turmas")

 /* Retrieves all the lessons from each "Turmas" and sorts them in order to get the oldest first. If the date is the same, the creation date tells you which is the oldest */
	.map(
		current.prop("Aulas")
		.sort(current.prop("Criado em"))
		.sort(current.prop("Data"))
		)
	
/*  Find, among the groups of lessons previously generated, the indexes corresponding to the lesson. */
	.map(	
		"Lição" + " n° " + (current.findIndex(current.prop("ID")==prop("ID"))+1).style("b,blue") + " "
		+ prop("Turmas").at(index).format().style("c,grey")
		)
		
	.join("\n") /* Display results on different lines if required. Allow "Wrap column" */

Turmas

Aulas

</aside>


Even more advanced answer

15 mars 2024 - u/_sopa0405 Just one question: How can i implement 2 lessons in the same page? In case of the lesson being 2 hours long instead of 1. Like displaying Lição nº 20 e 21.

u/Ouinx2

There's a recurring trick when you help someone with Notion: as soon as they're satisfied, they ask for more ^^.

Don't think that what I did was easy ^^. Adding the possibility of considering that a page can count double obviously adds complexity.

And often, when you have to go further, it calls the whole formula into question.

Here, there's no question of rethinking everything, so we'll make do with what's already been created.

The formula first creates a list of all the lessons. If a lesson is doubled, then it must appear twice. So we need something to indicate this. This could be a word in the lesson title or a new checkbox property. Let's try the latter, it'll be easier. → New checkbox property Aula dupla.

When the checkbox is selected, the lesson must be duplicated in the list. This is done by adding this little piece of code :

.map(current.prop("Aula dupla")? [current , current] : [current]).flat()

Next, the formula uses the list of lessons to calculate the lesson number according to its position in the list.

It still works without changing anything in the formula I made (because it's well conceived from the start ^^). The only problem is that we need to be able to identify doubled lessons so that we can indicate "lesson 20 & 21" instead of "lesson 20". It's a bit tricky, but it's possible :

First find the position of the lesson within the list (position variable), then use this position to retrieve the status of Aula dupla (isDouble variable). As Aula dupla is a checkbox, its status is either FALSE or TRUE.

position , current.findIndex(current.prop("ID")==prop("ID"))
isDouble , current.map(current.prop("Aula dupla")).at(position)

All that remains is to indicate the lesson number in text form, adding a test: if the lesson is double, then indicate "& position+2". You can even use the variable isDouble to write Lições instead of Lição.