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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
mandar5118
Frequent Visitor

Power Query - Select certain rows

Hi, I have certain rows which I want to filter the table. So I only need the first 12 rows of every attribute (without changing the current sort order)

 

How can I filter the table? I know I can manually do that by duplicating queries and then filter the data, but that will be very time consuming as there are 50+ columns. Also I am not sure if any new Attribute might get added/removed. Thanks

 

mandar5118_0-1679390181026.png

 

2 ACCEPTED SOLUTIONS

No, there are instances where I need debit and credit both

View solution in original post

In order to retain the sort order, you will need to add an index column and then buffer the table before grouping, like this:

AddIndex = Table.AddIndexColumn ( your_table, , "Index", 1, 1, Int64.Type),
Buffered = Table.Buffer ( AddIndex ),
filtered = 
        Table.Combine(
            Table.Group(
                Buffered, 
                "Attribute", 
                {{"rows", each Table.FirstN(_, 12)}}
            )[rows]
        )

View solution in original post

9 REPLIES 9
mandar5118
Frequent Visitor

Also if there is a way I can split the queries based on the attribute, that would also help. I can then select keep top 12 rows and then append all the queries

AlienSx
Super User
Super User

Hi, @mandar5118 

filtered = 
        Table.Combine(
            Table.Group(
                your_table, 
                "Attribute", 
                {{"rows", each Table.FirstN(_, 12)}}
            )[rows]
        )

It does give me 12 rows per attribute but the original sort order is disturbed

never saw Table.Group or Table.Combine are shuffling data. Anyway, add index column before Table.Group and use this code. 

idx = Table.AddIndexColumn(your_table, "idx", 1, 1),
filtered = 
     Table.Combine(
         Table.Group(
             idx, 
             "Attribute", 
             {{"rows", each Table.FirstN( Table.Sort (_, {"idx"}), 12)}}
         )[rows]
     )

 

In order to retain the sort order, you will need to add an index column and then buffer the table before grouping, like this:

AddIndex = Table.AddIndexColumn ( your_table, , "Index", 1, 1, Int64.Type),
Buffered = Table.Buffer ( AddIndex ),
filtered = 
        Table.Combine(
            Table.Group(
                Buffered, 
                "Attribute", 
                {{"rows", each Table.FirstN(_, 12)}}
            )[rows]
        )

Hi, @jennratten it seems to me that Table.Buffer alone should work.

Hi @AlienSx , sort of.  In the past, I have seen that Table.Buffer alone will work with small tables in which the buffer does not require paging but when paging occurs the rows are reordered by buffer's internal process.  However, I am not abe to find my past notes on this or other posts, so I may not be remembering that clearly.  However, the second purpose is for the model.  Once the table is loaded to the model and placed in a visual, it is nice to be able to specify the sort and the index will enable that functionality.  Based on how the visual is actually needing to appear, there could be other types of indexing needed.

jennratten
Super User
Super User

Hello - it looks like you are trying to retain the rows where Column 1 is Aug 2022 - Jul 2023 and column 2 = credit.  If that is correct, I recommend filtering those two columns.  That should leave you with the expected result without changing the sort order.

No, there are instances where I need debit and credit both

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.