Aggregate Operator: Count By Using Group Key
Overview
You'll sometimes use the Aggregate operator to count how many entries you have in a data set. To do this, you'll use the Count By function. You can also use the operator's Group Key field while performing this operation. This lets you separate the operation into categories based on your original data.
For example, let's say you have a Data Table that holds the details of several dogs, including their favorite foods. You can create a Data Workflow to count how many dogs have the same favorite food. In this case, each kind of food will show as a separate category thanks to the Group Key field.
Here's how your module will look in the Module Builder:
Here's how the completed use case will look in Express View, including a peek at the DevTools Console:
What You'll Learn
In this article, you'll learn how to use the Aggregate operator's Count By function.
What You'll Need
To set up this use case, you’ll need:
-
1 Data Table component
-
1 Button component
-
1 Data Workflow component
To set up your Data Workflow, you'll need:
-
1 Input operator
-
1 Aggregate operator
-
1 Console operator
These instructions assume you have a new module open, saved, and with a title.
Configuration
Configure the Data Table Component
First, let's add your Data Table. This will hold the data you'll bring into your Data Workflow. Here, you'll set up a list of dogs, storing their name and favorite food.
1. | Drag and drop a Data Table onto your canvas. |
2. | Enter dtDogs in the Label and Property Name. |
3. | In the data table, enter the following: |
A |
B |
---|---|
name |
favoriteFood |
Ivy |
Peanut Butter |
Lucy |
Beef Jerky |
Wally |
Peanut Butter |
Nacho |
Peanut Butter |
Thor |
Salmon |
Holly |
All Foods |
4. | Click Save. |
Configure the Data Workflow Component
Next, you'll add your Data Workflow component. You'll bring your data into the Data Workflow using an Input operator. Then, you'll add an Aggregate operator to count how many dogs have the same favorite food. But to get an accurate count, you'll need to count the names associated with each favorite food. So, you'll set the Aggregate operator to use the Value Key of name and the Group Key of favoriteFood. Finally, you'll show the result using a Console operator.
1. | Drag and drop a Data Workflow onto your canvas. Place your Data Workflow below your Data Table. |
2. | Enter dwfFavoriteFood in the Canvas Label Text and Property Name. |
Configure the Input Operator
1. | Drag and drop an Input operator onto your Data Workflow canvas. |
2. | Configure the Input operator's Info window as follows: |
Setting |
Value |
---|---|
Category |
Input |
Component |
dtDogs |
Required |
Yes |
Source |
Default |
Configure the Aggregate Operator
1. | Drag and drop an Aggregate operator onto your Data Workflow canvas. |
2. | Configure the Aggregate operator's Info window as follows: |
Setting |
Value |
---|---|
Category |
Aggregate By |
Label |
Favorite Food Counts |
Aggregation Type |
Count By |
Join Group Keys |
Yes |
Group Key |
favoriteFood |
Value Key |
name |
3. | Connect the output port (right) of the Input operator to the input port (left) of the Aggregate operator. |
Configure the Console Operator
1. | Drag and drop a Console operator onto your Data Workflow canvas. |
2. | Configure the Console operator's Info window as follows: |
Setting |
Value |
---|---|
Category |
Console |
Label |
How many dogs have the same favorite food? |
3. | Connect the output port (right) of the Aggregate operator to the input port (left) of the Console operator. |
4. | Click Save. |
Configure the Button Component
Now, you'll add a Button component to start the whole operation. Clicking your button fires your Data Workflow. From there, your Data Workflow takes care of the rest by counting the dogs who have the same favorite food.
1. | Drag and drop a Button component onto your canvas. Place your Button below your Data Workflow. |
2. | Enter btnFavoriteFood in the Property ID. |
3. | Enter Count Favorite Foods in the Label Text. |
4. | Select Event from the Button Action Type. |
5. | Enter dwfFavoriteFood in the Trigger on Click. |
6. | Click Save. |
7. | Save your module. |
Now, preview your module in Express View and open the DevTools Console. Next, click your button. You'll see a list of each food mentioned in your Data Table. Along with each food, you'll see how many dogs love it!
Lab
You can view this complete use case here: https://training.unqork.io/#/form/5f68c43babf886024d4db7c0/edit.