Forum Discussion

HeirsPowerBi's avatar
HeirsPowerBi
Helper I
4 years ago
Solved

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.

 

  • 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"

     

3 Replies

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    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"

     

    • HeirsPowerBi's avatar
      HeirsPowerBi
      Helper I

      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.