We are celebrating the 10-year anniversary of the Alteryx Community! Learn more and join in on the fun here.
Start Free Trial

Alteryx Designer Desktop Discussions

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

How to use lag function in Alteryx?

tk12
7 - Meteor

Hello everyone,

 

I'm working on data set that looks like below:

 

Account NumberAccount status DescAs of date
1Active01-01-2019
2Close01-01-2018
3Charge off 01-01-2017
4Non-Accrual01-01-2016

 

What I want is, if previous account status is active and  if status is in (Close, Charge off, Non-Accrual) after that  add new column status with value 1.

 

Here is SAS code I have .

 

if lag(accountstatusdescription) = 'Active' AND accountstatusdescription in ('Chargeoff', 'Closed', 'Non-Accrual') then status = 1

if status > 0 then status + 1;

if lag(accountstatusdescription) = 'Active' AND accountstatusdescription in ('Chargeoff', 'Closed', 'Non-Accrual') then status = status - 1;

 

Account NumberAccount status DescAs of dateStatus
1Active01-01-20190
2Close01-01-20181
3Close01-01-20152
4Charge off 01-01-20173
5Charge off01-01-20154
6Non-Accrual01-01-20165

 

Any help would be greatly appreciated! 

 

Please let me know if you need more information! 

Thank you! 

2 REPLIES 2
Maskell_Rascal
13 - Pulsar

Hi @tk12 

 

You can get the desired results using the Multi-Row Formula tool. 

 

Maskell_Rascal_0-1611784180316.png

 

IF [Account status Desc]='Active' 
THEN 0 
ELSEIF [Account status Desc]!='Active' AND [Account status Desc]!=[Row-1:Account status Desc] 
THEN 1 
ELSE [Row-1:Status Alteryx]+1 
ENDIF

 

This is a great tool that will allow you to reference rows forwards/backwards, and you can even group by different fields like an account number. 

 

Interactive lesson on Multi-Row Formula https://community.alteryx.com/t5/Interactive-Lessons/Multi-Row-Formula/ta-p/82872 

 

If this solves your issue please mark the answer as correct, if not let me know!

 

Thanks!

Phil

tk12
7 - Meteor

Hello Phil,

 

Thank you very much. This is was very helpful. Will mark as complete. Thanks! 

 

 

Labels
Top Solution Authors