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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Create calculated column (top 10 / others) based on rank by Sales

Hi guys,

 

This thing drives me crazy so I thought maybe I could get some help here.

I have 2 tables : 1 fact with the sales and 1 dimension with the products (1 to * relation)

In the fact table I have different types of sales, so the measure to get the sales I need is :

Sales = CALCULATE(SUM('Table'[Amount]),'Table'[TypeOfSales] = "This One")
 
The Rank measure is giving me the expected result : 
Rank Product by Sales = RANKX(ALL(Product), _Measures[Sales],,DESC)
 
I created a calculated column as is :
Top 10 / Others =
VAR RankProd = RANKX(ALL(SKU), _Measures[Sales],,DESC)
return
IF(RankProd <= 10, "Top 10", "Others")
And this is what I get. As you can see, the measure gives me the right result but for some reasons, not the calculated column.
 
 PBI_Res.JPG
 
The goal of having a calculated column is to be able to group the products dynamically in a matrix and being able to expend it if needed.
 
Really appreciate your help here.

 

2 ACCEPTED SOLUTIONS
PaulDBrown
Community Champion
Community Champion

A calculated column is not dynamic: it is calculated when the model is loaded. You need to use measures for this if you want it to be dynamic





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

I'm not too sure what the problem is. See if any of this helps:

Sample data:

sample.jpgmodel.jpg

Rank by product measure:

Rank Product =
RANKX ( ALL ( 'Product Table'[Product] ), [Sum Sales],, DESC, SKIP )

Sales for Top 3 products:

Sales top 3 = 
CALCULATE([Sum Sales], FILTER('Product Table', [Rank Product] <4))

List top 3 products:

Top 3 products by market = 
COUNTROWS(
    CALCULATETABLE(
        VALUES('Product Table'[Product]), 
        FILTER('Product Table', [Rank Product] <4)))

The CALCULATETABLE expression above will return the top 3 products

Sales top 3 = 
CALCULATE([Sum Sales], FILTER('Product Table', [Rank Product] <4))

within the filter context. So you can use the expression within measures. For example, you can also calculate the top 3 products by sales using:

Alternative top 3 sales = 
CALCULATE([Sum Sales], 
    CALCULATETABLE(
        VALUES('Product Table'[Product]), 
        FILTER('Product Table', [Rank Product] <4)))

So you can use the CALCULATETABLE expression for example for the forecast measure. You can also use the 

FILTER('Product Table', [Rank Product] <4)

 

result.jpg

 

Sample PBIX attached





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

12 REPLIES 12
Anonymous
Not applicable

Thanks Paul. I guess I'll have to find an other way to do it 🙂

 

Can you share some actual data? (Non-confidential)





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Like a PBI file ? It might be complicated.

 

But technically, I am trying to calculate a forecast accuracy for the top 10 products. Obviously this top 10 is different by market, so needs to be dynamic. It seems to be something pretty straightforward, but i can't figure something out.

 

I am able to create a calculated measure to get the correct top 10 by market but then I can't figure out a way to isolate those products to calculate my forecast accuracy.

I'm not too sure what the problem is. See if any of this helps:

Sample data:

sample.jpgmodel.jpg

Rank by product measure:

Rank Product =
RANKX ( ALL ( 'Product Table'[Product] ), [Sum Sales],, DESC, SKIP )

Sales for Top 3 products:

Sales top 3 = 
CALCULATE([Sum Sales], FILTER('Product Table', [Rank Product] <4))

List top 3 products:

Top 3 products by market = 
COUNTROWS(
    CALCULATETABLE(
        VALUES('Product Table'[Product]), 
        FILTER('Product Table', [Rank Product] <4)))

The CALCULATETABLE expression above will return the top 3 products

Sales top 3 = 
CALCULATE([Sum Sales], FILTER('Product Table', [Rank Product] <4))

within the filter context. So you can use the expression within measures. For example, you can also calculate the top 3 products by sales using:

Alternative top 3 sales = 
CALCULATE([Sum Sales], 
    CALCULATETABLE(
        VALUES('Product Table'[Product]), 
        FILTER('Product Table', [Rank Product] <4)))

So you can use the CALCULATETABLE expression for example for the forecast measure. You can also use the 

FILTER('Product Table', [Rank Product] <4)

 

result.jpg

 

Sample PBIX attached





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

OK, now I am even more confused 😐

 

When I do your calculation, it only works if I have only one column in the Product table.

If I have 2+, the top 5 doesn't work anymore. 

 

Only ID column in the product table

ID.JPG

ID and SKU in the Product table

Name.JPG

 

I wish I could share my PBIX but I can't find the option to do that ☹️

 

To share a PBIX (only if the information is not confidential; if it's confidential, please edit the condifential data) you can share a link to the file from a cloud service such as OneDrive, GoogleDrive, iCloud, Dropbox, Wetransfer...





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

The link to the file doen't seem to work





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Can you try again ?

Edit : Should work now 🙂

Thanks for the file. 

Seeing as you have more than one column in the SKU table, you need to specify the column you are filtering in the measures. So:

Sales top 5 ID =
CALCULATE (
    Sales[Sell Out Amount],
    FILTER ( VALUES ( SKU[Item_ID] ), [Rank ID] < 5 )
)

and

Sales top 5 SKU =
CALCULATE (
    Sales[Sell Out Amount],
    FILTER ( VALUES ( SKU[SKU] ), [Rank SKU] < 5 )
)

 

new.jpg

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Thank you SO much Paul for your help. I was able to do everything I needed 👍

 

 

PaulDBrown
Community Champion
Community Champion

A calculated column is not dynamic: it is calculated when the model is loaded. You need to use measures for this if you want it to be dynamic





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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