Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Remove the Last N rows of a given set of data

Hi I have this sample data, I want to remove the last 2 rows of a given set of data. For example in this case I want to remove the selected ones in red because its only allowed to have 2 records per combination

#"sample"

ID1ID2
11
12
13
14
21
22
31
32
33

 I get to these code but its incomplete. I dont know if need another approach or if is possible to iterate each row of the #"sample for distinct ID1s

 

 

#"ID1 Table" = Table.SelectColumns(#"sample",{"ID1"}),
#"Distinct ID1 Table" = Table.Distinct(#"ID1 Table"),

.... //Missing code

#"Last records" = Table.SelectRows(table,each [ID1] <> ID1Filter), //ID1Filter is a distinct ID1 from table
#"ID2 from ID1"= Table.SelectRows(table,each [ID1] = ID1Filter),
#"quantity Of ID2" = Table.RowCount(#"ID2fromID1"),
#"Last ID2 fromI D1"= Table.RemoveLastN(#"ID2 from ID1",if #"quantity Of ID2"> 2 then #"quantity Of ID2"- 2 else 0),
#"Removed"= Table.Combine({#"Last records",#"LastID2fromID1"})

 

 

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeJYHQjLCM4yhrNMwCwjuDojuDpjuJgxihhQbywA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID1 = _t, ID2 = _t]),
    Grouped = Table.Group(Source, "ID1", {"a", each if Table.RowCount(_) > 2 then Table.FirstN(_, 2) else _}),
    #"Expanded a" = Table.ExpandTableColumn(Grouped, "a", {"ID2"}, {"ID2"})
in
    #"Expanded a"

Screenshot 2021-06-17 021555.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Love the simplicity of your solution!. Thank you so much!

CNENFRNL
Community Champion
Community Champion

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeJYHQjLCM4yhrNMwCwjuDojuDpjuJgxihhQbywA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID1 = _t, ID2 = _t]),
    Grouped = Table.Group(Source, "ID1", {"a", each if Table.RowCount(_) > 2 then Table.FirstN(_, 2) else _}),
    #"Expanded a" = Table.ExpandTableColumn(Grouped, "a", {"ID2"}, {"ID2"})
in
    #"Expanded a"

Screenshot 2021-06-17 021555.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors