Alteryx Designer Desktop Discussions

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

**Balance Positive and Negative Amounts**

Divyajyothi7
8 - Asteroid

Hi
I need to balance positive and negative Amounts where sum is equal to zero.

for Example :

 

Input :

 

rowAmount
1101.05
2-50.45
3-50.6
430.99
540.67
667.78
7-30.55
8-44.33
9-33.57
1060.77

 

Expected output :

rowAmounttype
1101.05balanced
2-50.45balanced
3-50.6balanced
430.99 
540.67balanced
667.78balanced
7-30.55balanced
8-44.33balanced
9-33.57balanced
1060.77 

 

As shown in the expected output, Sum of balanced column is equal to zero

As shown in the expected output  from row 1 to 2 sum is zero ( balance is done based on 1 : M combination , 1 positive and M negatives)
and from row 5 to 9 sum is zero ( balance is done based on M : M combination , Many positives and Many negatives)
I need to build a workflow which balances amount with combination 

 

expected output should be tested with data set -1 and data set -2  excel file

 

is it possible with python tool in Alteryx ?

9 REPLIES 9
griffinwelsh
11 - Bolide

You can do this through Python of course, but here is an Alteryx only solution using an iterative macro. Basic idea is start at the top of the file and get a running total. Check for the first row where running total is 0. If no rows found go to the next row. Else flag the set as balanced and go to 1 + the last row number.

binuacs
20 - Arcturus

@Divyajyothi7 attaching the python version

image.png

Divyajyothi7
8 - Asteroid

@griffinwelsh  

I have tried your logic for the attached data set. Results are not as expected, every row of balanced column showing as null()

could you please check with the attached 2 datasets

Divyajyothi7
8 - Asteroid

@binuacs 

thank you 

your code is working with dataset and giving expected results.

is it possible to get expected results without python ?

griffinwelsh
11 - Bolide

@Divyajyothi7 sorry for the oversight. With your data set we were getting floating point rounding errors in my original solution. Attached solution uses the fixed decimal data type to fix this and should produce the results you need.

Divyajyothi7
8 - Asteroid

@griffinwelsh 

with sample data we are getting expected results.

but with data in excel, we are not getting expected results

 

could you please check ??

griffinwelsh
11 - Bolide

@Divyajyothi7 I don't see any sets that should return balanced from your data set unless the sequence of rows does not matter. For example can row 3 and row 9 balance each other or do the balancing rows have to be consecutive?

Divyajyothi7
8 - Asteroid

@griffinwelsh 
I have checked your workflow with this data, no amounts are balancing
As you can see the following Amounts are balanced with this data

could you please check ??
original dataset amounts column will be sorted in asc order

 

Amounttype
-751228.5 
-68039.4 
-52478.9 
-43102.3 
-31487.8 
-24178.3 
-16844.4 
-9489.4 
-3641.7 
-2766.3 
32508.03 
1236972.49 
-116000Balanced
-49125Balanced
-37948.84Balanced
-18792.43Balanced
-6667Balanced
-3587.84Balanced
10254.84Balanced
67917.43Balanced
153948.84Balanced

 

griffinwelsh
11 - Bolide

Sorry for the confusion, but I thought you were asking to consider only adjacent rows for balancing. When considering all possible combinations I won't be able to build anything nearly as efficient as Binaucs solution in python. I recommend you go that route.

Labels