You could do something like this. Admittedly, the expression can probably be cleaned up.
with_variable('existing_layer_name',
with_variable('options', array('buildings', 'facilities', 'real estate'), -- pre-determined list of layer names
with_variable('layer_names', array_foreach(@layers, layer_property(@element, 'name')),
array_get(@options,
array_filter(
array_foreach(
@layer_names,
array_find(@options, @element)
),
@element > 0
)[0]
)
)
),
-- the original expression
aggregate(
layer:=@existing_layer_name, -- the result of the matching procedure above
aggregate:='sum',
expression:="value"
)
)
It basically matches the layer names in the project against a pre-determined list. If more than one match is found then it uses the first one.
array_find
returns the index of the found element, and array_get
uses that index to pull the layer name from the array of 'options'. array_filter
is used to remove any -1
returned by array_find
(non-matching).
The following two screenshots omit the original expression to demonstrate what is returned by the match.

