I have a lookup table that contains a list of equipment along with an Elexon Charge code. I have already used the cascading select to sort equipment manufacturers and models, as these are presented as a drop down list, I am stuck with the next bit which is to automatically to fill in the Elexon charge code that is directly related to the model from the lookup table and make it un-editable, I have tried using the expression filter but that just gives a dropdown with all the codes in the table. I'm sure it is a simple fix but I just can't see the issue.
-
Hi Stuart Venn, welcome to GIS SE. Such a question as yours would heavily profit from a screenshot or - even better -sample data for testing. It's much harder just from text to try to understand what you have and what you want to achieve.– BabelNov 20 at 22:05
-
I assume you saying that Equipment Model should automatically determine Elexon Charge code? Is using a Virtual Field an option - that will just look up directly from the lookup table, and will auto populate based on the lookup value.– Tom BrennanNov 21 at 3:33
-
Yes that is exactly what I am trying to achieve. Is there a way to use a expression filter instead of a virtual field.?– Stuart VennNov 21 at 19:28
2 Answers
One way is to use a Virtual Field.
With lookup data table as follows (layer named "data", lookup field is 'model', result field is 'code'):
I created a virtual field ('code') in my data entry layer with the following expression:
attribute(get_feature('data', 'model', "model"),'code')
So now as I enter the model for a new feature in the data entry layer, the code populates automatically:
The advantage of this method is that if the lookup table changes, the codes in the dependent table will also change.
-
Thanks Tom, problem solved, the virtual field is the way to go. Tha.nk you very much Nov 22 at 21:17
-
Thanks - remember to accept the answer that most helped. See: stackoverflow.com/help/someone-answers Nov 22 at 21:40
One option is to use a Value Relation and Filter expression.
With lookup data table as follows (layer named "data", lookup field is 'model', result field is 'code'):
Then in the data entry layer, go to the Attributes Form and select the field to be looked up ('code' in my case).
- Disable the Editable checkbox.
- Select Value Relation
- Select the same Key Column and Value Column - using the lookup result column from the lookup table/layer ('code' in my case)
- Set the Filter expression using the lookup key column from the lookup table/layer ('model' in my case) to
current_value('model')=attribute($currentfeature,'model')
- note that the first field name relates to the data entry layer, and the second to the lookup layer.
Same result as for Virtual Field, but if for some reason you change the code mappings in the lookup table, it will stuff up your data in the data entry table. If you were going to change any mappings, you'd probably want to disable the Value Relation, and fix up any data before re-enabling it.
General approach taken from: Show ValueMap values based on value of another field in QGIS widgets, but yours is a simpler example.