I am using QGIS 2.18

I have a shapefile that contains data of each state of India, and the number of people per state following a particular religion. Each feature has the following attributes: name, total population, total hindu, total muslim, total christian, (there are 8 fields on religion)etc etc.Taken from census data.

I want to do stylize my map in several ways that involve finding out the religion with the maximum people in each state.

So basically, the styling function I'd need performs something like this:

Case when ~Hindu is max~ then color: color1 when ~christian is max~ then color: color2 ....

I've been trying to see if I can compare across fields and then return the field name of which the maximum value belongs to. But I cannot find any function that does that.

Any tips on how to do this?

PS I have not studied POSTGIS,grass etc, so it would be great if I could be given answers that do not require these. I will be studying it soon though, so if it is inevitable to use that I will. But I want to see what I can do now.

1 Answer 1


What you could do here is create a new column that will contain the religion with the most followers for each given state.

In properties, go to Fields → Add a new field. Let's call it most_followers, and make it a text type.

Update the new column using the field calculator. You can use a formula that goes like this :

CASE WHEN "total_religion_1" = max("total_religion_1", "total_religion_2", ...)
     THEN 'religion_1'
     WHEN "total_religion_2" = max("total_religion_1", "total_religion_2", ...)
     THEN 'religion_2'
     WHEN "total_religion_8" = max("total_religion_1", "total_religion_2", ...)
     THEN 'religion_8'

Be careful to use the right quotation marks ! Double quotations for attribute names, and single quotation for text.

You can then use the Categorized style with this new attribute.

  • You can use a virtual field if you either: 1) don't want to/can't edit the original shapefile, or 2) will be changing the attribute values and want a field that updates when you make those changes.
    – csk
    Commented May 15, 2018 at 18:52

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.