How is it possible to create several new fields at once in the same layer, using QGIS field calculator in batch mode? There is a similar question, but using Python - I'm interested in a solution without Python.
In QGIS, using the field calculator in batch mode (from processing), it is possible to calculate several fields at once. Let's suppose a very simple case just for demonstration purpose here: an attribute table with a field named value
. I now want to create 5 new fields (red box in the screenshot):
- output_1:
"value"+1
- output_2:
"value"+2
- etc.
This works easy in batch mode of field calculator, using the increasing row-number: each row added will create a new field. And all input parameters can be set with Autofill... / Calculate by Expression…
accordingly, writing expressions that generate the necessary values needed for each column (field calculator input parameters) in the batch dialog.
The problem now is: used in this way, QGIS will create a separate file for each row / each new field calculated: output_1, output_2 etc. If I define the same output path for each row (last column in the batch dialog window, see screenshot), than QGIS will iterate over all rows and each time overwrite the output of the previous row. Thus the result will contain only the last output, in this example with 5 rows/5 fields to create: output_5: "value"+5
. When definining separate output filenames (like using increasing numbers, generated by the variable @row_number
: output_1.gpkg, output_2.gpkg etc.), I get the desired result. But I get aseparate file for each field that is created.
The question: How is it possible to batch create new fields in the same layer or the same file - ideally adding the newly created fields to the attribute table of the input layer?
Screenshot: Field calculator in batch mode: field name, expression to calculate and output path are generated by an expression with Autofill... / Calculate by Expression…
:
Refactor Fields
to create the new fields populated by expression? It 'simulated' the batch process you are looking for and it will create only a new temporary layer with all the new fields."value" + @row-number
in the example above. This is especially useful for creating a lot of new fields at once with a regular pattern - like starting from a datetime field, creating 365 new fields for each day of a year by adding +1, +2 +3 days etc.