I'm using QGIS 3.4 attribute form to edit the widget type. I'm trying to retrieve "cab_no." field values (only unique values) from 'U_Cabinet' layer to use it in "fdt#" field in the 'Distribution2' layer. as shown in the image below

enter image description here

I've been trying to retrieve unique values by using the expression builder.

I tried the expression array_distinct(array("cab_no."))

where "cab_no." is the field who has the values.

but there is no result. as shown in the image below.

enter image description here

Am I missing something here??

2 Answers 2


You must use array_agg:

 array_distinct(array_agg( "cab_no" ))

This will return an array with distinct values found in your layer with the field cab_no.

  • Thank you very much for your answer. I tried it but still no result. the output preview shows this [3, 2] as a result preview and it looks like it should work but when I apply it still no result.
    – Ahmed Alaa
    Aug 11, 2020 at 5:57
  • The expression itself works, as you are saying with the output preview. But then it depends where you use it. There are plenty of area in QGIS where expressions are used. Can add in your question where you use it.
    – etrimaille
    Aug 11, 2020 at 6:13
  • Ok. I will update my question with the place that I want to use it.
    – Ahmed Alaa
    Aug 11, 2020 at 6:23

The filter expression requests a true/false output. The function array_distinct() gives you an array with distinct values as an output, plus you need to use array_agg first, as @etrimaille pointed out. To get a true/false you can try something like:

"id" = array_first( array_agg( "id", group_by:= "cab_no."))

For id you have to insert your unique id column of the U_Cabinet layer. If your u_cabinet layer is big, this can get quite slow.

I think it helps to look at your problem from a different angle. For your purpose I would recommend to use the Widget Type "Value Map" -> Load Data from Layer -> Value and Description are both cab_no.

  • The Widget Type "Value Map" is a good option but the values that I wanted to be returned is varying so I wanted to use "Value relation" to be always up to date.
    – Ahmed Alaa
    Aug 11, 2020 at 14:07
  • I've also used array_agg first, as @etrimaille said but still got no result
    – Ahmed Alaa
    Aug 11, 2020 at 14:08
  • I see, I added a possibility to my comment how you can do it with a filter expression.
    – nepluisse
    Aug 11, 2020 at 14:20
  • This code "id" = array_first( array_agg( "id", group_by:= "cab_no.")) works. Thank you very much.
    – Ahmed Alaa
    Aug 12, 2020 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.