4

I have a three point across x with five repeating rows with this attributes:

[ 2, 1, 1 ]
[ 1, 2, 2 ]
[ 1, 1, 1 ]
[ 2, 2, 2 ]
[ 4, 4, 4 ]

I want to calculate a new field which aggregates the rows, currently I have: array_min(array_agg( "collumn", group_by:="row" ))

This will compute this result:

[ 2, 1, 1 ] -> 1
[ 2, 2, 2 ] -> 2
[ 1, 1, 1 ] -> 1
[ 2, 2, 2 ] -> 2
[ 4, 4, 4 ] -> 4

What I'm aiming to do next is to do a conditional where if the row aggregates is lower than the previous row aggregates, use that lower value instead of the computed value.

[ 2, 1, 1 ] -> 1: OK
[ 2, 2, 2 ] -> 2: OK
[ 1, 1, 1 ] -> 1: Lower than previous, use previous
[ 2, 2, 2 ] -> 2: OK
[ 4, 4, 4 ] -> 4: OK

I'm constrained on only using QGIS field calculator. Temporary field is allowed. What are my options?

2
  • Do you have a row number to sort data or do you rely on the data creation order ?
    – J.R
    Commented Dec 15, 2023 at 17:29
  • @J.R I have row number inherited from creation order, it should be sequential Commented Dec 15, 2023 at 17:49

1 Answer 1

5

Something like this:

if(
    array_min(array("a","b","c"))
    <
    array_min(
        array(
            attribute(get_feature_by_id(@layer,$id-1), 'a'),
            attribute(get_feature_by_id(@layer,$id-1), 'b'),
            attribute(get_feature_by_id(@layer,$id-1), 'c')
            )
        )
    ,
    array_min(
        array(
            attribute(get_feature_by_id(@layer,$id-1), 'a'),
            attribute(get_feature_by_id(@layer,$id-1), 'b'),
            attribute(get_feature_by_id(@layer,$id-1), 'c')
            )
        )
    ,
    array_min(array("a","b","c"))
    )

Note that depending on your exact requirements you may want to use get_feature() instead of get_feature_by_id(). In my example no gaps in the id's are allowed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.