Forum Discussion

TK12345's avatar
TK12345
Icon for Resolver II rankResolver II
3 years ago
Solved

Column value based on other row and column

Hi guys,

I have a question about a calculation I need to do in the power query. See data for the expected outcome. I do have 5 columns and I need to make the column . 
Expected outcome enddate previous type

The data is all sorted how it needs to be. 

The first row of each new Cat needs to be empty. Then the second row is looking to the end date of the previous row (or more previous rows) but only if the type is the same as previous row. The 5th will not show an expected outcome date cause there is a new Type

The last important thing is the example on row 14. It shows 27-12-2020 because that type already had an endate 2 rows before with the same Cat

It is possible to use the id for a key or something. Or duplicate the table with an index merge after.... i tried it but did not turn out great. 

CatEnddateTypeExpected outcome enddate previous typeStartDateid
1 Type 0 26-11-2019137175
131-3-2022Type 1 20-6-2020153676
1 Type 1               31-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 3                 29-3-202030-3-2020142010
31-5-2022Type 3                 31-12-20201-1-2021164519
323-3-2025Type 3                 1-5-20222-5-2022230784
427-12-2020Type 4 1-1-2019113336
426-12-2021Type 5 2-1-2019113337
427-12-2022Type 4                27-12-202028-12-2020167168
431-12-2021Type 6 1-1-2021176085
426-12-2026Type 5               26-12-202127-12-2021215129


Could someone help me out?

  • Hi, TK12345 

    Please check my attached pbix for more details.

    M code:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZK9jsQgDIRfJUq9SIyNMXmP61ZbXr/tvv0B4ccczRaRsMw3MzZ5Pk+cj/PI38/n/Xv4VlB0gCOPKxdghcr5etyXGY5zi6hDXYG8i6VRRCAcNQ7msHetwMboFz4ONJgoIrz7UCvgwrAi9ZS+v3rVq1RNmqN2hjujrVOpAGpU6dN15/cbFNzcLQKFyeSZ52wDskrs5zmzHn7AcLLsaxNE9SXUkYLAZOVbVSxr9Mgc2WsKlQyl1P+Jw9inmZKZ42T6PtEZmU+7MLr7kPVZ3CktP4YipoGPPQzLuMS8l6LRJ9ljRhtzCT/8awFBef/XHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cat = _t, Enddate = _t, Type = _t, #"Expected outcome enddate previous type" = _t, StartDate = _t, id = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cat", Int64.Type}, {"Enddate", type text}, {"Type", type text}, {"Expected outcome enddate previous type", type text}, {"StartDate", type text}, {"id", Int64.Type}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Enddate", type date}, {"Expected outcome enddate previous type", type date}, {"StartDate", type date}}, "en-GB"),
        #"Sorted Rows" = Table.Sort(#"Changed Type with Locale",{{"Cat", Order.Ascending}, {"Type", Order.Ascending}, {"StartDate", Order.Ascending}, {"id", Order.Ascending}}),
        #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
        #"Grouped Rows" = Table.Group(#"Added Index", {"Cat", "Type"}, {{"Data", each _, type table [Cat=nullable number, Enddate=nullable date, Type=nullable text, Expected outcome enddate previous type=nullable date, StartDate=nullable date, id=nullable number, Index=number]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data],"Sub_index",0,1)),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Cat", "Enddate", "Type", "Expected outcome enddate previous type", "StartDate", "id", "Index", "Sub_index"}, {"Cat", "Enddate", "Type", "Expected outcome enddate previous type", "StartDate", "id", "Index", "Sub_index"}),
        #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Result", each if [Sub_index] = 0 then null else  try #"Expanded Custom"[Enddate]{ [Index]-1 } otherwise null),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Sub_index"})
    in
        #"Removed Columns"

     

    Best Regards,
    Community Support Team _ Eason
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

2 Replies

  • v-easonf-msft's avatar
    v-easonf-msft
    Icon for Community Support rankCommunity Support

    Hi, TK12345 

    Please check my attached pbix for more details.

    M code:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZK9jsQgDIRfJUq9SIyNMXmP61ZbXr/tvv0B4ccczRaRsMw3MzZ5Pk+cj/PI38/n/Xv4VlB0gCOPKxdghcr5etyXGY5zi6hDXYG8i6VRRCAcNQ7msHetwMboFz4ONJgoIrz7UCvgwrAi9ZS+v3rVq1RNmqN2hjujrVOpAGpU6dN15/cbFNzcLQKFyeSZ52wDskrs5zmzHn7AcLLsaxNE9SXUkYLAZOVbVSxr9Mgc2WsKlQyl1P+Jw9inmZKZ42T6PtEZmU+7MLr7kPVZ3CktP4YipoGPPQzLuMS8l6LRJ9ljRhtzCT/8awFBef/XHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cat = _t, Enddate = _t, Type = _t, #"Expected outcome enddate previous type" = _t, StartDate = _t, id = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cat", Int64.Type}, {"Enddate", type text}, {"Type", type text}, {"Expected outcome enddate previous type", type text}, {"StartDate", type text}, {"id", Int64.Type}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Enddate", type date}, {"Expected outcome enddate previous type", type date}, {"StartDate", type date}}, "en-GB"),
        #"Sorted Rows" = Table.Sort(#"Changed Type with Locale",{{"Cat", Order.Ascending}, {"Type", Order.Ascending}, {"StartDate", Order.Ascending}, {"id", Order.Ascending}}),
        #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
        #"Grouped Rows" = Table.Group(#"Added Index", {"Cat", "Type"}, {{"Data", each _, type table [Cat=nullable number, Enddate=nullable date, Type=nullable text, Expected outcome enddate previous type=nullable date, StartDate=nullable date, id=nullable number, Index=number]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data],"Sub_index",0,1)),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Cat", "Enddate", "Type", "Expected outcome enddate previous type", "StartDate", "id", "Index", "Sub_index"}, {"Cat", "Enddate", "Type", "Expected outcome enddate previous type", "StartDate", "id", "Index", "Sub_index"}),
        #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Result", each if [Sub_index] = 0 then null else  try #"Expanded Custom"[Enddate]{ [Index]-1 } otherwise null),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Sub_index"})
    in
        #"Removed Columns"

     

    Best Regards,
    Community Support Team _ Eason
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.