field calculator in qgis

QGIS Field Calculator Helper

Use this quick helper to convert area units, calculate population density, or test a simple reclassification rule before writing your final QGIS expression.

The Field Calculator in QGIS is one of the most useful tools in day-to-day GIS work. Whether you are calculating parcel area, assigning risk categories, standardizing text, or generating IDs, the expression engine helps you update thousands of records in seconds.

What is the Field Calculator in QGIS?

The Field Calculator is available from the attribute table of vector layers. It lets you:

  • Create a new attribute field and fill it using an expression.
  • Update an existing field with transformed values.
  • Use geometry, text, date, numeric, and logical functions in a single expression.
  • Apply conditional logic like CASE WHEN statements.

Think of it as a spreadsheet formula system designed specifically for spatial data.

How to Open the Field Calculator

Step-by-step

  • Load a vector layer (Shapefile, GeoPackage, PostGIS layer, etc.).
  • Open the Attribute Table.
  • Toggle editing mode (pencil icon).
  • Click the Field Calculator button.

You can now either create a new field or update an existing one.

Core Expression Concepts You Should Know

1) Field references

Use double quotes around field names:

"population"
"parcel_area"
"district_name"

2) Strings and numbers

Text values use single quotes; numbers do not:

'Urban'
1250
3.14159

3) Basic operators

  • Math: +, -, *, /
  • Comparison: =, >, <, >=, <=
  • Logic: AND, OR, NOT

Common Field Calculator Examples

Calculate area in hectares

If your layer is in a projected CRS with meter units, this is a common formula:

$area / 10000

Population density per km²

"population" / ($area / 1000000)

Create categories with CASE WHEN

CASE
  WHEN "score" < 50 THEN 'Low'
  WHEN "score" < 80 THEN 'Medium'
  ELSE 'High'
END

Clean and standardize text

upper(trim("street_name"))

Build a unique ID

concat('PAR-', lpad(to_string($id), 6, '0'))

Geometry Tips (Very Important)

Geometry-based outputs such as $area and $length depend on your layer CRS. If your layer is in degrees (geographic CRS), values can be misleading for distance and area calculations. Best practice:

  • Reproject your data to a suitable projected CRS first, or
  • Use geometry transformation functions in expressions when needed.

This single step prevents many analysis errors.

When to Create a New Field vs Update Existing

Create a new field when:

  • You want to preserve the original data.
  • You are testing a formula.
  • You need multiple derived metrics side by side.

Update existing field when:

  • The old values are incorrect and should be replaced.
  • You are running a repeatable cleanup workflow.

Troubleshooting Expression Errors

  • NULL output? Use coalesce("field", 0) to handle missing values.
  • Type mismatch? Convert with to_int(), to_real(), or to_string().
  • Unexpected classes? Verify threshold order in CASE blocks.
  • No changes saved? Make sure editing mode is on and save edits before closing.

Practical Workflow for Reliable Results

  1. Duplicate your layer before major calculations.
  2. Test formula on a small sample first.
  3. Create a temporary preview field.
  4. Check min, max, and null counts in attribute statistics.
  5. Then apply to production data.

Mastering the field calculator in QGIS will dramatically speed up your GIS work. Start with simple formulas, validate each result, and gradually build more advanced expressions with geometry, conditional logic, and string functions.

🔗 Related Calculators