Forum Discussion
mthiru
2 years agoFrequent Visitor
End date based on next row (Index+1) for each row
Hello community, new to power query and power bi space so like some help please. I am looking to determine the current row's end date. End date looks at the current row Start Date for example 7/3...
- 2 years ago
Hi mthiru, there are many ways. This one is not easy to understand, but it is realy fast.
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("i45WMtc31jcyMLRQitUBcwwRPAt9VI4JgmeJLGVoAOfFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Start Date" = _t]), ChangedType = Table.TransformColumnTypes(Source,{{"Start Date", type date}}, "en-US"), Ad_StartDateNextValue = fnShift(ChangedType, "Start Date", -1), Ad_EndDate = Table.AddColumn(Ad_StartDateNextValue, "End Date", each if Date.ToText([Start Date], [Format = "yyyy-MM"]) = Date.ToText([Start Date_NextValue], [Format = "yyyy-MM"]) then Date.AddDays([Start Date_NextValue], -1) else Date.EndOfMonth([Start Date]), type date), RemovedColumns = Table.RemoveColumns(Ad_EndDate,{"Start Date_NextValue"}) in RemovedColumns
dufoq3
2 years agoCommunity Champion
Hi mthiru, there are many ways. This one is not easy to understand, but it is realy fast.
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("i45WMtc31jcyMLRQitUBcwwRPAt9VI4JgmeJLGVoAOfFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Start Date" = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"Start Date", type date}}, "en-US"),
Ad_StartDateNextValue = fnShift(ChangedType, "Start Date", -1),
Ad_EndDate = Table.AddColumn(Ad_StartDateNextValue, "End Date", each if Date.ToText([Start Date], [Format = "yyyy-MM"]) = Date.ToText([Start Date_NextValue], [Format = "yyyy-MM"]) then Date.AddDays([Start Date_NextValue], -1) else Date.EndOfMonth([Start Date]), type date),
RemovedColumns = Table.RemoveColumns(Ad_EndDate,{"Start Date_NextValue"})
in
RemovedColumns- mthiru2 years agoFrequent Visitor
dufoq3 yes it is not easy to understand but it worked with the sample data. I am connecting to an SQL view so there on power query with a connection to SQL table it is getting stuck. Do you have an alternative method to achieve the same result?
- dufoq32 years agoCommunity Champion
Could you provide screenshot of that sql table which you've loaded to power query?