Forum Discussion
End date based on next row (Index+1) for each row
- 2 years ago
Hi mthiru,
I've added 1 more row (comfirm if this is expected result or not).
If there is something wrong, we need to consider sort order. Let me know.
Result:
let fnShift = (tbl as table, col as text, shift as nullable number, optional newColName as text) as table => //v 3. parametri zadaj zaporne cislo ak chces posunut riadky hore, kladne ak dole, 4. je nepovinny (novy nazov stlpca) let a = Table.Column(tbl, col), b = if shift = 0 or shift = null then a else if shift > 0 then List.Repeat({null}, shift) & List.RemoveLastN(a, shift) else List.RemoveFirstN(a, shift * -1) & List.Repeat({null}, shift * -1), c = Table.FromColumns(Table.ToColumns(tbl) & {b}, Table.ColumnNames(tbl) & ( if newColName <> null then {newColName} else if shift = 0 then {col & "_Duplicate"} else if shift > 0 then {col & "_PrevtValue"} else {col & "_NextValue"} )) in c, Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNA1MNc1MFbSUTI2MTQ0ANKGBiASJGKqFKuDpMgSqyIzFEWG2E0yQTUJU5EJiEXIOpAiIyRFlrqGhlgVGSvFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t, MainAccountID = _t, BU = _t, PV = _t, Transaction = _t]), ChangedType = Table.TransformColumnTypes(Source,{{"StartDate", type date}}), Ad_PrevWorkingDate = Table.AddColumn(ChangedType, "Prev. Working Date", each List.Max(List.Select(List.Dates(Date.AddDays([StartDate], -1), 3, #duration(-1,0,0,0)), (x)=> Date.DayOfWeek(x, Day.Monday) < 5)), type date), GroupedRows = Table.Group(Ad_PrevWorkingDate, {"MainAccountID", "BU", "PV"}, {{"All", each [ a = fnShift(_, "Prev. Working Date", -1 , "PWD Shift"), //Prev. Working Date Shift b = fnShift(a, "StartDate", -1 , "SD Shift"), //Prev. Start Date Shift c = Table.AddColumn(b, "EndDate", (x)=> if Date.ToText(x[StartDate], [Format="yyyy-MM"]) = Date.ToText(x[SD Shift], [Format="yyyy-MM"]) then x[PWD Shift] else Date.EndOfMonth(x[StartDate]), type date), //Add EndDate d = Table.RemoveColumns(c, {"Prev. Working Date", "PWD Shift", "SD Shift"}) //Remove helper columns ][d], type table}}), CombinedAll = Table.Combine(GroupedRows[All]) in CombinedAll
Hi mthiru,
I've added 1 more row (comfirm if this is expected result or not).
If there is something wrong, we need to consider sort order. Let me know.
Result:
let
fnShift = (tbl as table, col as text, shift as nullable number, optional newColName as text) as table =>
//v 3. parametri zadaj zaporne cislo ak chces posunut riadky hore, kladne ak dole, 4. je nepovinny (novy nazov stlpca)
let
a = Table.Column(tbl, col),
b = if shift = 0 or shift = null then a else if shift > 0
then List.Repeat({null}, shift) & List.RemoveLastN(a, shift)
else List.RemoveFirstN(a, shift * -1) & List.Repeat({null}, shift * -1),
c = Table.FromColumns(Table.ToColumns(tbl) & {b}, Table.ColumnNames(tbl) &
( if newColName <> null then {newColName} else
if shift = 0 then {col & "_Duplicate"} else
if shift > 0 then {col & "_PrevtValue"}
else {col & "_NextValue"} ))
in
c,
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNA1MNc1MFbSUTI2MTQ0ANKGBiASJGKqFKuDpMgSqyIzFEWG2E0yQTUJU5EJiEXIOpAiIyRFlrqGhlgVGSvFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t, MainAccountID = _t, BU = _t, PV = _t, Transaction = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"StartDate", type date}}),
Ad_PrevWorkingDate = Table.AddColumn(ChangedType, "Prev. Working Date", each List.Max(List.Select(List.Dates(Date.AddDays([StartDate], -1), 3, #duration(-1,0,0,0)), (x)=> Date.DayOfWeek(x, Day.Monday) < 5)), type date),
GroupedRows = Table.Group(Ad_PrevWorkingDate, {"MainAccountID", "BU", "PV"}, {{"All", each
[ a = fnShift(_, "Prev. Working Date", -1 , "PWD Shift"), //Prev. Working Date Shift
b = fnShift(a, "StartDate", -1 , "SD Shift"), //Prev. Start Date Shift
c = Table.AddColumn(b, "EndDate", (x)=> if Date.ToText(x[StartDate], [Format="yyyy-MM"]) = Date.ToText(x[SD Shift], [Format="yyyy-MM"]) then x[PWD Shift] else Date.EndOfMonth(x[StartDate]), type date), //Add EndDate
d = Table.RemoveColumns(c, {"Prev. Working Date", "PWD Shift", "SD Shift"}) //Remove helper columns
][d], type table}}),
CombinedAll = Table.Combine(GroupedRows[All])
in
CombinedAll
Hi again mthiru, I've added 1 more row to sample data and updated a code (in prev. post). Let me know it this is what you need.
- mthiru2 years agoFrequent Visitor
Thank you dufoq3. I have to try it with the new code. Previously I gotten the end date as expected, with one exception where in the data the PV is null. I get a null end date. Sorry, I keep forgetting to include all possible conditions in the data. We always have a MainAccountID and BU value but sometimes the PV is blank.
- dufoq32 years ago
Community Champion
Try and let me know whether it works or we need to update the code (in such case, provide new sample data that covers the issue please).