Important Community update: The process for changing your account details was updated on June 25th. Learn how this impacts your Community experience and the actions we suggest you take to secure your account here.

Alteryx Designer Desktop Discussions

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

How to build a for loop which is dynamically changing column values for each row?

Ashish6775
5 - Atom

Hi everyone!

 

I have below code which is written in SAS.

Capture.PNG

It is basically a loop which is looping from start_curve column to end_curve column. cal_day column is being incremented every time.

I have 366 columns named as days_calls1, day_calls2,...., day_calls_366.

I have 90 columns named as dayPct1, dayPct2, .......day_pct90.

 

How would I be able to achieve the same functionality in Alteryx?

Any ideas would be appreciated.

 

Input data would be as below:

cal_day is 1 in the input.

start_curvecal_dayend_curveday_calls1day_calls2day_calls3day_calls4day_calls5TotalCallsdayPct1dayPct2dayPct3dayPct4dayPct5
315000001013579
315000002013579
315000003013579
1140000040246810
1140000050246810

 

Output would look like this:

You can ignore cal_day of the output. It is being updated after every loop and more or less basically means how many times loop ran. 

 

Values from 1st to 3rd row for days_calls is calculated using dayPct3, dayPct4, dayPct5 as start_curve is 3 and end_curve is 5. (e.g. day_calls1 = totalCalls * dayPct3)

 

Values from 4th to 5th row for days_calls is calculated using dayPct1, dayPct2, dayPct3, dayPc4 as start_curve is 1 and end_curve is 4. (e.g. day_calls1 = totalCalls * dayPct1)

 

start_curvecal_dayend_curveday_calls1day_calls2day_calls3day_calls4day_calls5TotalCallsdayPct1dayPct2dayPct3dayPct4dayPct5
345507090001013579
345100140180002013579
345150210270003013579
15480160240320040246810
154100200300400050246810
5 REPLIES 5
binuacs
20 - Arcturus

@Ashish6775 Can you provide the output results of the first row? 

image.png

apathetichell
18 - Pollux

You'd use some form of nested macro or you'd use some form of generate rows. basically how you are doing it is a definition of how NOT to do it in Alteryx.. If you can share a few lines (say 3) of your 366 and your 90  (and expected outputs) - I can build this and you can run it for your remaning 366 of  (or unrandomized 366 - or whatever).

Ashish6775
5 - Atom

I have updated the question with sample input and output. Thank you.

apathetichell
18 - Pollux

@Ashish6775 Did you update this with sample data? Not seeing it beyond what you initially posted this. Trying to help out here - but if you don't post more comprehensive data it's a waste of our time... How are you getting the new cal_day values in your output? those are the kinds of things. The core x*y is straight forward but without something closer to real data this isn't productive.

Ashish6775
5 - Atom

@apathetichell Apologies for not being clear enough.

I have simplified the input and output data now. Assume these are the only columns. You can ignore the cal_day column in the output.

 

For now, I have used Python tool and wrote a script which replicates the scenario of SAS code.

 

#################################
from ayx import Package
from ayx import Alteryx

#################################
# read in data from input anchor as a pandas dataframe
# (after running the workflow)
df = Alteryx.read("#1")

#################################
for index, rows in df.iterrows():
    for n in range(df['start_curve'][index], df['end_curve'][index]):
        df['day_calls'+str(df['cal_day'][index])][index] = round(df['dayPct'+str(n)][index]*df['TotalCalls'][index], 0)
        df['cal_day'][index] =  df['cal_day'][index]+1


#################################
# and then send it to one of the output anchors
Alteryx.write(df, 1)

 

Labels