Alteryx Designer Desktop Discussions

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

Look back and count the rows it took for a value to beat its previous level.

akshaydalvicia
7 - Meteor

I am learning Alteryx and trying to figure out how to count the number of rows backwards/upwards it took for a price to beat its previous prices in "n" rows. Set the count to 0 if the Price is less than previous row price.

For sample purposes, I kept few rows but let say i want to look back 250 rows (n) at a time, how can i achieve that in Alteryx? So my lookback limit is 250. Shorter limits are also okay, if anyone has a better solution to solve this problem in alteryx.

 

I have explained it in the below example in Notes/comments column for the reasoning of the expected behavior in Notes/Comments column.

 

example:

DatePriceExpected outputNotes/Comments
11/12/2022110Why 0? coz no rows above to compare, Hence count is 0
11/13/2022100Why 0? coz 10 < 11 (previous price), hence count is 0
11/14/2022132Why 2? coz 13 > 10 (Previous price) and 13 > 11 (Previous to previous price), hence count is 2
11/15/2022110Why 0?  coz 11<13, hence count is 0
11/16/2022144Why 4? coz 14>11, 14>13, 14>10, 14>11, Hence count is 4 
11/17/2022120Why 0?  coz 12<14, hence count is 0
11/18/2022166Why 6? coz 16>12, 16>14, 16>11, 16>13, 16>10, 16>11
11/19/2022177Why 7? coz 17> 16, 17>12, 17>14, 17>11, 17>13, 17>10, 17>11
11/20/2022130Why 0?  coz 13<17, hence count is 0

 

8 REPLIES 8
DanielG
12 - Quasar

Hi @akshaydalvicia 

 

Check this out.  It appears to work on the small sample.  Give it a shot on your larger dataset and let me know how it goes.

 

Thanks

Yoshiro_Fujimori
15 - Aurora

Hi @akshaydalvicia ,

 

To simplify the logic, I took the liberty of re-defining your requirement as below.

If the Price of the current row is the biggest value ever, set ([current row number] - 1).

Otherwise, set 0.

If this definition satisfies your needs, here is one way of doing it.

 

Workflow

Yoshiro_Fujimori_1-1686389906994.png

 

Multi-Row Formula tool expression

[BiggestEver] = IF [Price] > [Row-1:BiggestEver] THEN [Price] ELSE [Row-1:BiggestEver] ENDIF

 

Formula tool expression

[IsBiggestEver]   = [Price] = [BiggestEver]

[Expected output] = IF [IsBiggestEver] THEN [RecordID] ELSE 0 ENDIF

 

Output

Yoshiro_Fujimori_2-1686390089830.png

 

I hope this helps.

akshaydalvicia
7 - Meteor

Hi Yoshiro_Fujimori and DanielG.

Thank you for your post and I definitely learnt something new from each of them.

I am sorry, but i did not mean to change the goal-post here. The Price is not going to be the biggest ever. I made my sequence too simplistic.

Hence i added 1 more row item (last item) to the table where a current price can be > previous price but not necessarily the biggest price of the lookback (Let say 250 rows). Thanks for opening my eyes and allowing me to re-define this problem again.

 

 

DatePriceExpected outputNotes/Comments
11/12/2022110Why 0? coz no rows above to compare, Hence count is 0
11/13/2022100Why 0? coz 10 < 11 (previous price), hence count is 0
11/14/2022132Why 2? coz 13 > 10 (Previous price) and 13 > 11 (Previous to previous price), hence count is 2
11/15/2022110Why 0?  coz 11<13, hence count is 0
11/16/2022144Why 4? coz 14>11, 14>13, 14>10, 14>11, Hence count is 4 
11/17/2022120Why 0?  coz 12<14, hence count is 0
11/18/2022166Why 6? coz 16>12, 16>14, 16>11, 16>13, 16>10, 16>11
11/19/2022177Why 7? coz 17> 16, 17>12, 17>14, 17>11, 17>13, 17>10, 17>11
11/20/2022130Why 0?  coz 13<17, hence count is 0
11/20/2022 151Why 1?  coz 15>13 but 15<17, hence count is 1
Yoshiro_Fujimori
15 - Aurora

