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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
TK12345
Resolver I
Resolver I

Find value based on previous rows and other column

Hi guys,

I need help with the following:

I need to create the column: expected outcome enddate

This column needs to be based on the column cat. The expected outcome of the first row of each Cat needs to be empty. As well as the first row of each new type. The expected outcome enddate previous type needs to be filled if the previous row has the same  cat and the same Type. One addition to that, it is possible that within 1 cat, the type changes but has been ended on a previous row above. So for example row 14 gives uit 27-12-2020, because the cat is still 4 and the type 4 has an enddate two rows above. 

I have tried with duplicate table add index and merge back. Did not work out great. 

Who could help me, you could also use startdate and id maybe. 

 

CatEnddateTypeExpected outcome enddate previous typeStartdatumid
1 Type 0 26-11-2019137175
131-3-2022Type 1 20-6-2020153676
1 Type 131-3-202220-6-2020153677
131-3-2022Type 1 2-12-2020165553
1 Type 2 1-4-2022227028
1 Type 2 1-4-2022227029
216-12-2027Type 3 17-12-2022241229
329-3-2020Type 3 14-1-2019114249
331-12-2020Type 329-3-202030-3-2020142010
31-5-2022Type 331-12-20201-1-2021164519
323-3-2025Type 31-5-20222-5-2022230784
427-12-2020Type 4 1-1-2019113336
426-12-2021Type 5 2-1-2019113337
427-12-2022Type 427-12-202028-12-2020167168
431-12-2021Type 6 1-1-2021176085
426-12-2026Type 526-12-202127-12-2021215129



1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @TK12345 ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want.

1. Add index column in Power Query Editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZE9DsIwDIWvgjojJf5P78FWMbKzcnviNEkxlRBSh9T53vOzs20LLNelfrfX83HJ9QCQUBNmWP2HDEyW+3XnKFG9zYhD4EWHRxGE1HTyPzH7bQuY0Istk4oInWyd5zSliJax/EWtjcK9DXg0tMFTr9pUMGBXUEuL6wh2CBLwXBow8sFXr328IKgz5zkeV2WeAklxG93fa20xygIhDvmVfOKy766lp2yFG859rRbS8LTv6YlIA6+jdeOl8Rh4O/tj8K/VcrymgZZPBUHooF8Dm+Yi50QaEo2+rkAQ8Be7vwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cat = _t, Enddate = _t, Type = _t, Startdatum = _t, id = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cat", Int64.Type}, {"Enddate", type date}, {"Type", type text}, {"Startdatum", type text}, {"id", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Cat", Order.Ascending}, {"Type", Order.Ascending}, {"id", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Cat", "Type"},  {{"Index", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Index" = Table.ExpandTableColumn(#"Grouped Rows", "Index", {"Enddate", "Startdatum", "id", "Index"}, {"Enddate", "Startdatum", "id", "Index"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Index",{{"Index", Int64.Type}, {"Startdatum", type date}, {"Enddate", type date}, {"id", Int64.Type}})
in
    #"Changed Type1"

yingyinr_1-1667788677815.png

2. Create a calculated column as below to get the expected end date

Outcome enddate previous type = 
CALCULATE (
    MAX ( 'Table'[Enddate] ),
    FILTER (
        'Table',
        'Table'[Cat] = EARLIER ( 'Table'[Cat] )
            && 'Table'[Type] = EARLIER ( 'Table'[Type] )
            && 'Table'[Index]
                = EARLIER ( 'Table'[Index] ) - 1
    )
)

yingyinr_0-1667788540114.png

Best Regards

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @TK12345 ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want.

1. Add index column in Power Query Editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZE9DsIwDIWvgjojJf5P78FWMbKzcnviNEkxlRBSh9T53vOzs20LLNelfrfX83HJ9QCQUBNmWP2HDEyW+3XnKFG9zYhD4EWHRxGE1HTyPzH7bQuY0Istk4oInWyd5zSliJax/EWtjcK9DXg0tMFTr9pUMGBXUEuL6wh2CBLwXBow8sFXr328IKgz5zkeV2WeAklxG93fa20xygIhDvmVfOKy766lp2yFG859rRbS8LTv6YlIA6+jdeOl8Rh4O/tj8K/VcrymgZZPBUHooF8Dm+Yi50QaEo2+rkAQ8Be7vwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cat = _t, Enddate = _t, Type = _t, Startdatum = _t, id = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cat", Int64.Type}, {"Enddate", type date}, {"Type", type text}, {"Startdatum", type text}, {"id", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Cat", Order.Ascending}, {"Type", Order.Ascending}, {"id", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Cat", "Type"},  {{"Index", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Index" = Table.ExpandTableColumn(#"Grouped Rows", "Index", {"Enddate", "Startdatum", "id", "Index"}, {"Enddate", "Startdatum", "id", "Index"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Index",{{"Index", Int64.Type}, {"Startdatum", type date}, {"Enddate", type date}, {"id", Int64.Type}})
in
    #"Changed Type1"

yingyinr_1-1667788677815.png

2. Create a calculated column as below to get the expected end date

Outcome enddate previous type = 
CALCULATE (
    MAX ( 'Table'[Enddate] ),
    FILTER (
        'Table',
        'Table'[Cat] = EARLIER ( 'Table'[Cat] )
            && 'Table'[Type] = EARLIER ( 'Table'[Type] )
            && 'Table'[Index]
                = EARLIER ( 'Table'[Index] ) - 1
    )
)

yingyinr_0-1667788540114.png

Best Regards

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors