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
henriwestra
Frequent Visitor

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 3
smpa01
Super User
Super User

@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"

Capture.JPG

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hy @smpa1,

 

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

 

 

Printscreen powerbi.PNG

 

 

 

 

 

 

@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_StepsSolution_M_Steps

Errored_M_StepsErrored_M_Steps

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

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