I created a similar pattern which uses the modulo operation to build dynamic tiers. The big difference is that you get concatenated tiers like 10-20, 20-30, etc. This is what the tiers look like:

#### Mathematical Approach:

Like I said before, I use the modulo operation (denoted by %) to calculate the tiers. The approach can be explained with some simple math:

- Let
*r* represent the user-defined bucket size
- Let
*n* represent the the value for which we want to assign a bucket

Weâ€™ll first calculate the lower bound of the bucket with the following formula:

*n - n % r*

We then calculate the upper bound with this formula:

*n - n % r + r*

**Example:**

Let *r* = 5

Let *n* = 33

*n* % *r* = 33 % 5 = 3

This implies:

Lower bound = 33 - 3 = 30

Upper bound = 33 - 3 + 5 = 35

When then concatenate these two values and get 30 - 35 as our bucket.

#### The LookML

First, I create a parameter that enables the end user to select his or her desired bucket size:

```
parameter: bucket_size {
default_value: "10"
type: number
}
```

I then build a dimension that takes the user input from the `bucket_size`

parameter and calculates even-sized buckets.

```
dimension: dynamic_bucket {
sql:
concat(${sale_price} - mod(${sale_price},{% parameter bucket_size %}),
'-', ${sale_price} - mod(${sale_price},{% parameter bucket_size %}) + {% parameter bucket_size %})
;;
order_by_field: dynamic_sort_field
}
```

One annoyance is that this dimension is a string, which can cause issues with sorting. For example, the value 100-110 would come before 90-100 if we applied an ascending sort on our bucket. For this reason, weâ€™ll need to build a sort field which determines â€“ you guessed it â€“ how our dimension will sort.

```
dimension: dynamic_sort_field {
sql:
${sale_price} - mod(${sale_price},{% parameter bucket_size %});;
type: number
hidden: yes
}
```

Because our sort field is now a number instead of a string, the dynamic bucket column will sort as intended. Itâ€™s also worth noting that this dimension has the same cardinality as our bucket, which ensures that our result set does not fan out. Check out this post to learn more about fanouts.

â€“

Thatâ€™s it! Go ahead and bucket away to your heartâ€™s desire.