I'm trying to get the maximum value from an array, using the max() function. The syntax for the max() function is this:


I have an array: [ 1, 2, 3, 4, 5, 6, 7, 8 ] which I got by using this expression:

array_remove_all( array_agg( "fieldname"), maximum("fieldname"))

I need to obtain the maximum value from the array, but the max() function doesn't work. When I calculate the max() of the array, it gives this error:

Eval Error: Cannot convert '' to double

I can convert the array to a string using the array_to_string() function. When I calculate the max of the string version of the array, it gives this error:

Eval Error: Cannot convert '1,2,3,4,5,6,7,8' to double

So it seems the max() function can't accept an array or a list in string format as input. I also tried the maximum() function, with similar lack of results. I don't see any other function in the Field Calculator for finding the maximum value

How to extract the maximum value from an array in the Field Calculator?

1 Answer 1


To get the largest value from an array you could sort it and take the last value:

array_last(array_sort( an_array ))

If you want the second highest value in a field called 'fieldname' (I assume this because you remove the maximum in your array construction) you would need remove duplicates and slice:

  ), -2, -2

An alternative would be to use the function editor and write it in python.

Note: array_sort is a new function introduced in QGIS version 3.6:

enter image description here

  • It turns out array_sort is a new function, only available from QGIS 3.6 on. I'm still using the current LTR, 3.4. In fact I did look for a sort function, and I was surprised it didn't seem to exist. I guess I'll have to update my QGIS version.
    – csk
    Commented Mar 29, 2019 at 18:48
  • Aha! I was looking for that sort function and couldn't find it.
    – Gabriel
    Commented Mar 29, 2019 at 18:49

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.