Qlik Sense class function

The class function enables you to group numeric values into buckets that you can then use as a dimension in a chart.

Imagine you have the following data table:

Say you want to group these users into age buckets of 10 years. So 0 to 10, 11 to 20, 21 to 30 etc. Your first option is to do a nested if statement to achieve this, however if the group buckets are evenly spaced apart you can use the class() function to do this quickly. In the load script on the table you could add this:

class(Age, 10) as AgeGroup

Fred50 <= x < 10
Steve2020 <= x < 30
Bob3230 <= x < 40
Ellie5550 <= x < 60
Great! A common question that follows this is how can I format it in a nicer way? For example you may actually want ‘to’ rather than <=x <.

I recommend do a simple replace to achieve this.

replace(class(Age ,10), ‘<= x <‘, ‘to’) as AgeGroupclean

Bob3230 to 40
Ellie5550 to 60
Fred50 to 10
Steve2020 to 30
There are also two extra parameters in the class function that you can to replace the value of just x. This is another way to achieve the same result. If you are looking for uneven grouping I would suggest checking out the Interval Match or use a nested if statement for small number of groups.