QGIS – Using Value Relation Widget with Generic Relation for Identical Dropdown Filter

attribute-formqgisqgis-expressionqgis-widget

Set up

I have a point layer ('Baum') to collect data with 3 attributes ('mass_typ', 'mass_grund', 'veg_typ'). I want the attribute 'veg_typ' to have a dropdown filtered according to the two other attributes. Therefore, I also have a table ('value_relation'), on which my value relation widget for 'veg_typ' is based.

enter image description here

Since many combinations of 'mass_typ' and 'mass_grund' result in the same filtered dropdown, I do not want to fill the 'value-relation' table with all possible combinations (with my real data there would be plenty and it would be hard to keep track). Instead I added only the combinations for which a filter is necessary and a combination called 'no_filter' for all the rest.
enter image description here

Problem

Now, I am struggling to write the according filter expression for the value relation widget.
I want to check, if the combination of the current values for 'mass_typ' and 'mass_grund' is in the 'value_relation'-table. If so, filter accordingly and if not, filter for 'no_filter'.

I tried the following:

IF (
    "mass_typ" = current_value('mass_typ') AND "mass_grund" = current_value('mass_grund'), 
    "mass_typ" = current_value('mass_typ') AND "mass_grund" = current_value('mass_grund'),
    "mass_typ" = 'no_filter' AND "mass_grund" = 'no_filter'
)

and also a simpler example with only one attribute:

IF (
    current_value('mass_grund') IN ("mass_grund") , 
    "mass_grund" = current_value('mass_grund'),
    "mass_grund" = 'no_filter'
)

The result is the same for both expressions:

  1. When I add an object to the layer 'Baum' and enter a combination that does not exist, it seems to work and the dropdown is filtered to values with 'no_filter' (although the background color of the dropdown looks strange) .

enter image description here

  1. When I enter a combination that exists in the table, the dropdown is filtered to both the values for the combination as well as the values for 'no_filter'.

enter image description here

How can the if-expression evaluate in a way, that it returns the true- and false-result at the same time?

How can I fix my if-condition?

I'm using QGIS 3.22.14-Białowieża but I also tried it unsuccessfully with 3.30.2-'s-Hertogenbosch.
The sample project can be downloaded here.

Best Answer

I checked your code and found a solution:

    CASE 
        WHEN current_value('mass_typ') IS NULL OR current_value('mass_grund') IS NULL
            THEN "mass_grund" = ''
        else
            if (
                array_contains( array_agg("mass_grund" + "mass_typ"), current_value('mass_grund') + current_value('mass_typ')),
                "mass_grund" = current_value('mass_grund') AND "mass_typ" = current_value('mass_typ'),
                "mass_grund" = 'no_filter'
            )
END

The code checks first whether both fields mass_grund and mass_typ are set. If this is the case, both columns mass_grund and mass_typ were aggregated to one array:

[abgestorbenAEG, abgestorbenAEG, Geringe RestwandstärkeAEG, ...]

Inside this array the function array_contains is looking for the concatenated user input:

current_value('mass_grund') + current_value('mass_typ')

If this combination is found the normal filter expression takes place. If not, no_filter is used and every veg_typ could be selected.

Related Question