Forum Discussion

henriwestra's avatar
henriwestra
Frequent Visitor
7 years ago

Group by the secondlast date.

I want to compare the last status with the secondlast status.

And i want to do that in M. 

 

I manage to create a set of data that only contains the last created status. What I want now is a st in which the second-last status is visible.

Is that possible? for example in the advanced editor?

 

This is my set of data.

ID_MainID_StatusStatusDate_status
11Red1-1-2019
22Orange2-1-2019
23Green3-1-2019
24Red4-1-2019
35Orange5-1-2019
46Green6-1-2019
57Red7-1-2019
58Orange8-1-2019
59Green9-1-2019
610Red10-1-2019
711Orange11-1-2019
712Green12-1-2019

3 Replies

  • smpa01's avatar
    smpa01
    Community Champion

    henriwestra  I tried it in the following way. Please let me know if it works for you

     

    let
        Source = Web.Page(Web.Contents("https://community.powerbi.com/t5/Desktop/Group-by-the-secondlast-date/m-p/658233#M315999")),
        Data0 = Source{0}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Data0, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID_Main", Int64.Type}, {"ID_Status", Int64.Type}, {"Status", type text}, {"Date_status", type date}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ID_Main"}, {{"AD", each _, type table}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "C", each let
      Source=[AD],
      X=Table.AddIndexColumn(Table.Sort(Source,{{"Date_status", Order.Descending}}),"IX",1,1),
      Y= Table.AddColumn(X, "Custom", each Table.RowCount(X))
    in
      Y),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AD"}),
        #"Expanded C" = Table.ExpandTableColumn(#"Removed Columns", "C", {"ID_Status", "Status", "Date_status", "IX", "Custom"}, {"ID_Status", "Status", "Date_status", "IX", "Custom"}),
        #"Grouped Rows1" = Table.Group(#"Expanded C", {"ID_Main", "Custom"}, {{"AD", each _, type table}}),
        #"Added Custom1" = Table.AddColumn(#"Grouped Rows1", "Custom.1", each if [Custom]>1
    then 
        let 
           Source=[AD],
           X= Table.SelectRows(Source, each ([IX] = 2))
        in
          X
    else
        let 
           Source=[AD],
           X= Table.SelectRows(Source, each ([IX] = 1))
        in
          X),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom.1"}),
        #"Expanded Custom.1" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom.1", {"ID_Main", "ID_Status", "Status", "Date_status", "IX", "Custom"}, {"ID_Main", "ID_Status", "Status", "Date_status", "IX", "Custom"}),
        #"Added Custom2" = Table.AddColumn(#"Expanded Custom.1", " 2nd Last Logic", each "If ID_Main count =1 then pick up the same row #(cr) else pick up the 2nd row in Descending Order #(cr)based on Date_Status"),
        #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom2",{"ID_Main", "ID_Status", "Status", "Date_status", " 2nd Last Logic"})
    in
        #"Removed Other Columns1"

     

    • henriwestra's avatar
      henriwestra
      Frequent Visitor

      Hy @smpa1,

       

      I get an error when creating column C, see print screen. Can you tell me what I am doing wrong?

       

       

       

       

       

       

       

       

      • smpa01's avatar
        smpa01
        Community Champion

        henriwestraIf you compare the steps between what I authored and you replicated, there are differences. It is not an exact replica and it is difficult to point out the error from this screenshot.

         

        Can you please share the M that is giving you this error. Then I can compare.

         

        Solution_M_Steps

        Errored_M_Steps