4

In QGIS in the Attribute Table, I have a field with values.

For this field, I need to calculate the Sum of all its values and divide by the number of values that are not equal to "NULL".

Afterwards, I need to create a new field, where each value of the original field will be divided into the previously calculated (the Sum of all its values and divided by the number of values).

Is it possible to do this with the function editor in the Field Calculator (as this action will be repeated more than once)?

0

1 Answer 1

11

First of all, you need to create one additional field in the Attribute Table:

"Output", where the calculated new values will be stored (probably a real data type).

Then you need to proceed in Expression dialogue using the following formula

CASE
    WHEN "YOUR_FIELD" <> 'NULL' THEN "YOUR_FIELD" / (sum("YOUR_FIELD") / count("YOUR_FIELD"))
    ELSE NULL
END

Let's assume we have a field with values, that vary between 3 and 300, and also include "NULL" values. See the Attribute Table below.

Attribute_Table

In my case, the sum of all values is 544. The number of values that are not equal to "NULL" is 7.

Then I proceed with a formula in the Expression dialogue with activated Editing mode.

CASE
    WHEN "Value" <> 'NULL' THEN "Value" / (sum("Value") / count("Value"))
    ELSE NULL
END

Expression_Dialogue

The output corresponding new values will look as following.

Output


References:

1
  • Many thanks for the answer, but my action is repeated very often and therefore I needed to create a function in the function editor. in Expression dialogue I also introduced the formula "Value" / ( sum( "Value" ) / (count( CASE WHEN "Value" >= 1 THEN +1 END))) Nov 9, 2018 at 3:45

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.