I'm working with QGIS 3.16 Hannover and this should be very straightforward but I'm unable to solve it.

I'm producing different maps, filtering the features of a layer. In the composer manager, I'm trying to create an automated legend title using the Expression String Builder that includes the value of an attribute of the first feature of that filtered layer. All my attempts with the commands attribute() and get_feature() or get_feature_by_id() have failed.

1 Answer 1


You can use the aggregate function for that:

array_get( aggregate(layer:='my_layer',

Aggregates all values form field my_fieldname of the layer my_layer into an array and with array_get you can get the value at position 0

  • 1
    Excellent solution, thank you. After your answer, another valid option would be array_first(aggregate(layer:='my_layer', aggregate:='array_agg', expression:="my_fieldname"))
    – jpinilla
    Oct 7, 2021 at 9:53
  • 1
    aggregate(layer:='my_layer', aggregate:='array_agg', expression:="my_fieldname")[0]
    – pigreco
    Oct 7, 2021 at 12:10

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.