Forum Discussion
Delete Bottom N rows based on criteria/condition
- 5 years ago
Hi Anonymous
Try this, where PreviousStep is the current last step in your query
last2_ = List.LastN(PreviousStep[X], 2), res= if Text.Start(last2_{0}, 3) ="XXX" or Text.Start(last2_{1}, 3) ="XXX" then Table.RemoveLastN( PreviousStep, N) else PreviousStep in resPlease mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
Actually, it's even easier:
= Table.RemoveLastN(PriorStep, each Text.StartsWith([ColumnName], "ABCD"))
This utilizes the optional parameter countOrCondition to determine how many rows to remove.
---Nate
Anonymous - that only works if the last row is XXX. It won't remove the XXX and all subsequent rows.
If Anonymous wants to only remove the last N rows if the last row is "XXX" then this will work - similar logic as before, but XXX is in the last row in my sample data. The last 5 rows get removed if that is the case.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSBEyagsmIiIhIMCOxOCUNB8PIyAimVik2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
varXXXPosition = List.PositionOf(Source[Column1], "XXX") + 1,
varTotalRows = Table.RowCount(Source),
Custom1 = if varXXXPosition = varTotalRows then Table.RemoveLastN(Source, 5) else Source
in
Custom1
The optional condition is nice, but it seems to be only really useful on numeric data, for example:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeJYnWglIyDLCMwyBrKMwSwTIMsEzDIFskzBrIiICCDbDMxOLE5JA3LMkTkWyBxLiNFGIMMNDZRiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column2", Int64.Type}}),
Custom1 = Table.RemoveLastN(#"Changed Type", each [Column2] > 3)
in
Custom1
will remove all rows where Column2 is 4 or higher.
A text comparison seems to return true/false, and true is 1, so it removes the last row.
Unless there is something I am missing.