
One common scenario we've encountered is users wanting to group values together in "buckets," such as 1-2, 3-5, 6-10, 10-20, and so on. This article explains how to do this using the IF function along with the SORT and GROUP/GROUPBY functions. This article provides an example for using this method on a spreadsheet data source and an XML data source.
The IF function lets you set up a condition using values from your data and tests the validity of that condition. If the condition is true, then the function will display the if true element; if the condition is false, then the function will display the if false element. These two elements are set by the user and can display almost any type of value, such as an image, literal value, or a selection of data. To understand how this works for grouping values in buckets, let's look at a simple example.
How to use the IF function to group values into buckets (Spreadsheet data source)
The IF function in the image above only tests a single condition (A:A is less than or equal to 1), and returns a "1" where true, and a "false" where false. To start to group values together, you will replace the text "false" with a second, nested IF function.
Note: The SLICE function is used to simplify the data selection for use in the Klip. See this blog post for more information on using the SLICE function to format data.
Now you will need to create a second condition, so that when the first condition is not met, the IF function will test the next condition. So if A:A is not less than or equal to 1, you want to set up a condition that states A:A is greater than or equal to 2. If true, the text will show "2 to 10"; if false, the text will show "false".
The obvious problem with the above IF function is that it groups values together in an open-ended framework. In other words, everything greater than or equal to 2 is grouped together, but this isn't necessarily practical. To limit the range of this statement, insert another IF function into the if true element, instead of the text 2 to 10. Now you can create a second statement for this IF function that says less than or equal to 3.
The next step is to insert another IF function in the if false element to group the remaining values together. Create a condition that states A:A is greater than or equal to 4, and then in the if true element, use a second IF function to create a condition that states A:A is less than or equal to 10. Now all the values in the data source are accounted for.
The next step is simple. Click on the first IF function, so the entire formula is selected, and then select the function wrap symbol. Insert a GROUP function. The values are grouped according to how they are divided by the IF function.
Learn more: The GROUP and GROUPBY functions
Now that this formula is set up, it is straightforward to use it to return values that align with each "bucket". The first thing you need to do is copy the entire formula (see copy and pasting formulas) and paste it into a second column. Next, you will need to replace the GROUP function with a GROUPBY function.
The final step is to insert the values B:B into the measure element of the GROUPBY (or whatever range of values that correspond to the buckets). The default method for the GROUPBY is summarizing the data, so this will work fine for our purposes.
Grouping values in an XML data source (Google Analytics)
The method above works exactly the same for XML data sources like Google Analytics (see this article to get started with XML data sources). The only difference is that the values may not be sorted in a logical order, so you will need to sort them using the SORT function. The example below illustrates this problem when grouping values in a Google Analytics data source.
Check out this article to learn how to create a Google Analytics data source.
To sort these values out, you will need to use the SORT function. This function returns values in the order specified. In this case, the values you want returned are the values grouped by the IF function. To put these values in ascending order (1, 2, 3, etc), you need to provide the function with the current order of the values. In the case of this example, "1, 4, 2, 5, 3" (shown in the next column).
The tricky part is getting the grouped values into the SORT function. To do this, you will use the copy and paste technique. Start by copying the entire formula and then delete the formula (don't worry, you just copied it). Next, insert a sort function and then paste the grouped values into the return_values element.
Check out this article to learn how to copy and paste formula elements.
The next part is to insert the current order of values into the "values" element. This tells the function what order the values are currently in. Next, type "ascnumeric" in the type element so that the values are sorted in the proper order.
Now with the grouped values sorted, it is straightforward to return values that align with each bucket. Copy the entire formula including the SORT function and paste it into the adjoining column. Once again, replace the GROUP function with the GROUPBY function.
The last step is to select a value from the data source that corresponds to the buckets. In this case, the value is the number of people who've visited the page X number of times. The GROUPBY summarizes the data by default, so once you select this value the buckets will align with the proper values from the data source.