In this blog post, I'll be walking through my solution to Alteryx Challenge #222, which uses synthetic data relating to whale migration. I broke this challenge down into two tasks:
Task A: For each pod in each year, find the total number of kilometers swam.
Task B: For each pod, find the average number of kilometers each whale swam. Then, for each species in each year, find the pod with the highest average kilometers swam.
Planning in Excalidraw
I first used Excalidraw to plan out my data prepping process. I personally like to map out all my input and output tables so I can see exactly what I need to transform. I also wrote out loose steps pertaining to each task, as you can see on the right hand side:

Then, I imported the challenge starter file into Alteryx and translated these steps into a flow. I also organized my steps into colored containers to match the ones I wrote in Excalidraw.
Task A: Kilometers Per Pod Per Year
The first step in Task A is joining the migration and pods table on Whale_ID, since that's the field they have in common:

This effectively adds the Pod_ID to the migration data:

In this case, we don't have to worry about the outputs from the L and R anchors of the join, since nothing from either dataset was included. The same applies to the next step, which involves joining the result of the previous join with the species data on Pod_ID. This adds Whale_Species to the migration data:

In our next step, we want to sum up the kilometers swam, but there's a problem—Kilometers is currently a V_String! To change this, stay on this left pane of the second join tool, and change the datatype to Int32:

Now we can proceed as planned! In the Summarize tool, we first group by Whale_Species, Pod_ID, and Year; then, we sum the kilometers and find the distinct count of whales in each pod:

After aggregating with the Summarize tool, each row in our output table now represents a whale species in a pod in a specific year.
Task B: Average Kilometers Per Whale Per Pod and Pod With Highest Average
Since our data is grouped by pod, we can find the average kilometers per whale by dividing the total kilometers by the number of whales with a simple Formula tool. Just make sure your resulting field is a decimal datatype!

The trickiest part for me was this last bit, but I found it helpful to think about it in this way:
a. If we're looking for the pods with the highest averages, it makes sense to first sort by average so that the highest ones are at the top of our output:

b. Now we can use the Sample tool to get the first N rows per group. In this case, N = 1 because for each group we want a singular value—the highest average. The question is, what are we grouping by?
Recall that in our Summarize tool, we grouped by species, pod, and year, so each row up to this point is a distinct combination of those three fields. In this last step, we want to keep every combination of species and year but not pod, since we only want pods with the highest average.
Another way to word this is: for each species in each year, we want the pod with the highest average. Then it becomes clear that we group by just species and year.

Your output should now look like the table in screenshot above, which matches the output provided in the challenge file! 🐳🐳🐳

