I was struggling to filter the legend items ( feature count ) , based on the current feature layer. My atlas layer is a world map layer and the points layer ( that is in being counted in the legend ). The below expression on the QGIS legend expressions is working but super slow . Any better way to do this ?

This is is using the Legend expression button. Works only with the Sum / total button disabled.

if (@symbol_label  = 'Remaining', @symbol_label 
 || ' ' || aggregate('Points__ebc6c554_d138_46f5_9280_9d5c1b2dfdbe','count',"ID","Status" = 'Remaining' AND within($geometry,@atlas_geometry))
, @symbol_label 
 || ' ' || aggregate('Points__ebc6c554_d138_46f5_9280_9d5c1b2dfdbe','count',"ID","Status" = 'Planned' AND within($geometry,@atlas_geometry))

enter image description here

  • Sure, Project and layers are here github.com/datavantage/gissample/blob/main/Legend3.gpkg @Babel Nov 7, 2023 at 8:23
  • Correct , this is for each atlas feature . As I iterate through the atlas pages, I want the legend to correctly filter the Feature counts. The expression is under the expression button in this image. It will only work if the Sum button is switched off. Nov 7, 2023 at 9:02
  • I just added an image , the expression is in the legend settings Nov 7, 2023 at 9:06
  • This is great, thank. you @Babel Nov 7, 2023 at 10:59

1 Answer 1


Use the following expression. Delete your expression from the header, but insert it directly to the legend of the categorised entries if you want to create the number of feautures dynamically in the legend.

By the way: I changed the point layer name to Points: your name contains spaces what can be a bit tricky. So in the following expression, adapt the layer's name to the one you use.

But even better would be to create new attributes Planned and Remaining on your World layer with the same expression and then use this attribute in the legend. Like this, you calculate it once and won't get any slowing down when creating the atlas.

    overlay_contains (
        'Pointlayer',  -- change layer name
        filter:=Status='Planned' -- change to Remaining for the other symbol

enter image description here

Your Answer

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

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