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:


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.

  • Do you want to permanently calculate that value and store it in the attribute table, or do you only want to calculate it for displaying in a form? – til_b Dec 12 '19 at 9:48
  • Yes i want to permanently calculate it and store it in the attribute table and in my postgresql database. (I am aware that i could calculate it with psql12 in the database, but i would prefer to get it already in the qgis formula) – Nunkuat Dec 12 '19 at 10:03
  • Are the values you want to use for your calculation numeric values or strings? – C.-F. Vintar Dec 12 '19 at 10:53
  • They are of Type int. – Nunkuat Dec 12 '19 at 10:58
  • 1
    Yes, i can insert, update and delete on my view. – Nunkuat Dec 17 '19 at 8:51

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

  • That actually works, but dont really helps with my problem, since now one has to fill out the formula and afterwords go into the attribute table and alter the column "RMR-Wert". My problem exactly is, that i cant use "Felder und Werte" in the default value expression builder. – Nunkuat Dec 12 '19 at 10:38
  • You should be able to overwrite the field "RMR-Wert" with "Vorhandenes Feld erneuern", which would save you one step. But I gather you want to calculate the RMR-Value directly without filling in all the other stuff first and then go to the Field calculator? – C.-F. Vintar Dec 12 '19 at 11:19
  • 1
    Yes thats right, since im not building the formula for myself, it should work without having to make changes in the attribute table or use expressions in the Field calculator. – Nunkuat Dec 12 '19 at 11:27
  • Unfortunately that doesnt work for me, also if I would agree, that it should solve the problem (at least how I imagine the functionality...). I guess its because of my postgresql view (see my edit). – Nunkuat Dec 17 '19 at 8:26
  • Try making a minimal working example, by creating a layer (maybe temporary, maybe a subset of your real columns) and then build up from there. I really had to play around a bit with element types and default values. Also, are you running a recent QGIS (I use 3.10.1)? – til_b Dec 17 '19 at 8:44

The "default value" setting ("Vorgabewert" in your QGIS language setting) cannot use current field values in its calculation. That would be a great feature* if it did, but it doesn't. It can only use values like $area or $length.

So instead you have to use a workaround like:

  • As the other answers suggested, re-calculate the field with the field calculator after creating the feature.
  • Use a virtual field. Note that a virtual field is only stored in the project, it's not stored in the source data.

*Update: it sounds like this feature was introduced in QGIS 3.10 (see the changelog for details). So this answer applies to QGIS versions prior to 3.10. Update to the latest version if you want default field values that depend on other field values.

  • This should work. If you have to do it with a great number of layers, you could e.g. use a script to spare you the repetitive typing. Some solutions on this, see here: gis.stackexchange.com/questions/344779/… – C.-F. Vintar Dec 16 '19 at 8:43
  • I think it can use it, it doesn't show them in the dropdown, but manually keying in a formula works in my experiments. See my answer. Could you please also try that? If this is really behaving the way we are observing this is a bug and should then be reported to the qgis bug tracker. – til_b Dec 16 '19 at 15:42
  • @til_b I tested your method, but the default field value is not applied when I create a feature. It only gets auto-filled if I edit the geometry of the feature or one of its other attributes. Otherwise the field with a default value stays null. What version are you using? I'm using 3.4.10. – csk Dec 16 '19 at 17:17
  • @csk I run 3.10.1, specifically commit ef24c526da from the OSGeo4W installer. Have you checked "changeable" at the top of the form? If I uncheck changeable the field is displayed correctly but not written to the attribute table. If "changeable" is checked, even if i never click into the field the correct value gets written. – til_b Dec 17 '19 at 8:25
  • 1
    @til_b Yes, I had "changeable" checked. It looks like this is a new feature introduced in version 3.10. Prior to 3.10, the default value couldn't depend on other field values. I updated my answer accordingly. – csk Dec 17 '19 at 16:44

You should be able to set the field value itself to a function of other values, just like in Excel etc. You can do this in the layer properties in section Attribute Form. Just select the field, you want to change, click on the little Sigma-Icon under Expression and type in your expression (see below). It should actually be possible with your values.

enter image description here

  • Thanks, that kind of works... but instead of adding the values up, it links them to each other... so instead of 3 + 4 +7 = 14 i get 347, what is not my desired outcome... So it feels like the values are treated as strings, what they arent... – Nunkuat Dec 12 '19 at 11:58
  • 2
    They do rather look like strings in your example above, thats why I asked. However, you can use the function to_int( ) in the expression builder as well as in the field calculator to convert them into int. I would make a backup of the file first and then use the field calculator to convert the values to int in new fields. Afterwards you can delete the string fields and use the values as described above. – C.-F. Vintar Dec 12 '19 at 12:08
  • I have added some Screenshots from my project and formular, maybe it makes the whole issue more plain. The 'to_int()' didnt't work unfortunately. – Nunkuat Dec 12 '19 at 13:16
  • What you describe should be achieved by concat() and not +. Could you add a screenshot of your attribute table? If you have stored your data in a Shp-File you can also look into the .dbf using Excel or similar software. But be sure to make a backup before that, because you can break your data very easily this way. – C.-F. Vintar Dec 12 '19 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.