Hi @akshaydalvicia ,

 

No problem. It happens when testing with larger data set.

I think I could follow your needs with a little tweak.

The idea is that we need to remember which rows are biggest ever.

If the current row has the biggest price ever, the Row No (starting with 0) is the output.

Otherwise, the row count (starting with 0) after the latest "bigesst ever" is the output.

I hope this change fits your needs.

 

Workflow

Yoshiro_Fujimori_0-1686623319463.png

Output

Yoshiro_Fujimori_1-1686623333999.png

 

Multi-Row Formula tool (1)

BiggestEver = IF [Price] > [Row-1:BiggestEver] THEN [Price] ELSE [Row-1:BiggestEver] ENDIF

 

Multi-Row Formula tool (2)

RowNo_BiggestEver = IF [Price] = [BiggestEver] THEN [RecordID] ELSE [Row-1:RowNo_BiggestEver] ENDIF

 

Formula tool

Expected output = IF [Price] = [BiggestEver] THEN [RecordID] ELSE [RecordID] - [RowNo_BiggestEver] - 1 ENDIF

 

DanielG
12 - Quasar

@akshaydalvicia - @Yoshiro_Fujimori That is a great solution right there and shows why the multi-row tool is one of the best tools that Alteryx has to offer.  😊

akshaydalvicia
7 - Meteor

Hi  Yoshiro_Fujimori,

I accepted it as a solution. It unintentionally answered a question which i did not think of and this feature is also needed where i want to know how far a Price is from the "Biggest Ever Price" in the past?

 

My current problem is not Biggest Price ever in the past. My original intention is the first occurence of the price in the past (Descending Date lookback) which is Greater than Price in the current row. Take a look at the last row in the following table which i newly added:

 

DatePriceExpected outputNotes/Comments
11/12/2022110Why 0? coz no rows above to compare, Hence count is 0
11/13/2022100Why 0? coz 10 < 11 (previous price), hence count is 0
11/14/2022132Why 2? coz 13 > 10 (Previous price) and 13 > 11 (Previous to previous price), hence count is 2
11/15/2022110Why 0?  coz 11<13, hence count is 0
11/16/2022144Why 4? coz 14>11, 14>13, 14>10, 14>11, Hence count is 4 
11/17/2022120Why 0?  coz 12<14, hence count is 0
11/18/2022166Why 6? coz 16>12, 16>14, 16>11, 16>13, 16>10, 16>11
11/19/2022177Why 7? coz 17> 16, 17>12, 17>14, 17>11, 17>13, 17>10, 17>11
11/20/2022130Why 0?  coz 13<17, hence count is 0
11/21/2022 151Why 1?  coz 15>13 but 15<17, hence count is 1
11/22/2022162Why 2? coz 16>15, 16>13, 16<17, hence the count is 2
11/23/2022130Why 0, coz 13 < 16
11/24/2022141Why 1? coz 14>13 but 14<16, hence the count is 1. Although 17 is the Biggest Ever but 16 on 11/22 is still Greater than 14, hence the expected output is 1 and not 5.
Yoshiro_Fujimori
15 - Aurora

Hi @akshaydalvicia ,

 

Here you go. I hope it works.

To compare the price of each row with the older rows, Batch Macro comes in handy.

 

Main Workflow

Yoshiro_Fujimori_0-1686802169344.png

Macro

Yoshiro_Fujimori_2-1686802194143.png

 

Output

Yoshiro_Fujimori_3-1686802427812.png

 

akshaydalvicia
7 - Meteor

Fantastic and Thank you so much @Yoshiro_Fujimori for being patient. All open threads close here related to this discussion. The large dataset humbled me everytime, i thought my example provided here was correct.

So much to learn from these Macros and workflows that you provided. Thanks a ton Appreciate it!

Labels