QGIS Attribute Selection – How to Select an Attribute Value with the Expression String Builder in QGIS

composer-managerqgisqgsexpression

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.

Best Answer

You can use the aggregate function for that:

array_get( aggregate(layer:='my_layer',
aggregate:='array_agg',
expression:="my_fieldname"),0)

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