I am building a model in which I join attributes from a .csv file (let's name it "upload" here) to a layer. I want to work with some of the new attributes in the next step of the model and I am having the problem that the field names in "upload" are always different except for the first part of the name. So what I am looking for is a way of doing the following, e.g. in the field calculator: Take the field of a certain layer that has a name beginning with "substring" and multiply its values by x. Alternatively, I could imagine a solution where I refer to a field at a certain position (like the last field in a layer). So far, I only managed to refer to fields of a completely known name.

1 Answer 1


What you can do is using an expression to get the names of your attributes. The core of this solution is that you can use the expression attributes() in combinaiton with map_akeys. When you have the name of the field, you can get it's value for each feature with attribute(attribute_name). Thus you can use this to multiply it, let's say by 4, like this: attribute(attribute_name)*4

This here is the exact expression to use to get the attribute_name and a screenshot that shows the result (see below for explanation):

array_get ( 
    map_akeys ( attributes( ) ) , 
    array_first (
        array_remove_all ( 
            array_foreach ( 
                generate_series ( 
                    array_length ( map_akeys ( attributes( ) ))
                ) ,
            if (
                left (
                        map_akeys ( attributes( ) ) , 
                    ) , 9) = 'substring', 
        'false' )

See the screenshot for the output this expression generates at the bottom: it corresponds to the field name we were looking for. Now just enclose this whole expression inside attribute( ) to get the value of this field for each feature.

QGIS expressions getting attribute field name

The above expression was designed for an attribute table like this one here where you want to get the field that starts with 'substring':

enter image description here

To start, the most important part is getting the attribute name (field name). Let's demonstrate it using the above example of an attribute table with several fields and random values:

attributes() creates a map in the key:value form:

{ 'date': <Datum: 2020-12-20>, 'fid': 1, 'hight': 375.000… [and so on]}

Add map_akeys in the form map_akeys( attributes()) to get an array of fieldnames - as you see in alphabetic order, so you can't use the position of the field in the attribute table:

[ 'date', 'fid', 'hight', 'length', 'masurement_3', 'measu… ]

Now with this array of fieldnames, you can creat a for-loop using array_foreach to get through each element and test of the first 9 letters (use: left(string,length) with length=9) are equal to 'substring'. If you get to an element where this is true, you found your fieldname and can use it as an output.

The expression from above does exactly this, it needs some additional steps that are not so difficult to understand as it might seem at the first glance. If you still need this and anything is unclear, ask so that I can elaborate on this.

  • Very nice answer ! Thanks for sharing !
    – lejedi76
    Commented Jan 28, 2021 at 22: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.