Forum Discussion
henriwestra
7 years agoFrequent 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...
smpa01
7 years agoCommunity 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
7 years agoFrequent Visitor
Hy @smpa1,
I get an error when creating column C, see print screen. Can you tell me what I am doing wrong?
- smpa017 years agoCommunity 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