Count Values Matching a Criteria in an Array

I have an array containing numeric values (steps data for past 30 days). Is there an option to count number of instances in the array exceeding a value. i.e. number of values above 10,000.

Kustom supports loops via fl():

fl(
    [start i],  [end i], [next i value formula string],
    [iteration formula string], 
    [optional separator]
)

The loop starts with i being equal to [start i] and runs until i is equal to [end i]. Every iteration consists of calculating the result of the [iteration formula string], setting i to the result of [next i value formula string] and checking if the end condition is met (either i = [end i] or maximum number of iterations exceeded).

The iteration results are concatenated together, if the [optional separator] is provided, it is inserted between each iteration result.

Formula strings passed to fl() don’t need the $, those are appended automatically under the hood. Any string literal i within the formula string will be replaced with the current value of i (the i in split won’t be replaced).

Let’s say your data has 30 entries. The solution could look like this:

$tc(len, fl(
    0, 29, "i + 1",
    "if(fd(steps, r + i + d) > 10000, a)"
))$

Each iteration will return a if the number of steps for i days ago is greater than 10000, empty string otherwise. By then measuring the length of the result with tc(len), we get how many iterations found a value greater than 10000.

3 Likes

Screenshot_20241026-184732
Thank you. Got it working.

1 Like

This topic was automatically closed 25 days after the last reply. New replies are no longer allowed.