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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Anonymous
Not applicable

PowerQuery Filter Out Data from Last Two Dates

I have a dataset which looks like this:

FileDateItemPDQE#E TypeAvailable
12/2/2021A5108ShelfY
13/4/2021C323ShelfY
24/3/2021D24ShelfY
24/3/2021E567BulkY
35/2/2021G74SemiN
34/2/2021B611BulkY

 

The date of any file is the minimum date minus 7 days. I only want to add a table in power query which only queries data from the latest 2 files, which in this case will be files 2 and 3. This is just a sample I have a lot of files that keep adding s I need this to be dynamic. Please guide me how to do this in power query directly (instead of through filters)

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

You can sort by your date column and then just keep the top two rows.  See example below.  To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLSB0IDIxDTEYhNgdjQwAJIBmek5qQB6UilWB2IUmN9E5hSZxAXpNsYQ6URkGWibwxT6QJSBRIipNAVaruZOZBwKs3JhisE2WGKcKY7EJvDTEzNzQRSfnB1Jgh1TiDDQN4xRDEwFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [File = _t, Date = _t, Item = _t, PDQ = _t, #"E#" = _t, #"E Type" = _t, Available = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
#"Sorted Rows" = Table.Buffer(Table.Sort(#"Changed Type",{{"Date", Order.Descending}})),
#"Kept First Rows" = Table.FirstN(#"Sorted Rows",2)
in
#"Kept First Rows"

 

Note that I wrapped the Table.Sort with Table.Buffer to make sure the sort order is maintained.

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

1 REPLY 1
mahoneypat
Microsoft Employee
Microsoft Employee

You can sort by your date column and then just keep the top two rows.  See example below.  To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLSB0IDIxDTEYhNgdjQwAJIBmek5qQB6UilWB2IUmN9E5hSZxAXpNsYQ6URkGWibwxT6QJSBRIipNAVaruZOZBwKs3JhisE2WGKcKY7EJvDTEzNzQRSfnB1Jgh1TiDDQN4xRDEwFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [File = _t, Date = _t, Item = _t, PDQ = _t, #"E#" = _t, #"E Type" = _t, Available = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
#"Sorted Rows" = Table.Buffer(Table.Sort(#"Changed Type",{{"Date", Order.Descending}})),
#"Kept First Rows" = Table.FirstN(#"Sorted Rows",2)
in
#"Kept First Rows"

 

Note that I wrapped the Table.Sort with Table.Buffer to make sure the sort order is maintained.

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.