Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
HeirsPowerBi
Helper I
Helper I

How to get previous state for each state per customer

Hello Guys i have a dataset  that looks like this

 

TaskDateState
A1/1/2022Enter
A1/2/2022Break
A1/3/2022Continue
B1/4/2022Enter
B1/5/2022Fall out
B1/6/2022Carrier Step
C1/7/2022Enter
C1/8/2022Go Live
C1/9/2022Retrace

 

I am trying to create another column that will tell me the previous state based on the date for each state per task.

output would look like this

 

TaskDateStatePrevious State
A1/1/2022Enter 
A1/2/2022BreakEnter
A1/3/2022ContinueBreak
B1/4/2022Enter 
B1/5/2022Fall outEnter
B1/6/2022Carrier StepFall out
C1/7/2022Enter 
C1/8/2022Go LiveEnter
C1/9/2022RetraceGo Live

 

I need to do this in power query because there are some other transformations i need to with that column before loading it.

 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Solution file uploaded to - https://1drv.ms/x/s!Akd5y6ruJhvhuSf4jBWInubgMG_Q?e=K1J6Dq 

Below is M Code

 

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Task", type text}, {"Date", type date}, {"State", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Task"}, {{"Temp", each _, type table [Task=nullable text, Date=nullable date, State=nullable text]}}),
    //Function Start
    fxProcessTable = (InputTable)=>
        let
            #"Added Index" = Table.AddIndexColumn(InputTable, "Index", 0, 1, Int64.Type),
            #"Added Custom" = Table.AddColumn(#"Added Index", "Previous State", each try if [State]="Enter" then "null" else #"Added Index"[State]{[Index]-1} otherwise null),
            #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
        in
            #"Removed Columns",
    //Function End
    #"Custom" = Table.AddColumn(#"Grouped Rows", "RunFunction", each fxProcessTable([Temp])),
    #"Expanded RunFunction" = Table.ExpandTableColumn(Custom, "RunFunction", {"Date", "State", "Previous State"}, {"Date", "State", "Previous State"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded RunFunction",{"Temp"})
in
    #"Removed Columns"

 

View solution in original post

3 REPLIES 3
Vijay_A_Verma
Super User
Super User

Solution file uploaded to - https://1drv.ms/x/s!Akd5y6ruJhvhuSf4jBWInubgMG_Q?e=K1J6Dq 

Below is M Code

 

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Task", type text}, {"Date", type date}, {"State", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Task"}, {{"Temp", each _, type table [Task=nullable text, Date=nullable date, State=nullable text]}}),
    //Function Start
    fxProcessTable = (InputTable)=>
        let
            #"Added Index" = Table.AddIndexColumn(InputTable, "Index", 0, 1, Int64.Type),
            #"Added Custom" = Table.AddColumn(#"Added Index", "Previous State", each try if [State]="Enter" then "null" else #"Added Index"[State]{[Index]-1} otherwise null),
            #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
        in
            #"Removed Columns",
    //Function End
    #"Custom" = Table.AddColumn(#"Grouped Rows", "RunFunction", each fxProcessTable([Temp])),
    #"Expanded RunFunction" = Table.ExpandTableColumn(Custom, "RunFunction", {"Date", "State", "Previous State"}, {"Date", "State", "Previous State"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded RunFunction",{"Temp"})
in
    #"Removed Columns"

 

I figured out a way around that is similar to this but was not working but your solution has pointed me to what was missing.

Thank you so much, this helped alot

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.