2

I am new to QGIS layout. I would like to display a label showing the proportion of table values meeting set criteria. Please see below:

round((count(score,score<=3)/count(score))*100,2)||'%'

In the above, score is a field in an attribute table. The above expression generates the correct value in the preview, but shows 0% in the text box on the print layout map.

How do I display the preview text on the print map, and why is the above expression unusable?

1
  • Do the labels work on the normal map canvas?
    – nr_aus
    Commented Oct 19, 2021 at 5:50

3 Answers 3

2

Functions with layer defined by context

The function count is an aggregate function that should be used when the layer could be defined by the context where the expression is evaluated (if you use this formula in a labelling rule the layer will be defined).

As you are evaluating this expression in a text box within a layout, there is no layer defined by context. You have to use aggregate function to get the count of feature from a specific layer.

A solution (based on my understanding)

Your expression is round((count(score,score<=3)/count(score))*100,2)||'%'

Based on my understanding, you try to calculate the share in percent of features that have a score attribute lower than 3.

Note : As you have not specified filters, all the features will be used for this calculation (even the one outside the layout map).

Your formula should look like this :

round((
  aggregate ('your_layername_here','count', "score", "score"<=3)
  / aggregate ('your_layername_here','count', "score")
)*100,2)||'%'
1

I presume you're using an atlas layer for your layout and that score is a field from that layer.

The expression will result in 0% in the textbox when the atlas hasn't been activated.

If you activate the atlas in the layout window (click the world map icon button as circled in red below) it should show you the correct percentage per feature.

enter image description here

0

I think you should add to_string in front the first expression, cause you try to concatanate a double with a string!

1
  • Sorry, that is not correct. Inside labels data types don't matter, unless you're calculating something in order to use it as a label.
    – Erik
    Commented Oct 19, 2021 at 6:53

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