Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Generate all possible combinations of values from a column

JackTequila
5 - Atom

Hi all,

 

My doubt is, how to generate all possible combinations of elements from a given list and assign each combination a unique group ID? Here are some examples

 

Input 1:

Value
A
B

 

Output 1:

GroupIDValue
1A
1B
2A
3B

 

Input 2:

Value
A
B
C

 

Output 2:

GroupIDValue
1A
1B
1C
2A
2B
3A
3C
4B
4C
5A
6B
7C

 

OBS: The real case input right now has 15 values, and it should generate 32767 groups (2^n-1).

 

Thanks.

11 REPLIES 11
Yoshiro_Fujimori
15 - Aurora
15 - Aurora

@JackTequila ,

This can be an interesting weekly challenge.

I understand we cannot use Recursive call of a function on Alteryx (except for in Python / R Tools).

My best bet is using Iterative Macro, but not sure how it goes for now...

Qiu
21 - Polaris
21 - Polaris

@JackTequila 
I hope the order of GroupID is not significant here. 😁
As commented by @Yoshiro_Fujimori , I use the Iterative Macro for generation of combinations.

 

0528-JackTequila.png

aatalai
14 - Magnetar

Unless I miss understood the ask, couldn't you just append group id and values?

Qiu
21 - Polaris
21 - Polaris

@aatalai 
You are correct on Appending tools but there is more I believe. 😁

JackTequila
5 - Atom

Your workflow seems to be working fine. However, unfortunately it is too compute expensive for my real case scenario (n=15). I tried to run it, gave up around 30 min of execution.

 

ChatGPT gave me a piece of python code able to run with n=15 in ~5 seconds using some iteration trick. Do you know if there is a way to convert this way of doing it to Alteryx? The python code works, so my quest should end here. But I'll have a hard time justifying the use of Python to my team, Alteryx's low code is practically a rule around here to facilitate future maintenance.

 

 

from ayx import Alteryx
import pandas as pd
import itertools

# Define the input DataFrame
input_df = Alteryx.read("#1")

# Extract values from the DataFrame
input_values = input_df['Column'].tolist()

# Create an empty list to hold the results
results = []

# Define a function to dynamically generate group values
def generate_group_values(input_values):
    group_id = 1
    length = len(input_values)
    
    # Generate all combinations for lengths from 1 to the length of input_values
    for r in range(1, length + 1):
        for combo in itertools.combinations(input_values, r):
            results.append({'Group': group_id, 'Column': combo})
            group_id += 1

# Generate the group values dynamically
generate_group_values(input_values)

# Expand the results list into rows of Group and Column
expanded_results = []
for item in results:
    group_id = item['Group']
    columns = item['Column']
    for col in columns:
        expanded_results.append({'Group': group_id, 'Column': col})

# Convert the results to a DataFrame
df = pd.DataFrame(expanded_results)

 

SPetrie
13 - Pulsar

I had a similar request recently and ended up making a batch macro for it. Its not as efficient as other code, but it seems to work well.

It can handle up to N=18 in a few seconds, but you will hit a wall soon afterwards. N=15 takes around 2 seconds in my flow example.

I concat the values to make it easier to read the output, but you can edit the macro to suit your own needs. 

Output1.PNGmacro.PNG

JackTequila
5 - Atom

Hi SPetrie. This is awesome

 

Can you please attach the workflow here? Its not showing up for me.

SPetrie
13 - Pulsar

Sorry about that. I thought I had attached it.

 

JackTequila
5 - Atom

This is really impressive, i ran it with n=20 and the execution time was ~7 seconds. After n=25 i hit the wall, but with this i have more than enough. Thanks a lot.

Labels
Top Solution Authors