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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
G-san
New Member

PQ function to filter first 4 rows of each Date

Hi everyone!

I'm new in PowerBI, so maybe this could be a nonsense question. I'm building a new dashboard using a database with repeated values on the same reported day. I aim to filter the 4 first rows each time the variable DATE changes. There is any way to do this? I tried so many times but I failed. 


This is an example of the data, and FILTER is 1 to identify the desired output.

VAR1DATEValueFilter
Apple01-01-200111
Grape01-01-200121
Wine01-01-200111
Banana01-01-200111
Apple01-01-2001100
Grape01-01-2001150
Wine01-01-2001100
Banana01-01-2001180
Apple02-01-200151
Grape02-01-200151
Wine02-01-200161
Banana02-01-200141
Apple18-01-200121
Grape18-01-200121
Wine18-01-200121
Banana18-01-200121
Apple18-01-2001840
Grape18-01-2001840
Wine18-01-2001860
Banana18-01-2001880

 

Thanks in advance

2 REPLIES 2
AlienSx
Super User
Super User

Hello, @G-san 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZCxCoAgEIbfxdlAJcW1lh6hQRwcGoIQ6f2HjOoy7i7U4fzu+Lg/BDGUsi1CCqW7eo1SuhbXizKIaU8FYQN4XvPP8JhyPTyn3er84eXaAift7zij99AAftNwi3en8WNvqSN2b3mPdteeDPZxM/h2MxTcDCfdvke5c5yye4dz/zbcuccD", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [VAR1 = _t, DATE = _t, Value = _t, Filter = _t]),
    groups = Table.Group(Source, {"DATE"}, {{"all", each Table.FirstN(_, 4)}})
in
    Table.Combine(groups[all])

 

 

edhans
Super User
Super User

Try this. I generated the "New Filter" column.

edhans_0-1679532151221.png

Here is my code. My code removes the unnecessary columns and formats the New FIlter to be an integer, but that isn't shown in the above screencap. Wanted you to see the intermediate steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZBNC8MgDIb/S84dqLTFq2Mf9zG6g/TgoYeCFCnb/5+yNnMkGSoBn4SHvN7D4G4aGji5+zmXIcTXlOtljs9phbHx4FKK5UvpQ75GqdL+eQVf15AINogf8/Jn+BiWfGTOu1X5keW6Q87av+OC3mID+k3FO7o7j3d7TXtm95q3ZHdt2WB3t4A3t0DRLXDWbVuSu8Q5u+1p7r8NW+7jGw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Promoted Headers", {{"DATE", type date}}, "en-BM"),
    #"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"Value", Int64.Type}, {"Filter", Int64.Type}}),
    #"Grouped Rows" = 
        Table.Group(
            #"Changed Type", 
            {"DATE"}, 
            {
                {
                    "All Rows", 
                    each Table.AddIndexColumn(_, "Index", 1, 1), 
                        type table [VAR1=nullable text, DATE=nullable date, Value=nullable number, Filter=nullable number, Index = Int64.Type]
                }
            }
        ),
    #"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"VAR1", "Value", "Filter", "Index"}, {"VAR1", "Value", "Filter", "Index"}),
    #"Added New Filter" = Table.AddColumn(#"Expanded All Rows", "New Filter", each if [Index] <= 4 then 1 else 0),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added New Filter",{{"New Filter", Int64.Type}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type1",{"DATE", "VAR1", "Value", "New Filter"})
in
    #"Removed Other Columns"

The first thing I did is grouped by Date and stuck all other columns in ALL ROWS.

edhans_1-1679532258733.png

Then I edited manually the Group By step to add an Index Column in the grouping, so once grouped you see this:

edhans_2-1679532328074.png

 

Then I expanded the All Rows step, excluding the DATE field as that was already there. Finally I added a New Filter column that looked at the index. If it it was <=4, it returns 1, otherwise 0.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors