I have a grid-layer (polygon) and a point-layer. The points that lay within a grid-cell have the same unique id like the grid cell. they also have an attribute "year". What I want is to create a column in the table of the grid layer that is populated with the number of unique values from the attribute "year" from the point-layer (only of points that lie within the respective cell). So in the screenshot-example, for the upper cell that would be "6" (2014,2015,2016,2017,2018,2019), for the lower it would be just "1" (2 x 2017, so only one unique value), and so on.

In the field calculator I can only find within_geom_count and within_geom_sum, but nothing like within_geom_unique or similar.

I also have found a workaround using the "group stats" add on, where I can count the unique values, and then join the produced csv with the grid layer, but I'm pretty sure that there is some easy way to do it directly in the field calculator without these extra steps.

enter image description here enter image description here

  • 3
    Try to use Aggregate function with count_distinct based on Year field. – ahmadhanb Oct 24 '19 at 7:01
  • thanks for the reply - I just can't get it to work. I end up with the total number of unique values ("6" in this case) for the attribute "year" in each field, even if there are no points inside the grid cell. I tried the following formula: aggregate( 'punkt','count_distinct',"jahr"["grid"]) thought it should be ordered b the "grid" field by that way? ("punkt" is the point-layer, "Jahr" the attribute, "grid" is the unique ID of the grid-cells, which are also present in the point layer... – Bjoern Oct 24 '19 at 10:51
  • 1
    Don't include the square brackets in your expression. In the expression builder help section, brackets indicate that the bracketed parameter is optional. So your expression, with corrected syntax, would be: aggregate('punkt','count_distinct',"jahr","grid"). But that last parameter should be a filter expression, and it's not. Try replacing "grid" with "grid" = attribute(@parent, 'grid'). – csk Oct 24 '19 at 14:59
  • Thanks again, my fault :-) But it still won't work, I only get "0" as values... – Bjoern Nov 7 '19 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.