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:
Name
Age
Bob
32
Ellie
55
Fred
5
Steve
20
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
Name
Age
AgeGroup
Fred
5
0 <= x < 10
Steve
20
20 <= x < 30
Bob
32
30 <= x < 40
Ellie
55
50 <= 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
Name
Age
AgeGroupclean
Bob
32
30 to 40
Ellie
55
50 to 60
Fred
5
0 to 10
Steve
20
20 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.