cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Nikhilpowerbi New Member
New Member

Dynamic topn()

Is there a way in power bi to display top n rows in a table based on varying criterea such as sales amount, expected sales, actual sales, etc.
1 ACCEPTED SOLUTION

Accepted Solutions
CheenuSing Super Contributor
Super Contributor

Re: Dynamic topn()

Hi @Nikhilpowerbi

 

Try the following

 

1. Create table called as Measure with column names - MeasureId, MeasureName

    Eg :  Row 1 -  1, Quantity

            Row 2 - 1, Value

   Create as many rows as you want depending on the number of measures you wish to alternate.

 

2.  Create table called TopTable as follows :

         TopTable =
                           Union (
                                  TOPN (10, SUMMARIZE('Product',[Item],"Qty",sum('Product'[StockAvailable]),"Value",0),[Qty],DESC ),
                                  TOPN (10, SUMMARIZE('Product',[Item],"Qty",0,"Value",sum('Product'[StockValue])),[Value],DESC )
                             )

           This is creating  a table with columns Item, Sum(StockQty), sum(StockValue)  from the Product table. Replace your fact table names accordingly.

 

3.  Create measure ShowMeasure as

     ShowMeasure = switch (TRUE,
              Min ( 'Measure'[MeasureID] ) = 1,
                                       SUM(TopTable[Qty] ),
                            Min ( 'Measure'[MeasureID] ) = 2,
                          SUM ( TopTable[Value] ))

 

4. Create a Filter using the MeasureName at Step 1.

5.  Create a report using Item from TopTable and ShowMeasure and in the filters for the report for ShowMeasure set not equal to zero.

6. This will work.

 

 

Capture.GIF

 

Output when select Value

                            Capture.GIF 

 

If this works for you please accept it as solution and also give KUDOS.

 

Cheers

CheenuSIng

 

 

 

 

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!

View solution in original post

2 REPLIES 2
CheenuSing Super Contributor
Super Contributor

Re: Dynamic topn()

Hi @Nikhilpowerbi

 

Try the following

 

1. Create table called as Measure with column names - MeasureId, MeasureName

    Eg :  Row 1 -  1, Quantity

            Row 2 - 1, Value

   Create as many rows as you want depending on the number of measures you wish to alternate.

 

2.  Create table called TopTable as follows :

         TopTable =
                           Union (
                                  TOPN (10, SUMMARIZE('Product',[Item],"Qty",sum('Product'[StockAvailable]),"Value",0),[Qty],DESC ),
                                  TOPN (10, SUMMARIZE('Product',[Item],"Qty",0,"Value",sum('Product'[StockValue])),[Value],DESC )
                             )

           This is creating  a table with columns Item, Sum(StockQty), sum(StockValue)  from the Product table. Replace your fact table names accordingly.

 

3.  Create measure ShowMeasure as

     ShowMeasure = switch (TRUE,
              Min ( 'Measure'[MeasureID] ) = 1,
                                       SUM(TopTable[Qty] ),
                            Min ( 'Measure'[MeasureID] ) = 2,
                          SUM ( TopTable[Value] ))

 

4. Create a Filter using the MeasureName at Step 1.

5.  Create a report using Item from TopTable and ShowMeasure and in the filters for the report for ShowMeasure set not equal to zero.

6. This will work.

 

 

Capture.GIF

 

Output when select Value

                            Capture.GIF 

 

If this works for you please accept it as solution and also give KUDOS.

 

Cheers

CheenuSIng

 

 

 

 

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!

View solution in original post

mdaamirkhan Member
Member

Re: Dynamic topn()

can you please share the pbix file

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Users Online
Currently online: 206 members 2,171 guests
Please welcome our newest community members: