8

I create new field based on other fields. For example; if "Field_A" is smaller than "Field_B", the value of the new field is 'A', otherwise it is 'B'. So I want to create a new variable for this case within Expression area.

It's possible in QGIS 3 by means of with_variable function. But QGIS 2 doesn't have that function.

In QGIS 3:

# mn -> new variable
with_variable( 'mn', min( "Field_A", "Field_B" ),
 if( @mn = "Field_A", 'A', 'B' ) )

or

with_variable( 'mn', min( "Field_A", "Field_B" ),
 case when @mn = "Field_A" then 'A' else 'B' end )

In QGIS 2, I want to write something like that:

mn = min("Field_A", "Field_B")
CASE
WHEN mn = "Field_A" THEN 'A'
WHEN mn = "Field_B" THEN 'B'
END

I don't mean global or project variable, I'm asking to create a new variable to be used in Expression area scope.

How can I do that within Expression area in QGIS 2?

Val P
3,9501 gold badge10 silver badges34 bronze badges
asked May 15, 2018 at 13:19
1
  • 9
    The with_variable() function is a new feature in QGIS 3. As far as I can tell there's no equivalent function in QGIS 2. Commented May 15, 2018 at 18:15

1 Answer 1

1

The with_variable() function is a new feature in QGIS 3 (see changelog). There's no equivalent function in QGIS 2.

with_variable(name, value, node): This function sets a variable for any expression code that will be provided as 3rd argument. This is only useful for complicated expressions, where the same calculated value needs to be used in different places.

1
  • This community wiki answer is given in order for this question to be marked as "answered". Commented Jan 15 at 12:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.