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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Filter Top N values in query with number column.

Hi,

 

I wanna have only top N values with column 'Period', not top N rows cuz some rows have the same value.

 

jeongkim_0-1744101178244.png

 

2 ACCEPTED SOLUTIONS
timalbers
Skilled Sharer
Skilled Sharer

Hi @Anonymous 

in Power Query add something like this to your query:

let
    Source = YourTable,
    Distinct = Table.Distinct(Table.SelectColumns(Source, {"Period"})),
    Sorted = Table.Sort(Distinct, {{"Period", Order.Descending}}),
    TopN = Table.FirstN(Sorted, N),
    Merged = Table.NestedJoin(Source, {"Period"}, TopN, {"Period"}, "Merge", JoinKind.Inner),
    Result = Table.ExpandTableColumn(Merged, "Merge", {"Period"})
in
    Result

you'll need to replace "N" with the actual amount of values you want to keep and "YourTable".

In DAX (e.g. for a new calculated table) you could do this:

Top_N = 
VAR Top_Periods = TOPN( N, VALUES( YourTable[Period] ), YourTable[Period], DESC )
RETURN FILTER( YourTable, YourTable[Period] IN Top_Periods )

same here, replace N and YourTable accordingly.

 


—————————————————————————————
✔️ If my answer helped you, please consider marking it as a solution.

View solution in original post

danextian
Super User
Super User

Hi @Anonymous 

 

In the Query Editor, remove duplicates from the Period column, sort it in descending order, and use Table.Buffer to store the sorting in memory. Then, from the Home tab, select Keep Rows and enter the number of rows to retain.

This will return only one row per unique period. If you need to preserve all related rows, duplicate the query and merge it with the original using an Inner Join. Note: This additional transformation may impact refresh performance due to the increased processing steps. Alternatively, consider using RANKX in DAX.

danextian_0-1744102329672.png

danextian_1-1744102361679.png

Please see the attached pbix.

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

3 REPLIES 3
danextian
Super User
Super User

Hi @Anonymous 

 

In the Query Editor, remove duplicates from the Period column, sort it in descending order, and use Table.Buffer to store the sorting in memory. Then, from the Home tab, select Keep Rows and enter the number of rows to retain.

This will return only one row per unique period. If you need to preserve all related rows, duplicate the query and merge it with the original using an Inner Join. Note: This additional transformation may impact refresh performance due to the increased processing steps. Alternatively, consider using RANKX in DAX.

danextian_0-1744102329672.png

danextian_1-1744102361679.png

Please see the attached pbix.

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

Thanks but can we filter out without duplicating data table or dax?

Main purpose of this is to reduce modeling size and loading time by taking some unnecessary data out before move on to dax data. 

 

timalbers
Skilled Sharer
Skilled Sharer

Hi @Anonymous 

in Power Query add something like this to your query:

let
    Source = YourTable,
    Distinct = Table.Distinct(Table.SelectColumns(Source, {"Period"})),
    Sorted = Table.Sort(Distinct, {{"Period", Order.Descending}}),
    TopN = Table.FirstN(Sorted, N),
    Merged = Table.NestedJoin(Source, {"Period"}, TopN, {"Period"}, "Merge", JoinKind.Inner),
    Result = Table.ExpandTableColumn(Merged, "Merge", {"Period"})
in
    Result

you'll need to replace "N" with the actual amount of values you want to keep and "YourTable".

In DAX (e.g. for a new calculated table) you could do this:

Top_N = 
VAR Top_Periods = TOPN( N, VALUES( YourTable[Period] ), YourTable[Period], DESC )
RETURN FILTER( YourTable, YourTable[Period] IN Top_Periods )

same here, replace N and YourTable accordingly.

 


—————————————————————————————
✔️ If my answer helped you, please consider marking it as a solution.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.