cancel
Showing results for 
Search instead for 
Did you mean: 
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
Resident Rockstar
Resident Rockstar

@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

 


New Animated Dashboard: Sales Calendar


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


New Animated Dashboard: Sales Calendar


Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Get Ready for Power BI Dev Camp

Power BI Dev Camp - June 24th

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors