Add orders to database items & show last n items

summary
Here are some ways to add orders to database items or show last n items. The main idea is relate all the items to one father page, then get a batch of page ids and their orders. All the methods can rank items by created time initially, and the last method can also work for last edited time.
tags
Notion
Formula
Example
date
Feb 6, 2021
English version
Here are some ways to add orders to database items or show last n items. The main idea is relate all the items to one father page, then get a batch of page ids and their orders. All the methods can rank items by created time initially, and the last method can also work for last edited time.

Add Orders

Orders - son database
Order
Name
Relate to Father
id
id_rollup
Created time
1
Feb 15, 2022 02:57 PM
1
Feb 15, 2022 02:58 PM
1
Feb 15, 2022 02:59 PM
1
Feb 15, 2022 03:01 PM
1
Feb 15, 2022 03:09 PM
Orders - father database
Name
id_rollup
Relate to son
id_rollup_formula
 
Initial Setting:
  1. Add a filter for son database. When add items, they will automatically relate to Father page. (If you have created some pages, you can select them all, right click, use edit property to add relation to Father page in batch.
    1. notion image
  1. Add formula property id() (It is the PageID).
  1. Use the Father page to get the rollup of ids. Then the son pages rollup this value (because we cannot directly rollup a rollup property, we need to create a formula property in father db for son pages to rollup.
  1. Using Regex replace, calculate the rank of son page’s id (which is equals to the number of characters before it / 32 + 1).
 
Notice:
  • The order is the same as relation order to Father page, which is created time by default. You can drag them to reorder.
  • I add round() to order formula because when we add a new item, the update of rollup may delay for a while. But since there are no match of this id, the result will + 0.9.... After round, it will be + 1, which is the correct result.

Show last n items

Here I use last 3 items as an example to show two ways.

Way 1: Get ids of last 3 items, judge if included

Way 1 of last 3 items - son db
Show all
Last 3 items
Name
relate to father
id
id of last 3 items
Created time
Feb 15, 2022 02:57 PM
Feb 15, 2022 02:58 PM
Feb 15, 2022 03:00 PM
Feb 15, 2022 03:01 PM
Feb 15, 2022 03:10 PM
Way 1 of last 3 items - father db
Name
relate to son
id_rollup
ids of last 3 items
Initial Setting:
  1. Add a filter for son database. When add items, they will automatically relate to Father page.
  1. Add formula property id()
  1. Use the Father page to get the rollup of ids.
  1. In ids of last 3 items formula, edit the last 3 to number of pages you want to show. Then we can use slice to get ids of last 3 items.
  1. Rollup ids of last 3 itmes in son pages. Add formula Last 3 items to judge id in last 3 items or not.
  1. Add a filter: Last 3 items is true.

Way 2: Get max timestamp one by one (can use in created time, last edited time or manual time)

Way 2 of last 3 items - son db
Show all
Name
relate to father
Created time
Timestamp
max1
exp max1
max2
exp max12
max3
exp max123
Feb 15, 2022 02:57 PM
NaN
Feb 15, 2022 02:59 PM
NaN
Feb 15, 2022 03:00 PM
NaN
Feb 15, 2022 03:01 PM
NaN
Feb 15, 2022 03:48 PM
NaN
Way 2 of last 3 items - father db
Name
relate to son
max1
max1_
max2
max2_
max3
max3_
Initial Setting:
  1. Add a filter for son database. When add items, they will automatically relate to Father page.
  1. Add created time (or last edited time, or manual time) and generate timestamp().
  1. Get the max timestamp using Father page (rollup - Calculate - Max).
  1. Judge if equals to timestamp of son page. Get the max timestamp and label to 0.
  1. repeat step 3, 4 n times.
  1. items labeled 0 are the last n items
Notice: This method require different timestamps for every items (means the time property can not be the same). Possible improvement: add data to last four digits, like add different numbers manually, add the length of Name property, convert content of Name to numbers... Just need to keep every “timestamp” different.

© Niin Ni 2021 - 2024