Forum Discussion

umekuro's avatar
umekuro
Helper II
4 years ago
Solved

How can I get the second row's data

Dear all,   I want to create the table on the right from the table on the left below.   "Max Date and second-max content (not null) in Name with status Delete."   I created an All Rows co...
  • ronrsnfld's avatar
    ronrsnfld
    4 years ago

    Ah, the old moving target question.

     

    If you are going to return all of the status's, then you would use a different algorithm.

     

    Merely

    • Group by name
    • Extract the Max Date from each sub-table for Date
    • Sort each sub-table by date descending
      • extract the first Status for Status
      • If Status=Delete then extract the second line for Content
      • else extract the first line for content

     

     

    let
    
    //read in the data and set data types
    //be sure to change table name in next line to actual table name
        Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{
            {"Name", type text}, {"Status", type text}, {"Date", type date}, {"Content", type text}}),
    
    //Group by name
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {
    
        //extract status for max date
        //if status "Delete" return content for next line
        //   else return status for the same line
        {"Status", each Table.Sort(_,{"Date",Order.Descending})[Status]{0}, type text},
        {"Date", each List.Max([Date]), type date},
        {"Content", (t)=>
            let 
                sorted=Table.Sort(t,{"Date",Order.Descending})
            in 
                if sorted[Status]{0}="Delete" then sorted[Content]{1} else sorted[Content]{0},type text}
        })
    in #"Grouped Rows"