3

I am trying to display attribute values from various layers in a QGIS layout using a dynamic text expression. I've been able to calculate statistics for such layers (by following this tutorial: https://www.youtube.com/watch?v=NmdMnfwP3EQ); however, I want to display string attributes and not just statistics.

Is there an expression that allows me to display an attribute of choice from a layer of choice as dynamic text in a QGIS layout? The aim is to present snippets of information from various layers as text labels on the same sheet.

1 Answer 1

4

You can use a combination of the attribute and the get_feature (or get_feature_by_id) functions.

attribute takes 2 arguments: a feature and the field from which you want the value. It returns the attribute value of a feature.
get_feature takes 3 arguments: the layer name, the field name, and the field value you wish to match. It returns a feature.


get_feature_by_id takes 2 arguments: the layer name and the feature id number


attribute(
    get_feature(
         'mylayer', 
         'myfieldname', 
         'myfieldvalue'),     -- match a feature using the field name and a specific value
    'mytargetfield'           -- get a specific attribute of the matched feature
)

For example, here I find the feature in mylayer that has a value of 1703 in the field myfieldname. The attribute of interest (mytargetfield) has the value B.

enter image description here

enter image description here

1
  • Hi, apologies for the delay, but this is great! Thanks so much for your response.
    – Reggie87
    Commented Nov 9, 2022 at 17:02

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.