QGIS 3 – Calculating Attribute with Formula in QGIS 3

attribute-tableformulaqgis-3

I want to build a QGIS formula where I calculate one attribute from 10 others, and I only need to sum them up.

My Problem is, that I don't get the actual values of the attributes. I thought about using the function: current_value() which works fine if I build a value relation but is not present in the default value expression builder.

Here my example:

example

The "RMR-Wert" should be calculated of the upper values (the values in the parentheses are saved to the attribute table and database).

Is there another option than depending on the function: current_value()?


The Entrys only look like strings in the formula, in the attribute Table they appear as int. This is because I use "Wertabbildung" (Value references (?)):

enter image description here

So the Columns I want to calculate with are all int:

enter image description here

For the Formula I use the Drag and Drop option and it looks like this:

enter image description here


Until now none of the proposed approaches worked for me.

It could be, because the layer i want to alter is a view from a postgres database. Also in the QGIS attribute table the values are displayed as strings:

enter image description here

while in the database they are stored as integers:

enter image description here

Also i can't use a virtual column, because then my QGIS crashes, i guess it's also because its a database view, but not sure about this.

If i want to change my values to int values i get an error message:

evaluation error: not possible to convert "..." to integer (hope thats a propper translation...)

evaluation error: not possible to convert "…" to integer (hope thats a propper translation…)

That could be, because in the attribute table the values appear as strings…

My plan now is to move on from QGIS to Postgresql and try to tackle the problem there, using Generated Columns.

Best Answer

see update below

Use the field calculator, create the expression by adding all the fields together. Field names can be double clicked from the middle widget in the calculator window, under the heading "Felder und Werte" (don't know how its called in english, probably "Attributes and Values"). They will then be added to the expression, and you only have to manually key in the "+" for addition.

Field calculator

Method for form editor, not using the attribute table calculation:

this works only for QGIS 3.10 and later

The trick is to just input the formula into the expression box. It doesn't show the fields when clicking on "Felder und Werte", but the expression works nevertheless! Then it was just a matter of randomly clicking and trying out different options to make the field update and retain the default value when clicking OK and not having made an edit to the calculated value.

A catch is to check "Änderbar" at the top of the form, under the field name. If the field is not Änderbar (changeable), then the calculated value will be displayed but not written to the attribute table!

Form editor

Related Question