Engine Works

Under the hood of Alteryx: tips, tricks and how-tos.
NeilR
Alteryx Alumni (Retired)

learnwithcode.png

 

Editor's note: you may need some background on what Macro Maynia is. Find it here.

If you know zero things about programming, by the end of this blog you'll know three. And you'll learn how those three things are like the different flavors of Alteryx macros.


Functions - They're Like Standard Macros

 

A function is a way to make a program snippet repeatable and abstract-able, so that you don't need to keep repeating the same code blocks all the time. Let's say you want to calculate the length of the hypotenuse of a right triangle given the lengths of the other two sides. If you've taken a geometry course, you know it's not hard...

 

pythagoras.svg

 

In the Python programming language, you can calculate c given a and b like so: c = (a ** 2 + b ** 2) ** 0.5

 

But what if you need to calculate c a lot, and don't want to have to type it out every time? That's where functions come in...

 

def pythagoras( a, b ): c = (a ** 2 + b ** 2) ** 0.5 return c print( pythagoras( 1, 2 ) ) print( pythagoras( 3, 4 ) )

Try it yourself!

 

Now instead of needing to type in the entire formula every time we need c, we just call the pythagoras function that we've defined and pass in the values of a and b. Note how we call the function twice with different values without needing to copy/paste the contents of the function itself. Anyone can reuse this function without needing to know how it works inside.

 

The equivalent in Alteryx is a macro! Macros are a way to create a new tool in Alteryx Designer by creating a special kind of workflow that includes interface tools. The macro appears to the end user as a tool, but the complexity of underlying workflow is hidden.

 

Calculating the length of the hypotenuse in Alteryx is easily accomplished in a Formula tool. Bookending the Formula tool with Macro Input and Macro Output tools turns the workflow into a macro.

 

Get this math out of my face!Get this math out of my face!

And this is how the macro appears to the end user that no longer needs to worry about the equation itself...

 

Much better - just like any other tool.Much better - just like any other tool.

 

While Loops - They're Like Iterative Macros

 

If you need to do the same thing over and over until a condition is met, you need a while loop. In keeping with the math theme, what better example than the Fibonacci sequence. Here's a Fibonacci sequence generator built in Python using a while loop within a function. All you need to do is specify the maximum value you want in the sequence, and the function will print out each number in the sequence below that limit...

 

def fibonacci( max ): i = 0 j = 1 while i < max: print(i) temp = j j = j + i i = temp return fibonacci( 10 )

 

In Designer, an iterative macro is like a while loop inside a function.

 

Fibonacci_macro.PNG

 

You can see the Formula tool is performing the same calculations on i and j and temp that are taking place inside the while loop above, and the i < max condition is performed in the Filter tool. The user of the tool specifies the max, and to them, it simply looks like this:

 

Fibonacci_tool.PNG

 

Finally, the data that flows out of the I Output gets fed back into the Input. You configure this in the interface designer.

 

For Loops - They're Like Batch Macros

 

Ever heard of Gottfried Leibniz? Among many other things, he came up with a cool way to calculate the value of pi.

 

pi.svg

 

If we wanted to write a script to approximate the value of pi to an arbitrary number of terms in the above series, we'd want a for loop - a loop that repeats a certain number of times. (Side note: math is weird.) Here's the leibniz function with a for loop inside. Try changing the terms parameter to see how high it has to be before you get to 3.14.

 

def leibniz( terms ): denominator = 1 multiplier = 1 sum = 0 for x in range(0, terms): sum += multiplier / denominator denominator += 2 multiplier = multiplier * -1 return sum * 4 print( leibniz( 100 ) )

 

An Alteryx batch macro is like a for loop - the contents of the macro will execute as many times as the number of rows being fed into it through the control parameter. But (surprise!) you can easily replicate the above logic in an Alteryx workflow (and therefore a standard macro) because a workflow is also like a for loop in that a tool's logic is applied across all rows of a dataset. Here's the guts of the standard macro...

 

See? No control parameter. Another standard macro.See? No control parameter. Another standard macro.

Here's what the configuration looks like to the end user:

 

config.png

 

One way you'll likely know if you need a batch macro is if you need to change the configuration of one or more tools for each iteration of the loop based on incoming data. For example, let's say you're constantly calculating statistics based on census data. People ask you to get the average county population in the state of Maryland, or the most populated county in California, etc. Finally, you just tell them to make you a list, and include the state and the statistic they're after. You can make your life a lot easier with a batch macro.

 

Here's what that list that your coworkers put together looks like:

 

table.PNG

 

And here's your batch macro:

batch.PNG

Those two tools at the top are the aforementioned control parameters. The one labeled State is updating the Filter tool to only look at a certain state; the Operation one is updating the Summarize tool with the relevant action - so that you don't have to!


All examples in this blog, both macros and code snippets are included in the attached workflow. Enjoy!

Neil Ryan
Sr Program Manager, Community Content

Neil Ryan (he/him) is the Sr Manager, Community Content, responsible for the content in the Alteryx Community. He held previous roles at Alteryx including Advanced Analytics Product Manager and Content Engineer, and had prior gigs doing fraud detection analytics consulting and creating actuarial pricing models. Neil's industry experience and technical skills are wide ranging and well suited to drive compelling content tailored for Community members to rank up in their careers.

Neil Ryan (he/him) is the Sr Manager, Community Content, responsible for the content in the Alteryx Community. He held previous roles at Alteryx including Advanced Analytics Product Manager and Content Engineer, and had prior gigs doing fraud detection analytics consulting and creating actuarial pricing models. Neil's industry experience and technical skills are wide ranging and well suited to drive compelling content tailored for Community members to rank up in their careers.