I am working with QGIS 2:14, for now, I have a qgs project with a lot of layers and I want to create a label in the print-composer with the result of a calculated expression from one of this layers.

In one of my layers (recycling_points.shp ) I have 150 records with the recycling points in the city. Then, for every record I have a field (num_containers) with the number of containers of every recycling point, and I want to create a text label in the print-composer, based on a calculated expression, with the number of containers in my city, but I don't know how can do it correctly.

I've tried with the next expression:

layer_property('recycling_points', count"num_containers") 

but is invalid.

Any idea?

3 Answers 3


I think you can't sum the value of all feature in Qgis 2.14 with the field calculator. The formula 'layer_property' can only count the number of features within a layer, if you enter within a text label ('insert an expression'):

  • layer_property( 'recycling_points','feature_count')

If you have to use Qgis 2.14 you could create a single feature for the number of 'num_containers', e.g. if this number is 4 there should be 4 features at that recycling_point

If you can update to (at least) Qgis 2.18 it is much easier, you can use the new 'aggregate' function and simply enter:

  • aggregate( LAYERNAME, 'sum', FIELDNAME)
    • aggregate( 'recycling_points', 'sum', "num_containers")

  • PERFECT! aggregate is my function. I've been updated my QGIS version to the 2.18 and I have achieved my purpose. Thanks!
    – Litus78
    Commented May 11, 2017 at 8:56
  • Glad I could help. If you want to help others you can accept the answer, so others will see more easily how you solved the problem ;-).
    – swad
    Commented May 11, 2017 at 10:34

The number of containers for every recycling point is already stored so, if I understand your question, you only want to sum them.

You may use this expression:


and it will return the sum of the values stored in the "num_containers" field.

Furthermore, if you want to group the sums by another field (e.g. a field that separates different neighborhoods), you may add the "group_by" parameter:

sum("num_containers", group_by:="neighborhood_field")


If you are working with many layers, you may use something like this:

layer_property(@layer_name, 'name') = 'recycling_points'

for running the sum only when the layer's name is 'recycling_points'.

  • Thanks for the answer, but I need that the expression select previosly from what layer select the sum("num_containers") because I want to insert this expression (in a text label) in the map composer and my qgs project has a lot of layers.
    – Litus78
    Commented May 10, 2017 at 9:33
  • @Litus78 I can't find the part of your question where you specified that you are working with a lot of layers. Please, see the edit on my answer: if it still doesn't fit your needs, consider to edit your original question by adding more information on which is the situation you are starting from and to where you are pointing to.
    – mgri
    Commented May 10, 2017 at 10:12

Just a note on one potential risk with using the aggregate function as outlined in the accepted answer...

With aggregate, if the layer name is used to specify the layer, then if that layer is renamed later, the aggregate function will break. Of course, you can manually update the layer name in every aggregate call that uses that layer, but it makes renaming a layer a more manually intensive process that now has easy-to-forget dependencies.

One alternative is to pass aggregate the layer id instead of the layer name; aggregate accepts either, but using the layer id will continue to work even if the layer name changes. One drawback here is that the layer id is usually less human-readable than the layer name.

A second alternative is to use an attribute table item instead of a label item. In the attribute table item, set its layer to the one for which the calculation is needed. Then clear all the attributes and add a single attribute that is the calculation needed, such as sum("num_containers"). After turning off the headers and grid and setting the maximum rows to 1, you should be left with something that looks just like a label item. Like the first alternative above, this approach is also resilient to layer renaming, and it has the advantage that you are still working with the layer name in the GUI instead of having to hard-code the layer id.

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.