DAX pattern: Customer Sales for top companywide sellers

Hello! I’m a new member, and new to Power BI. Here’s my first complex DAX formula.

My question: Is there a better way? I’d like it to be more “consumable” across other visualizations. I’d like it to honor contexts from SLICERS but ignore all context from visuals. Is there a way?

Description:
As an exercise, I wanted to report customer sales for the top 3 products across the company. I’m using the Enterprise DNA sample database from the “Mastering DAX” tutorials. So the 3 products I want are PRODUCT 1, PRODUCT 2, and PRODUCT 7.

I really struggled finding a way to make it ignore unwanted contexts and came up with CALCULATETABLE. Am I over complicating this?

Suggestions?

I’m thinking a virtual table in a VAR would be more efficient because it would only be rendered once. Am I missing something? It seems like VARs are partly collaborative (with stuff below the RETURN), and partly not.

The weakness is if I modify the visualization by adding other groupings (Lookup table columns) to the visualization like City, or decide to use it for a different visualization not sorted by Customer or Product, or in Measure Branching where it might get used anywhere, it may fall apart due to inherited contexts, and fail to use the desired 3 best sellers (if that’s what the user specification required).

Would “ALLEXCEPT” accomplish this?

I realize not all designs would want to ignore other contexts, but for the sake of this exercise, let’s just assume that’s how my user wants it. For this screen, they’re tracking who’s consuming the top x products their factory produces.

Measure definition:
Products Sales for Top 3 Products =
// Returns totals for the same 3 products across all customers.
VAR ProductContext = values(Products[Product Name])
VAR TopProducts = // Table containing top 3 products across all customers
CALCULATETABLE(
TOPN( 3, ALL( Products), [Total Sales] ),
REMOVEFILTERS( Customer ))
RETURN
CALCULATE( [Total Sales],
TopProducts, // Only add sales for the top products
ProductContext) // Return Products context for Visualizations

Note: Here is a similar measure (closely mirroring one Sam presented on a video I Googled) that doesn’t remove the Customer context, so it would be the top 3 for each customer (or region, or city, etc).

Sales for Top 3 Product by Customer =
var ProductContext = Values( Products[Product Name] )
RETURN
CALCULATE( [Total Sales],
TOPN( 3, ALL( Products), [Total Sales] ), // Only add sales for the top products
ProductContext) // Return product context

Also note: Here’s another variation to grab those exceeding a physical threshold. I could see replacing “9000000” with a slider value…

Products over $9M only =
var ProductContext = Values( Products[Product Name] )
var TopProducts = // Table of products with total sales > $9M for date range across all customers
CALCULATETABLE(
filter(
ALLSELECTED( Products ),
[Total Sales] > 9000000 ), // The test DB has 3 products in 2020 over the arbitraty threshold of $9M.
REMOVEFILTERS( Customer ))
RETURN
CALCULATE( [Total Sales],
TopProducts, // Restrict to top products defined above
ProductContext ) // Return Product context or customer’s total will show on every line.

Side note: I don’t see how to upload the PBI file or code snippets so they retain their formating. Is there a video on this?

Hi @Bradley, I noticed you didn’t provide a PBIX file. Providing one will help users and experts find a solution to your inquiry faster and better.

Here are some videos that may help you masking sensitive data and create datasets and data models representative of your problem:

Check out this thread on Tools and Techniques for Providing PBIX Files with Your Forum Questions

Not completing your data may sometimes cause delay in getting an answer.

For the above requirement Allselected is the right contender for the above requirement .

Thanks,
Anurag

Thanks! One more small step towards understanding how Power BI has implemented “context”. It’s not as straightforward as SQL’s WHERE clause.

Sam covered several TOPN and RANKX patterns towards the end of his “Mastering DAX” lectures that also address my concerns.

I’m finding with DAX, there are a ton of nuances that apparently only trial-n-error will clarify; like when to include the Table vs. Table[Column] in a calculate filter. Every one a gotcha until understood and resolved. Thanks for helping with this one!

It’s great to know that you are making progress with your query @Bradley.

Please don’t forget if your question has been answered within the forum it is important to mark your thread as ‘solved’.

We request you to kindly take time to answer the Enterprise DNA Forum User Experience Survey, We hope you’ll give your insights on how we can further improve the Support forum. Thanks!

Hello @Bradley, just following up if you still need help with your inquiry?

We’ve noticed that no response has been received from you since a few days ago. In case there won’t be any activity on it in the next few days, we’ll be tagging this post as Solved.

Hi @Bradley, due to inactivity, a response on this post has been tagged as “Solution”. If you have any concern related to this topic, you can create a new thread.

Done. I’m still finding my way around. Thanks Anurag.