Bring your best ideas to the AI Use Case Contest! Enter to win 40 hours of expert engineering support and bring your vision to life using the powerful combination of Alteryx + AI. Learn more now, or go straight to the submission form.
Start Free Trial

Alteryx Designer Desktop Discussions

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

Find Reoccurring Results

Yatesaj8
7 - Meteor

I'm trying to figure out how many consecutive weeks a customer appears in a report. For example, in the below screenshot, customer 1 appears for 4 consecutive weeks (5 through 8). Customers 2 and 3 only appear once. The actual data is obviously much more complex and I would love a way to automate in Alteryx. The other issues is that a customer may appear multiple times in the same week or appear in non consecutive weeks. I don't know if that changes how your would approach the problem.

 

 

CustomerWeek
15
16
17
18
22
34
13 REPLIES 13
apathetichell
20 - Arcturus

Sure!

 

Let's go through this piece by piece:

 

if [Row-1:Week]=null() then 1 elseif tonumber([Row-1:Week])=tonumber([Week])-1 then [Row-1:in-a-row]+1 else [Row-1:in-a-row] endif

 

[Row-1:week] is the row prior to the current week - it looks to see if it doesn't exist. This would be true for the very first row and it also would be true for EVERY row which starts a new grouping - since we grouped on customer. This means that even if customer name pops up again later in the list - it'll still populate at 1. The group option also means that our numbers won't continue if a different customer went to the store the following week.

 

1 sets the value.

 

tonumber() converts to number from string

[row-1:week]=[week]-1 says are these in a row. Ie is the value for week in the previous row one less than the current value. If so - then we'll add 1 to the previous value (ie the number of weeks in a row a customer visited the store).

 

else [row-1:in-a-row] this tells atleryx to look at the previous entries value here and just carry it over. It's kind of as close to recursion at Alteryx gets.

 

ENDIF. in alteryx you need to conclude an if statement with endif OR IT WILL NOT WORK. The three most common errors on If statements

1) comparing unlike types (ie strings and integers)

2) not including endif

3) not including else statements.

 

unlike Excel and many programming languages - Alteryx requires all If statements to have elses and endifs at the conclusion of the statment.

Yatesaj8
7 - Meteor

I do have one more question that expands on this a bit. My new goal is to find consecutive weeks prior to (and including) the current week that a customer appears in the data set. So if we are currently in week 5 and a customer appears in weeks 5, 4, 3, and 1, they would show as 3.  I think I have it set up correctly and am trying to figure out the formula. I attached a document with some mock data and sample output.

 

Any help would be greatly appreciated!

apathetichell
20 - Arcturus

Do you care about previous streak or just any streak leading up to the current week - also will current week be real time (ie dattimetoday()) or integer based?

 

My strategy for doing a streak from current week down (integer or date sorted really) would be change the  sort to sort by customer ascending and week descending (with the assumption that no data can exist beyond the current week). Then I'd count backwards and look for situations where [row+1week] is not equal to [week]-1 (ie the previous week).  This would give you a count of consecutive weeks backwards.

 

If you are using datetime it gets a little different but the idea/functionality is the same.

Yatesaj8
7 - Meteor

For this I would only care about the current streak. Current week will be interger based. I'll use datetimetoday() but will convert to an interger.

 

How would you approach that counting backwards solution you proposed?

Labels
Top Solution Authors