How to count instances of values in an array that fall within specific ranges

Hi all,

Could someone please advise how I could do this?

I have a number of bundles with key/value pairs as an output from an API. I want to extract a count of the number of times one of the values falls within a range and save it as a variable.

For example, of the following output:

I would like to count how many times the value of item 11 exists within these ranges:

  • 0-749
  • 750-999
  • 1000-1249
  • 1250-1499
  • 1500-1749
  • 1750-1999

For the example, there is one count existing in the range 0-749 and one count between the range 1000-1249.

I guess first I would need to convert the value from a string to a number (removing the $ and the trailing decimals) and then use the Numeric Aggregator operation to do the count, but I can’t understand how to count values that fall within a specific range.

Any help would be much appreciated!

Welcome to the Make community!

Looks like you need to use a “Set Variable” with nested if-functions to test and set the range of the current item, then you can aggregate (group by) this range.

If you need further assistance, please provide the following:

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”.
Screenshot_2023-10-06_141025

A.

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

Uploading them here will look like this:

module-1-output-bundle.txt (12.3 KB)

B.

If you are unable to upload files on this forum, alternatively you can paste the formatted bundles in this manner:

  • Either add three backticks ``` before and after the code, like this:

    ```
    input/output bundle content goes here
    ```

  • Or use the format code button in the editor:
    Screenshot_2023-10-02_191027

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.

This will allow others to better assist you. Thanks!

samliewrequest private consultation

Join the Make unofficial Discord server!

1 Like

Hi again samliew! You really helped me with a previous question, so thanks again in advance :slight_smile:

Here is the output from the Iterator module:

Output.txt (786 Bytes)

Welcome to the Make community!

Yes, that is possible. You’ll need a minimum of three modules:

Output:

Screenshot_2024-05-01_150544

Give it a go and let us know if you have any issues!

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

View Module Export Code

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 14,
                    "module": "json:ParseJSON",
                    "version": 1,
                    "parameters": {
                        "type": ""
                    },
                    "mapper": {
                        "json": "[\n    {\n        \"0\": \"4803461122\",\n        \"1\": \"xxxxx\",\n        \"2\": \"xxxxx\",\n        \"3\": \"xxxxx\",\n        \"4\": \"02/28/2024\",\n        \"5\": \"04/11/2024\",\n        \"6\": \"05/09/2024\",\n        \"7\": \"andlt; select andgt;\",\n        \"8\": \"\",\n        \"9\": \"\",\n        \"10\": \"251\",\n        \"11\": \"$1,125.00\",\n        \"12\": \"xxxxx\",\n        \"__IMTINDEX__\": 1,\n        \"__IMTLENGTH__\": 2\n    },\n    {\n        \"0\": \"4034199563\",\n        \"1\": \"xxxxx\",\n        \"2\": \"xxxxx\",\n        \"3\": \"xxxxx\",\n        \"4\": \"\",\n        \"5\": \"\",\n        \"6\": \"02/19/2024\",\n        \"7\": \"xxxxx\",\n        \"8\": \"04/19/2024\",\n        \"9\": \"09/30/2023\",\n        \"10\": \"647\",\n        \"11\": \"$300.00\",\n        \"12\": \"xxxxx\",\n        \"__IMTINDEX__\": 2,\n        \"__IMTLENGTH__\": 2\n    }\n]"
                    },
                    "metadata": {
                        "designer": {
                            "x": 12,
                            "y": -378
                        },
                        "restore": {
                            "parameters": {
                                "type": {
                                    "label": "Choose a data structure"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "type",
                                "type": "udt",
                                "label": "Data structure"
                            }
                        ],
                        "expect": [
                            {
                                "name": "json",
                                "type": "text",
                                "label": "JSON string",
                                "required": true
                            }
                        ]
                    }
                },
                {
                    "id": 15,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "range",
                        "scope": "roundtrip",
                        "value": "{{if(parseNumber(substring(14.`11`; 1); \".\") >= 1750; \"1750-1999\"; if(parseNumber(substring(14.`11`; 1); \".\") >= 1500; \"1500-1749\"; if(parseNumber(substring(14.`11`; 1); \".\") >= 1250; \"1250-1499\"; if(parseNumber(substring(14.`11`; 1); \".\") >= 1000; \"1000-1249\"; if(parseNumber(substring(14.`11`; 1); \".\") >= 750; \"750-999\"; \"0-749\")))))}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 253,
                            "y": -376
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "range",
                                "label": "range",
                                "type": "any"
                            }
                        ]
                    }
                },
                {
                    "id": 17,
                    "module": "builtin:BasicAggregator",
                    "version": 1,
                    "parameters": {
                        "feeder": 14
                    },
                    "mapper": {
                        "range": "{{15.range}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 496,
                            "y": -378
                        },
                        "restore": {
                            "extra": {
                                "feeder": {
                                    "label": "JSON - Parse JSON [14]"
                                },
                                "target": {
                                    "label": "Custom"
                                }
                            }
                        },
                        "advanced": true
                    },
                    "flags": {
                        "groupBy": "{{15.range}}"
                    }
                },
                {
                    "id": 20,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "count",
                        "scope": "roundtrip",
                        "value": "Range: {{17.`__IMTKEY__`}}; Count: {{length(17.array)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 738,
                            "y": -376
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "count",
                                "label": "count",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

samliewrequest private consultation

Join the Make unofficial Discord server!

2 Likes

That worked great - thank you!

1 Like