I have a point layer named 'observation', with different attributes and one called "Année". My project also have a variable called @Année.

In the layout, I have a A0 page (1189x841), but I want an extra space to put my attribute table (1cm for each row). So I'm trying to calculate the size with this formula, and it's worked fine:

841 + (10 * layer_property('Observation', 'feature_count'))

My problem is that my attribute table shows only the observations for the given year (@Année), with this formula:

to_string(year("Année")) LIKE @Année

How can I adapt layer_property('Observation', 'feature_count') to add a filter, so it will count only the observation for a given year?

(EDIT) I tried the solution with aggregate, but it doesn't work, cause it says "No feature was found on this layer to evaluate the expression", so I can't click on OK. enter image description here

(EDIT2) The expression works to calculate a field for exemple: enter image description here But as an expression to set the size of the page, I still have the problem... and it's exactly the same expression: enter image description here

  • Remove filter:=to_string(year("Année")) LIKE to_string(@Année) and trailing coma at the end of expression:=... line to see if it works without filter. It's the equivalent of layer_property('Observation', 'feature_count'). The goal is to sort out where is the issue
    – ThomasG77
    Commented Oct 15, 2021 at 8:29
  • I still have the same issue, even if I removed the filter.
    – katagena
    Commented Oct 15, 2021 at 8:32
  • I do not have it in both cases, with & without filter. If it does not work without filter, I wonder where the issue could be
    – ThomasG77
    Commented Oct 15, 2021 at 8:41
  • OK, with this expression, I have something: aggregate('aa_6d49a2f5_1d8a_478d_8a79_96d0b1af30a8', aggregate:= 'count', expression:='Année du relevé').
    – katagena
    Commented Oct 15, 2021 at 8:41
  • So the field value for the expression must be between ', and not ".
    – katagena
    Commented Oct 15, 2021 at 8:42

1 Answer 1


You can try the following (tested)

with_variable('Année', '2019',
    filter:=to_string(year("Année")) LIKE to_string(@Année)


Unable to make things work in the expression for page size, you can try to use the custom Python function

from datetime import datetime

from qgis.core import *
from qgis.gui import *
from qgis.utils import *

@qgsfunction(args='auto', group='Custom')
def get_count_filtered(layer_name, field_name, variable_val_name, feature, parent, context):
    """ Returns the value of the variable 'var_name' 
        Example usage: env('user_full_name') """
    project = QgsProject.instance()
    layer = project.mapLayersByName(layer_name)[0]
    variable_val = QgsExpressionContextUtils.projectScope(project).variable(variable_val_name)
    vals = [str(feat[field_name]) for feat in layer.getFeatures() if feat[field_name] is not None]
    vals = [d for d in vals if str(datetime.strptime(d, '%Y-%m-%d').year) in variable_val]
    return len(vals)

Then use in the expression

get_count_filtered('Observation', 'Année du relevé', 'Année')
  • It doesn't work. See the edit above.
    – katagena
    Commented Oct 15, 2021 at 7:36
  • See my edit, replaced expression:="", with expression:='whatever',. It could also be expression:="any_existing_field_name",
    – ThomasG77
    Commented Oct 15, 2021 at 7:59
  • Sorry, but I still have the same issue... Did you test the expression where we set the size of the page? Cause if I use this expression to calculate a field in a layer, it's work! But not to generate the size of the page?!?? See above for edit2.
    – katagena
    Commented Oct 15, 2021 at 8:12
  • No, tested in an expression context. Will look on size of the page and edit then
    – ThomasG77
    Commented Oct 15, 2021 at 8:16
  • 1
    I've tested the Python expression and contrary to you, it was working in the page size too. Sorry, it does not work for you. I suppose it needs some adaptation
    – ThomasG77
    Commented Oct 18, 2021 at 8:07

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.