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
Anonymous
Not applicable

How to retrieve the category with max sum of sales using a measure to use with card visual

Basically i have some sample data that looks like this:

categorysales
product A100
product A100
product B100
product B50
product B200
product C200

 

And i need a measure that can bring  "product B" as the result in a card, because the sum of sales would be the maximum value for product B:

CategorySum of Sales
product B350
product C200
product A200

 

I've seem some other examples here in the community but none of then had a sum inside of the calculation, all where based on retrieving the maximum value directly of an existing database...

Thanks in advance!

2 ACCEPTED SOLUTIONS
mahoneypat
Employee
Employee

Here's one way to do it.  Replace "Data" with your actual table name.

 

Max Category =
VAR vSummary =
    ADDCOLUMNS (
        VALUES ( Data[category] ),
        "cSumSales",
            CALCULATE (
                SUM ( Data[sales] )
            )
    )
RETURN
    MINX (
        TOPN (
            1,
            vSummary,
            [cSumSales], DESC
        ),
        Data[category]
    )


Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

CNENFRNL
Community Champion
Community Champion

Hi, @Anonymous , you might want to try such a solution,

Max Category = 
VAR __sum = SUMMARIZECOLUMNS ( Table1[category], "Total", SUM ( Table1[sales] ) )
RETURN
    MAXX ( TOPN ( 1, __sum, [Total] ), Table1[category] )

Screenshot 2020-12-29 010200.png

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

Write these measures

Total sales = SUM(Data[sales])
Top selling category = FIRSTNONBLANK(TOPN(1,VALUES(Data[category]),[Total Sales]),1)

Drag the second measure to a card visual.

Hope this helps.

Untitled.png


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

Hi, @Anonymous , you might want to try such a solution,

Max Category = 
VAR __sum = SUMMARIZECOLUMNS ( Table1[category], "Total", SUM ( Table1[sales] ) )
RETURN
    MAXX ( TOPN ( 1, __sum, [Total] ), Table1[category] )

Screenshot 2020-12-29 010200.png

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

mahoneypat
Employee
Employee

Here's one way to do it.  Replace "Data" with your actual table name.

 

Max Category =
VAR vSummary =
    ADDCOLUMNS (
        VALUES ( Data[category] ),
        "cSumSales",
            CALCULATE (
                SUM ( Data[sales] )
            )
    )
RETURN
    MINX (
        TOPN (
            1,
            vSummary,
            [cSumSales], DESC
        ),
        Data[category]
    )


Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Thanks for the help, this solution worked! Could you give me an explanation of why MINX were used instead of the equivalent MAXX?? I didn't actually understand what happened here!

Also, how would you use this value to bring another unique value from another table that has a relationship? It happens that in my actual data, product A is a product code like PD-001, and it has a better description on a separate table.

Regards,

Leo



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.