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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
sqlguru448
Helper III
Helper III

Ranking measure with Table variable

Hello Folks, 

 

I'm trying to create measure for Top 10 customers based on sales amount in Power BI which is connected Live to the Tabular model.

 

Customer is Dimension and Sales amount is in fact table and I need to Rank the customers from 1 to 10 based on highest sales amount for each customer.

 

I have tried to create this DAX query below but this gives me Rank '1' for all the rows, I am guessing this has to do with filter context.

Measure =
var _top10 =
TOPN(10, FILTER(SUMMARIZE(all('Fact'), Customer[Name],"Total_Sales", SUM('Fact'[Sales Amount])), [Sales Amount] >0 ), [Sales Amount])
 
RETURN
COUNTROWS(FILTER(_top10, [Total_Sales] >= EARLIER([Total_Sales]))
)
I have also tried using RANKX but it gives performance issues.
 
Any help or guidance is really appreciated.
Thanks

 

1 ACCEPTED SOLUTION

Hi @sqlguru448 ,

 

Be aware that making a summarizaition of a full table will bring for sure performance issues, in your case you are using the FACT table that I assume have a huge number of data so be aware of that.

 

Try the following code:

 

Measure = 
var temp_table =
        FILTER (
            SUMMARIZE (
                ALLSELECTED ( Customer[Name] );
                Customer[Name];
                "Total_Sales"; [Sales Amount]
            );
            [Sales Amount] > 0
        )

var Ranking = RANKX(temp_table; [Sales Amount])


RETURN
    Ranking

 

 

Then filter out all rankings above 10.


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

11 REPLIES 11
vivran22
Community Champion
Community Champion

Hello @sqlguru448 ,

 

What is the expected result? Can you share a sample data/pbix file  along with the expected results?

 

Cheers!
Vivek

Blog: vivran.in/my-blog
Connect on LinkedIn
Follow on Twitter

Hi @vivran22 

 

Below is the sample data screenshot. The ranking should be from 1 to 10 based on highest sales amount. but my above dax gives me Rank 1 in all rows.

Capture.PNG

Hi,

Try this measure

=rankx(all(data[Customer]),[Sales])

I have assumed that sales is an explicit measure.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur 

This has performance issue when other dimesions are added to the visual, that is why I am trying to get top 10 first within variable table and then add other dimensions to it. Thank you.

HI  @sqlguru448 

Try to use this formula to create a measure

Measure 2 = 
VAR  _RANK= RANKX (
        ALLSELECTED ( Customer[Name] ),
        CALCULATE ( SUM ( 'Fact'[Sales Amount] ) ))
RETURN
IF ( _RANK <= 10,  _RANK)

 

if not your case, please share some simple sample data and your expected output.

 

Regards,

Lin

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

@sqlguru448 ,

 

Just came out today check this videos about rankx and countrows.


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi

@v-lili6-msft   The above DAX gives performance issues as posted in my earlier comments as my real data is in millions, I am adding other dimension columns to the table which is causing the issue. Below is the expected data result for rank, apart from this I have to add other columns from different dimensions.

 

  Capture.PNG

I am trying to fix it by writing below DAX, this works fine in SSAS dax query but not in Power BI desktop.

The power bi desktop gives rank as all 1's

Measure =
var _top10 =
TOPN(10, FILTER(SUMMARIZE(all('Fact'), Customer[Name],"Total_Sales", SUM('Fact'[Sales Amount])), [Sales Amount] >0 ), [Sales Amount])
 
RETURN
COUNTROWS(FILTER(_top10, [Total_Sales] >= EARLIER([Total_Sales]))
)
 
Thanks

Hi @sqlguru448 ,

 

Be aware that making a summarizaition of a full table will bring for sure performance issues, in your case you are using the FACT table that I assume have a huge number of data so be aware of that.

 

Try the following code:

 

Measure = 
var temp_table =
        FILTER (
            SUMMARIZE (
                ALLSELECTED ( Customer[Name] );
                Customer[Name];
                "Total_Sales"; [Sales Amount]
            );
            [Sales Amount] > 0
        )

var Ranking = RANKX(temp_table; [Sales Amount])


RETURN
    Ranking

 

 

Then filter out all rankings above 10.


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Thank you this gave me an idea to modify my dax.

 

Measure = var _top10 =
FILTER(SUMMARIZE(ALLSELECTED('Fact Sales'), Product[Product_Name], Product_Subcategory[Product_subcategory_name,  location[state] , "Sales", [Total Sales]), [Total Sales] > 0 )
RETURN RANKX(_top10,[Total Sales])
 
Thank you for your help @MFelix 

Hi @sqlguru448 ,

 

Glad I could point out in the correct direction, I made the measure with only a few lines of data so it could work incorrectly on a bigger scale.

 

Always remenber that FILTERING a full table will have a very big impact on performance, you should always filter on the minimum amount of columns needed for your calculations and your context needs.


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Any thoughts from the DAX Experts please?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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