How to do it in QGIS or QGIS Expression to replace the element of an array(cell value) of a table with a new value taken from another table (which contains the list of the element and its corresponding new value), see image below:
3 Answers 3
As long as the order of Old element
and New Element
are the same, you can use this expression on Table 1
:
array_get(
aggregate(
'Table 2', -- the name of the layer with the field `New element`
'array_agg',
"uuid_trgt" -- the name of the field in `Table 2` containing `New element`
),
array_find(
aggregate(
@layer, -- the current layer (`Table 1`)
'array_agg',
"uuid_src" -- the name of the field in the current layer (`Table 1`) containing `Old element`
),
"uuid_src" -- the name of the field in the current layer (`Table 1`) containing `Old element`
)
)
The expression makes an array of all the values in the Old element
field, then for each row of Table 1, finds the position in the array of the current value of Old element
and retrieves the corresponding item from an array of New element
values.
Use this expression to find the corresponding value, regardless of the attribute's order:
attribute (
get_feature(
'Table 2',
'Old element',
"Existing Data"
),
'New Element'
)
Explanation:
- Use function
get_feature()
to return the feature ofTable 2
where the fieldOld element
matches the attribute value ofExisting Data
inTable 1
- then use
attribute()
to return the value of the attributeNew Element
of this feature.
By the way: try to avoid names with a space in it, as in some contexts, that can cause problems.
This is a classic use case for table joins: Enter layer properties of Table 1
> Tab Joins, create a new join (green + icon) and define the table to join and the attributes from both tables that match.
Options: select which fields to join and if the names of the joined fields should get a prefix:
Explore related questions
See similar questions with these tags.
Table 2
) and field names (Existing Data
) - and any other names like filnames etc.