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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors