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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
gbarr12345
Post Patron
Post Patron

Best Selling Product per market

Hi,

 

I'm trying to create a measure that shows the best selling product per market.

 

However, when I inserted my measure it's giving each market the same product.

 

gbarr12345_0-1716161480094.png

 

 

Any idea where I'm going wrong?

 

Code below and PBIX attached.

 

Best Selling product per market = FIRSTNONBLANK( TOPN( 1, VALUES( 'Dimension_Item Table'[Item Description] ),
    SUM( 'Sales Table'[Amount]) ) , 1)

 

Many Thanks in advance.

 

https://drive.google.com/file/d/1ZDfNmrZ60eMfjB1WU3cSMcOGrQqkPt7X/view?usp=drive_link

 

3 ACCEPTED SOLUTIONS
PawelWrona
Resolver I
Resolver I

What in case you have multiple items with same sales per market? You could use following formula that will concatenate such items:

Best Selling product per market v2 = 
VAR summarizedTable =                                           // Creates virtual table of sales per item
    ADDCOLUMNS(
		DISTINCT('Dimension_Item Table'[Item Description]),     // DISTINCT gets rid of blank rows
		"@Sales", CALCULATE(SUM('Sales Table'[Amount]))
	)

VAR maxSales =                                                  // Gets max sales
    MAXX(summarizedTable, [@Sales])

VAR filteredTable =                                             // Returns all the rows with max sales
    FILTER(summarizedTable, [@Sales] = maxSales)

VAR result =                                                    // Concatenate in case there are multiple items with same sales
    CONCATENATEX(
        filteredTable,
        'Dimension_Item Table'[Item Description],
        ","
    )

RETURN {result}

I think it should work as per your requirements:

PawelWrona_0-1716252537524.png

 


I included comments but let me know if you have questions. TOPN can be resource expensive with large data. This formula is quite fast.

 

View solution in original post

I am not sure I follow you Chain related question. When I use the chain from your model it works fine:

PawelWrona_0-1716268039856.png

As for the question to bring the top sales as well, you could use the same code but till "maxSales" variable, like here:

Top Sales = 
VAR summarizedTable =                                           // Creates virtual table of sales per item
    ADDCOLUMNS(
		DISTINCT('Dimension_Item Table'[Item Description]),     // DISTINCT gets rid of blank rows
		"@Sales", CALCULATE(SUM('Sales Table'[Amount]))
	)

VAR maxSales =                                                  // Gets max sales
    MAXX(summarizedTable, [@Sales])


RETURN {maxSales}

 

And here is the result:

PawelWrona_1-1716268180209.png

 

View solution in original post

Sure, here the DAX...

TopProductAmountByMarket = 
CALCULATE(
    SUM('Sales Table'[Amount]), 
    FILTER(
        'Dimension_Item Table',
        'Dimension_Item Table'[Item Description] = [TopProductByMarket]
    )
)

 

amustafa_0-1716304673250.png

 





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

Proud to be a Super User!




View solution in original post

11 REPLIES 11
PawelWrona
Resolver I
Resolver I

What in case you have multiple items with same sales per market? You could use following formula that will concatenate such items:

Best Selling product per market v2 = 
VAR summarizedTable =                                           // Creates virtual table of sales per item
    ADDCOLUMNS(
		DISTINCT('Dimension_Item Table'[Item Description]),     // DISTINCT gets rid of blank rows
		"@Sales", CALCULATE(SUM('Sales Table'[Amount]))
	)

VAR maxSales =                                                  // Gets max sales
    MAXX(summarizedTable, [@Sales])

VAR filteredTable =                                             // Returns all the rows with max sales
    FILTER(summarizedTable, [@Sales] = maxSales)

VAR result =                                                    // Concatenate in case there are multiple items with same sales
    CONCATENATEX(
        filteredTable,
        'Dimension_Item Table'[Item Description],
        ","
    )

RETURN {result}

I think it should work as per your requirements:

PawelWrona_0-1716252537524.png

 


I included comments but let me know if you have questions. TOPN can be resource expensive with large data. This formula is quite fast.

 

That worked thank you!

 

One more quick question. Is there a way to be able to drag in the correct amount instead of the overall amount as per my screenshot?

 

gbarr12345_1-1716258290369.png

 

Also i nmy data I'm trying to get the best selling item per chain. Some of the chains have no items sold so as a result it's throwing out all the products instead of being blank. Is there a way to fix this also?

 

gbarr12345_2-1716260737220.png

 

gbarr12345_3-1716260753569.png

 

gbarr12345_4-1716260759603.png

 

 

I am not sure I follow you Chain related question. When I use the chain from your model it works fine:

PawelWrona_0-1716268039856.png

As for the question to bring the top sales as well, you could use the same code but till "maxSales" variable, like here:

Top Sales = 
VAR summarizedTable =                                           // Creates virtual table of sales per item
    ADDCOLUMNS(
		DISTINCT('Dimension_Item Table'[Item Description]),     // DISTINCT gets rid of blank rows
		"@Sales", CALCULATE(SUM('Sales Table'[Amount]))
	)

VAR maxSales =                                                  // Gets max sales
    MAXX(summarizedTable, [@Sales])


RETURN {maxSales}

 

And here is the result:

PawelWrona_1-1716268180209.png

 

Thank you so much for your help, it's much appreciated!

amustafa
Super User
Super User

Here's the updated code...

CALCULATE(
    FIRSTNONBLANK('Dimension_Item Table'[Item Description], SUM('Sales Table'[Amount])),
    FILTER(
        ALLNOBLANKROW('Dimension_Item Table'),
        RANKX(
            ALLNOBLANKROW('Dimension_Item Table'),
            CALCULATE(SUM('Sales Table'[Amount])),
            ,
            DESC,
            Dense
        ) = 1
    )
)

 

amustafa_0-1716249941208.png

 





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

Proud to be a Super User!




Hi Mustafa,

 

Thank you so much for the code. That worked. 

 

Is there a way to be able to drag in the correct amount for the best selling product also?

 

gbarr12345_0-1716258097625.png

 

Sure, here the DAX...

TopProductAmountByMarket = 
CALCULATE(
    SUM('Sales Table'[Amount]), 
    FILTER(
        'Dimension_Item Table',
        'Dimension_Item Table'[Item Description] = [TopProductByMarket]
    )
)

 

amustafa_0-1716304673250.png

 





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

Proud to be a Super User!




Thank you so much, that worked!

 

Thank you!

amustafa
Super User
Super User

Try the updated DAX...

Best Selling Product per Market NEW = 
VAR TopProductByMarket = 
    TOPN(
        1, 
        VALUES('Dimension_Market Table'[Market]), 
        CALCULATE(
            SUM('Sales Table'[Amount]), 
            ALL('Dimension_Item Table')
        ), 
        DESC
    )
RETURN
    FIRSTNONBLANK(
        'Dimension_Item Table'[Item Description], 
        CALCULATE(
            SUM('Sales Table'[Amount]), 
            TopProductByMarket
        )
    )

 

Results.

amustafa_0-1716167887731.png

 





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

Proud to be a Super User!




I tried your code and still seem to be getting the incorrect best selling product.

 

gbarr12345_0-1716168649236.png

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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