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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Anonymous
Not applicable

Rand() recalculated within TOPN

The data table used is below 

may_bi_3-1646891735820.png     

I have a slicer  nValue, with values from 1 to 20.

Case 1 (working as expected):
Based on the slicer selection, the data table should show the top  products based on Index values.

Measures used:

1.  TotalSales = SUM('Table'[Amount])

2.  top_Products =  //sorts the products based on the top N Index values
         CALCULATE (
                  'Table'[TotalSales],
                  KEEPFILTERS ( TOPN ( nValue[nValue Value], ALLSELECTED ( 'Table' ), 'Table'[Index], DESC ) )
                           )
               ) 
may_bi_7-1646892750468.png 
      Fig: table sorted correctly based on top 2  Index values

 

Case 2 (Not working):
Based on the slicer selection, the data table should sort the Products based on the top N random values.
However, the results don't come as expected. 

1. Random = RAND()
2. Top_Random//should sort the products based on the top N rand() values
CALCULATE (
            ' Table'[TotalSales],
             KEEPFILTERS ( TOPN ( nValue[nValue Value], ALLSELECTED ( 'Table' ), RAND(), DESC ) )
                 )
          )
may_bi_10-1646894983229.png
 
Here, table gets sorted wrongly - Instead of showing value for top 3 Random values, it is showing 2 other rows,  ie; sorting doesn't consider the rand() values.
 
Will the value of rand() get changed for  each iteration of TOPN?
Is something happening to the rand() inside the TOPN function, that makes it impossible to sort the values?

 

1 ACCEPTED SOLUTION
v-angzheng-msft
Community Support
Community Support

Hi, @Anonymous 

 

Measure Random and Measure Top_Random perform different random number calculations. The random number generated by Measure op_Random is not the same as the random number generated by Measure Random.

 

And due to the characteristics of the TOPN function, the returned result may be more than N rows.

  • If there is a tie, in order_by values, at the N-th row of the table, then all tied rows are returned. Then, when there are ties at the N-th row the function might return more than n rows.

You can read these articles below for more information

Refer:

Filtering the Top 3 products for each category in Power BI

Filtering the top products alongside the other products in Power BI

Showing the top 5 products and Other row

 

 

Best Regards,
Community Support Team _ Zeon Zheng


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-angzheng-msft
Community Support
Community Support

Hi, @Anonymous 

 

Measure Random and Measure Top_Random perform different random number calculations. The random number generated by Measure op_Random is not the same as the random number generated by Measure Random.

 

And due to the characteristics of the TOPN function, the returned result may be more than N rows.

  • If there is a tie, in order_by values, at the N-th row of the table, then all tied rows are returned. Then, when there are ties at the N-th row the function might return more than n rows.

You can read these articles below for more information

Refer:

Filtering the Top 3 products for each category in Power BI

Filtering the top products alongside the other products in Power BI

Showing the top 5 products and Other row

 

 

Best Regards,
Community Support Team _ Zeon Zheng


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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