7

I'm using QGIS to get some "stats" on a current layer e.g. to determine the number of features of a certain class. That I do by using the Aggregate-Algorithm on the class-attribute with count on "ID". I get a table that shows the number of features per class.

But how could I get another attribute giving me the percentage of the total number of features?

While this is easily done by hand (the number of total features in the layer can be easily determined, then it's just count / total_number), I don't know how to access the total number of features of the layer via an expression.

2 Answers 2

7

You can use a one-step expression using count() function:

count("ID","CLASS") / count("ID") * 100

This will return the percentage of the current class of the total feature count.

Where "ID" is a unique field and "CLASS" an identifier for your class, so not unique.


If you need an overview-table, you can run Statistics by categories from processing toolbox and then use "count" / sum("count") * 100 on the result table to calculate the percentage of each category.

0
6

The solution is to use the function aggregate().

I did it in a two-step process: Tool Aggregate with count as aggregation, and subsequently tool Field Calculator with the following expression (where count is the attribute respectively column created with the Aggregate tool):

"count" / aggregate(layer:=@layer, aggregate:='sum', expression:="count")

Aggregate settings

Field Calculator settings

Your Answer

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

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