How to use LIMIT to retrieve the most recent items from a data store, yet sorted chronologically?

I have a database of chat messages. I wish to take the 10 most recent chats from the data store, and need them sorted chronologically (aka ascending by date) and then put into JSON.

However when I apply a limit to an ascending sort, it always starts with the FIRST 10 items…whereas I actually want the LAST 10 items.

How best to handle? Thanks!

Something like this perhaps? You should use Descending instead of Ascending.

Hope this helps! Let me know if there are any further questions or issues.

@samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!

That would select the 10 most recent, but I then need the selected items to be chronological.

So is there a way to then flip the order of the 10 items before putting them into JSON?

Did you also implement the date sort in the Search module’s Sort field?

Please refer to the above image.

If you need further assistance, please provide the following:

1. Relevant Screenshots

Please share screenshots of your scenario, any error messages, relevant module fields, and filters in question? It would really help other community members to see what you’re looking at.

You can upload images here using the Upload icon in the text editor:

2. Scenario Blueprint

Please export the scenario blueprint file to allow others to view the mapped variables in the module fields. At the bottom of the scenario editor, you can click on the three dots to find the Export Blueprint menu item.

3. Output Bundles of Modules

Please provide the output bundles of the modules by running the scenario (or get from the scenario History tab), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.

A. Upload as Text File

Save each bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.

B. Insert as Formatted Code Block

If you are unable to upload files on this forum, alternatively you can paste the formatted bundles.
These are the two ways to format text so that it won’t be modified by the forum:

  • Method 1: Type code block manually

    Add three backticks ``` before and after the content/bundle, like this:

    ```
    content goes here
    ```

  • Method 2. Highlight and click the format button in the editor

Providing the input/output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

Following these steps will allow others to assist you here. Thanks!

Hope this helps! Let me know if there are any further questions or issues.

@samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!

Yes, but neither DESCENDING nor ASCENDING perfectly fits my needs, because when I apply “LIMIT”, I have no control over whether it’s taking the beginning or end of the items.

Let’s say there’s 30 items. If I do “descending with limit 10”, I get “30, 29, 28, 27, 26, 25, 24, 23, 22, 21.”

If I do “ascending with limit 10” I get “1, 2, 3, 4, 5, 6, 7, 8, 9, 10”

But what I actually want is “20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30”

I don’t think a screenshot is necessary here, but here you go, thank you in advance for an suggestions!

So once you have this, simply use an Array Aggregator to aggregate to an array, then reverse the array to get “21, 22, 23, 24, 25, 26, 27, 28, 29, 30”. What else am I missing???

You can use the built-in function reverse

e.g.:

reverse(1.array)

Screenshot_2024-09-03_140912

For more information, see the function documentation in the Help Center.

Hope this helps! Let me know if there are any further questions or issues.

@samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!

Thanks, but it’s not actually a simple array. It’s multifield json and I ultimately need it to be in json form (which is why I’m currently using a json aggregator).

Re: “ What else am I missing???“ possibly nothing! I’m still newish to make so this may not be a super complex question after all.

Please implement the array aggregator with the reverse function first, then provide the following to show the update to your scenario.

To allow others to assist you with your updated scenario, please provide the following:

1. Relevant Screenshots

Please share screenshots of your scenario, any error messages, relevant module fields, and filters in question? It would really help other community members to see what you’re looking at.

You can upload images here using the Upload icon in the text editor:

2. Scenario Blueprint

Please export the scenario blueprint file to allow others to view the mapped variables in the module fields. At the bottom of the scenario editor, you can click on the three dots to find the Export Blueprint menu item.

3. Output Bundles of Modules

Please provide the output bundles of the modules by running the scenario (or get from the scenario History tab), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.

A. Upload as Text File

Save each bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.

B. Insert as Formatted Code Block

If you are unable to upload files on this forum, alternatively you can paste the formatted bundles.
These are the two ways to format text so that it won’t be modified by the forum:

  • Method 1: Type code block manually

    Add three backticks ``` before and after the content/bundle, like this:

    ```
    content goes here
    ```

  • Method 2. Highlight and click the format button in the editor

Providing the input/output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

Following these steps will allow others to assist you here. Thanks!

Without more detail, I’m afraid this isn’t helpful :confused:

There’s no array aggregator. It’s not actually an array. As per the pictures I shared, it’s a data store search results > Aggregate to Json.

Attaching a micro version of the scenario:
blueprint-5.json (15.5 KB)

Managed to get something working, but it’s 4 steps long which doesn’t feel efficient. I’m putting the search bundle results into an aggregated array, then iterating that array reversed, then forming json from it.


blueprint-7.json (17.3 KB)

Is this really the best way to do it?

Good job on implementing what I have suggested.

This is exactly how I would have done it.

There is a possible way to reduce an operation further, by replacing the Iterator/Aggregate to JSON, by using JavaScript with the CustomJS module. However this is advanced stuff and is for a different thread.

Hope this helps! Let me know if there are any further questions or issues.

@samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!