Get Inspire insights from former attendees in our AMA discussion thread on Inspire Buzz. ACEs and other community members are on call all week to answer!

Alteryx Designer Desktop Discussions

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

Update Multiple Fields with Multi-row Formula Tool

owill
6 - Meteoroid

Hi all,

 

I am trying to use one calculation to update multiple fields using information from multiple rows. As an illustration, there are three pieces of logic I want to incorporate:

 

  1. I eat three apples a week
  2. I can carry 10 apples at a time
  3. If I buy 40 apples in a ten week period, I get the last five for free

What I imagine is the following fields:

 

Number of Apples I HaveWeekTotal Apples Purchased

10

110
7210
4310
1410
8520
5620
2720
9830
6930
131040
1000
700
400
100
8110
5210
2310
9420

 

Basically, I need a running total of the number of apples I have purchased so I know how many to buy in week ten. And I need a field that is a re-setting counter from the first purchase so I know when I've reached week ten.

 

Is there a way to update separate fields as I do a multi-row formula on a single field?

 

Thank you!

9 REPLIES 9
SeanAdams
17 - Castor
17 - Castor

Hey @owill

 

I don't believe that there's a way to do a multi-row formula, where you update two columns on each row (sadly - this would be a good idea - you should log it under IDEAS https://community.alteryx.com/t5/Alteryx-Product-Ideas/idb-p/product-ideas)

 

However - in all the cases that I've needed this, I've managed to find away around it by using formulas.   I've mocked this up in Alteryx and attached.   The key here is to do as much as possible using standard formulas.

 

Number of Apples I HaveWeekApples PurchasedApplesPurchasedLast10WeeksApplesEatenFreeApplesNumberRemainingAtEndWeekNumberApplesDiscarded
10110030177
72101030144
43102030111
1410303088
852040302515
562060352717
272080352414
9830100354131
6930130353828
131040160355545
100019035122
7001803599
4001703566
1001603533
8110140352010
521013035177
231012035144
9420100353121
owill
6 - Meteoroid

Hi Sean,

 

I will post this over there, thanks!

 

For the problem, I'm assuming we don't discard any (they're magic apples). I would also like the total apples purchased field to reset every time we hit ten weeks. I should have been more clear that I want the table as the output using the three pieces of logic. For instance, I can imagine writing something like the following to generate these rows:

 

IF
    [Row-1:Week] = 0 && [Row-1:Number of Apples I Have] - 3 >= 0
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3
    [Week] = 0
    [Apples Purchased] = 0
ELSEIF
    [Row-1:Week] = 0 && [Row-1:Number of Apples I Have] - 3 < 0
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3 + 10
    [Week] = 1
    [Apples Purchased] = 10
ELSEIF
    [Row-1:Week] < 9 && [Row-1:Week] > 0 && [Row-1:Number of Apples I Have] - 3 >= 0
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3
    [Week] = [Row-1:Week] + 1
    [Apples Purchased] = [Row-1:Apples Purchased]
ELSEIF
    [Row-1:Week] < 9 && [Row-1:Week] > 0 && [Row-1:Number of Apples I Have] - 3 < 0
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3 + 10
    [Week] = [Row-1:Week] + 1
    [Apples Purchased] = [Row-1:Apples Purchased] + 10
ELSEIF
    [Row-1:Week] = 9 && [Row-1:Apples Purchased] < 40
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3 + 40 – [Row-1:Apples Purchased]
    [Week] = [Row-1:Week] + 1
    [Apples Purchased] = 40
ELSEIF
    [Row-1:Week] = 9 && [Row-1:Number of Apples I Have] – 3 >= 0
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3
    [Week] = [Row-1:Week] + 1
    [Apples Purchased] = [Row-1:Apples Purchased]
ELSEIF
    [Row-1:Week] = 9 && [Row-1:Number of Apples I Have] – 3 < 0
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3 + 10
    [Week] = 1
    [Apples Purchased] = 10
ELSEIF
    [Row-1:Week] = 10 && [Row-1:Number of Apples I Have] – 3 >= 0
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3
    [Week] = 0
    [Apples Purchased] = 0
ELSE
    "[Row-1:Week] = 10 && [Row-1:Number of Apples I Have] – 3 < 0"
THEN
    [Number of Apples I Have] = [Row-1:Number of Apples I Have] – 3 + 10
    [Week] = 1
    [Apples Purchased] = 10
ENDIF

 

 

Hope that makes sense as I see it is a little dense.

 

Thanks for the help!

SeanAdams
17 - Castor
17 - Castor

Hey @owill

 

Made some updates based on your notes - screenshot; alteryx flow; and detailed workings below.   Note: I would avoid trying to code complex if-then code logic, you can do this much more elegantly in Alteryx.

Capture2.PNG

 

Here's what's going on:

 

Tool 1: Creates a weekID column which counts the week ID seen so far.   So the first week will be 1, second will be 2, and so on.   This will be helpful later

Resulting data from tool1:

WeekIDNumber of Apples I HaveWeekApples Purchased
110110
27210
34310
41410
58520
65620
72720
89830
96930
10131040
111000
12700
13400
14100
158110
165210
172310
189420

 

Tool 2 & 3: 

Given that you want to think in 10 week periods, I've created a new column that tells you what 10 week period you're in, and what specific week you are in during that 10 week period.

So, for example- weekID 1 is period 1; week1.   WeekID number 15 is period 2; week 5 in that period.   you can see these as the last two columns in this set below

Resulting data from tool 2&3:

WeekIDNumber of Apples I HaveWeekApples Purchased10WeekShoppingPeriodShoppingCounterWeekID
11011011
2721012
3431013
4141014
5852015
6562016
7272017
8983018
9693019
10131040110
11100021
1270022
1340023
1410024
15811025
16521026
17231027
18942028

 

Tool 4: Calculate the total purchase count in the 10 week period using a multi-row formula - you can see this in the last column below.   Note - you can do this with a summarise tool (grouping on the 10WeekShoppingPeriod column, and then join this back to the data if you wanted instead)

Because I reset the counter every time the 10WeekShoppingPeriod changes - it resets to zero (or the appropriate sum) by definition

 

Resulting data from tool 4:

WeekIDNumber of Apples I HaveWeekApples Purchased10WeekShoppingPeriodShoppingCounterWeekIDApplesPurchasedLast10Weeks
1101101110
272101220
343101330
414101440
585201560
656201680
7272017100
8983018130
9693019160
10131040110200
111000210
12700220
13400230
14100240
1581102510
1652102620
1723102730
1894202850

 

Tool 5: 

Here we calculate

- the number of apples eaten per week (defined as 3 in your brief);

- The number of free apples (for any week where it's the 10th week in the period, and I've purchased more than 40 so far in the month, give me 5 free)

- the number that remain at the end of the week (starting value + purchases + free apples - eaten apples)

 

Note: I had included a "Discarded Apples" column because your original brief said that you could only carry 10 apples at a time, so if you got to Friday with 7 you could start on Monday with 7 - but if you got to Friday with 25 you wouldn't be able to carry the other 15, and could only start the week on Monday with a maximum of 10.    However - I've left the column (your reply about them being Magic), and made the column zero

Resulting data from tool 5:

 

WeekIDNumber of Apples I HaveWeekApples Purchased10WeekShoppingPeriodShoppingCounterWeekIDApplesPurchasedLast10WeeksApplesEatenFreeApplesNumberRemainingAtEndWeekNumberApplesDiscarded
110110111030170
27210122030140
34310133030110
4141014403080
58520156030250
65620168030220
727201710030190
898301813030360
969301916030330
1013104011020035550
1110002103070
127002203040
134002303010
1410024030-20
158110251030150
165210262030120
17231027303090
189420285030260

 

What's next

So - depending on where you want to go next with this data - you should have a very good framework to start adding on - all without needing a multi-value-multi-row formula tool.

For example -

- if you wanted to set the next week's starting value based on the previous week's ending value - that's a simple multi-row formula at the end

- if you wanted to remove 10% of the apples that are still in the house when you get to Friday (really bad squirrels or mice invade your house every weekend) - that's a simple formula 

etc.

 

If I'm still missing the key challenge here - best way to get to a final solution would be to mock up the input data (what you start with before doing any work) and the output data (the output you want this workflow to deliver) and the basic rules in the middle in the way that @JoeM does in the weekly challenges (see here for an example - https://community.alteryx.com/t5/Weekly-Challenge/Challenge-1-Join-to-Range/m-p/36621#U36621).   BTW - I can't say enough good things about doing the weekly challenges - best training ever!

 

Alternatively - if this has solved your problem - would you mind marking this thread as solved?

Cheers

Sean

 

 

 

 

 

 

owill
6 - Meteoroid

Thanks for all the work.

 

I don't think I am explaining the problem very well.

 

As input I have the following:

  • Apples are eaten every day (-3)
  • Apples only get ordered when I run out (+10)
  • Apples get purchased at week 10 to make sure I bought 40 total in ten week timeframe (+ 40 - apples purchased in last 9 weeks)
WeekApples EatenApple Order Size10 Week Apple Discount
101040
231040
331040
431040
531040
631040
731040
831040
931040
1031040
1131040
1231040
1331040
1431040
1531040
1631040
1731040
1831040
1931040

 

The only output I really want to get is:

Number of Apples At the End of the Week
10
7
4
1
8
5
2
9
6
13
10
7
4
1
8
5
2
9
6

 

The pieces of logic I want to put in are:

  1. Don't buy more than you need
  2. Buy in quantities of 10, unless you buy extra in week 10
  3. Ten weeks after an initial purchase, buy up to 40
  4. Only buy again when you need to after buying extra from week 10
  5. Once you buy again, your next 10 week period starts

So for the first twenty weeks the sequence should be the following:

  • Week 1 - Buy ten apples (10) <-- Start of first ten week window
  • Week 2 - Eat three apples (7)
  • Week 3 - Eat three apples (4)
  • Week 4 - Eat three apples (1)
  • Week 5 - Buy ten apples, eat three apples (8)
  • Week 6 - Eat three apples (5)
  • Week 7 - Eat three apples (2)
  • Week 8 - Buy ten apples, eat three apples (9)
  • Week 9 - Eat three apples (6)
  • Week 10 - Buy ten apples, eat three apples (13) <-- To get 10 week discount
  • Week 11 - Eat three apples (10)
  • Week 12 - Eat three apples (7)
  • Week 13 - Eat three apples (4)
  • Week 14 - Eat three apples (1)
  • Week 15 - Buy ten apples, eat three apples (8) <-- Start of new 10 week window (have to buy total of 40 by week 24)
  • Week 16 - Eat three apples (5)
  • etc.

The logic is convoluted, but does that make sense? I thought the multiple fields (Apples Purchased & Week) would be the best way to keep track of how many I've bought and when I have to purchase the additional apples at the end of a given 10 week period.

 

Thanks for all the help!

SeanAdams
17 - Castor
17 - Castor

:-) I think I have the logic now:

 

- Week 1: eat zero and stock up for the apple-eating to come

- Every week thereafter

     - eat 3 apples

     - if you don't have 3 to eat, buy 10

     - In some weeks you can buy a bunch more apples to hit the quantity discount, according to the following logic:

                      The first time you buy apples, count this as discount week 1

                      Every week there after is discount week 2, 3, etc

                      In discount week 10, you can buy up to 40 apples to hit a cumulative total of 40 since discount week 1 (to hit discount pricing)

                      Whatever you buy in Discount week 10 - the discount week counter resets to zero until you buy again, then it starts from 1.

 

I think I now understand why you were struggling with the double multi-field-formula - I'm going to tackle this with an iterative macro which is the simplest way to loop through stuff until you hit a particular outcome.   An iterative macro can mimic the looping structure you're looking for, and it allows you to do multiple things on each loop iteration.

 

Hopefully get this done tonight, or tomorrow morning

 

Cheers @owill

Sean

 

 

 

 

owill
6 - Meteoroid

@SeanAdams

 

That totally makes sense, thank you! I will try to work with the iterative macro to get the logic built in.

 

 

SeanAdams
17 - Castor
17 - Castor

Here you go.

 

I've done this in a very explicit way - every formula broken out so that you don't have to decipher it - you could probably compact this a lot with a little thought, but here it is, and I've validated that it matches your data exactly, and also run it with a random input set.

 

The trick with Iterative Macros is getting your head around the idea that the input for the next round is the output from this - so the way that this works is basically:

- Do a bunch of calculations on the first week, figuring out what all the closing balances and counters are

- Pop these into the starting balance for the second week

- Send the first week to the output, and then send all other weeks to the "rework" process

 

That way, your record set gets 1 row shorter every iteration, and outputs 1 row every iteration.

 

This should solve your problem and hopefully give you an example that you can reuse in other challenges.  If so - would you mark as "Solved"?

Cheers

Sean

 

 

owill
6 - Meteoroid

Wow, this is amazing. Totally makes sense - thank you for all the help!

 

Seems like using macros is pretty powerful for niche applications.

SeanAdams
17 - Castor
17 - Castor
No worries at all - it was a nice challenge to help stretch my own thinking.

If I can recommend - @JoeM has a fantastic series of weekly challenges, and I've really found that they accelerated my learning (just look at the training tab on the left). If you're keen to start working through these, I'd recommend you do the following:

- Take a look at the list the Joe publishes - this contains a tag to say which are easy; intermediate; hard - list here: https://community.alteryx.com/t5/Weekly-Challenge/Weekly-Challenge-Index-amp-Welcome/td-p/48275

- Pick one of the beginner ones, and download the start file. For example - number 2; or 8 or 9 or 15

- Battle through this until you have a solution that works - no matter how awful your solution may be :)

- Submit your answer to the discussion board

- Then, once you've closed your solution - download @JoeM's solution; or some of the others on the forum - and try to understand how others have tackled this (if I see something novel, I often go-back and try to replicate this myself).

I've found that this is a powerful way to learn because it forces you to struggle for your own solution, and once you've got your head around the problem you have fertile soil to learn a few other tips and tricks.

I can't say enough about the learning power of the weekly challenges - check them out here, and I'll look out for some of your solutions on these challenges as you work through them: https://community.alteryx.com/t5/Weekly-Challenge/Weekly-Challenge-Index-amp-Welcome/td-p/48275

Cheers
Sean
Labels