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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Compare aggregated values and select max

I need help to summarize the data for a category per user and count the results filtered by max value for each user

 

For ex: In the below image, user - "SL-08" has an entry for category - H and 2 entries for category - F. So, in a give period, such as a week of 5/24, this user should be counted towards category - F (as shown in summarized data). If there's a tie, Category F takes precedence.

 

I need help with dax measure, there are other related tables and filters i need to apply using them to the data.

 
 

Capture.JPG

 

 

IdUserCategory IdWkDateCategory Name
165SL-0815/24/20205/26/2020 0:00H
165SL-0825/24/20205/28/2020 0:00F
165SL-0825/24/20205/29/2020 0:00F
165SL-0925/24/20205/27/2020 0:00F
164SL-AE0115/24/20205/26/2020 0:00H
164SL-AE0125/24/20205/27/2020 0:00F
164SL-AE0215/24/20205/26/2020 0:00H
164SL-AE0615/24/20205/26/2020 0:00H
164SL-AE0615/24/20205/27/2020 0:00H
164SL-AE0625/24/20205/26/2020 0:00F
165SL-AE0825/24/20205/27/2020 0:00F
165SL-AE0825/24/20205/26/2020 0:00F
165SL-AE0825/24/20205/27/2020 0:00F

 

Thank You!!

7 REPLIES 7
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

Based on your description, F has a priority, so you can first create a calculated column like this:

rank =
VAR count_catefory =
    CALCULATE (
        COUNT ( 'Table (2)'[Category Id] ),
        ALLEXCEPT (
            'Table (2)',
            'Table (2)'[User],
            'Table (2)'[Wk],
            'Table (2)'[Category Name]
        )
    )
VAR precedence =
    IF ( 'Table (2)'[Category Name] = "F", count_catefory + 0.5, count_catefory )
RETURN
    precedence

Then create a calculated column to get the category name of each user.

category_test =
VAR max_ =
    CALCULATE (
        MAX ( 'Table (2)'[rank] ),
        ALLEXCEPT ( 'Table (2)', 'Table (2)'[User] )
    )
RETURN
    CALCULATE (
        FIRSTNONBLANK ( 'Table (2)'[Category Name], 1 ),
        FILTER ( 'Table (2)', 'Table (2)'[rank] = max_ )
    )

test_Compare aggregated values and select max.PNG

The third graph cannot be achieved in power Bi for the time being

Sample .pbix

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @V-lianl-msft. Thank you! Area graph shown in the final result is what i really need and i would like to use measures to make it dynamic because it needs to return data for period selected month/week etc, actual data set is over 8M+ rows 

Also, I've added the "week" and "category Name" columns for ease. They both have different tables

amitchandak
Super User
Super User

Greg_Deckler
Community Champion
Community Champion

@Anonymous - This looks like a use case for Lookup Min/Max - https://community.powerbi.com/t5/Quick-Measures-Gallery/Lookup-Min-Max/m-p/985814#M434

 

Although simpler in your case, get the MAX of Date, use that to lookup the Category Name corresponding to that Date.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler . Thanks, I've tried a similar approach before and it doesn't seem to get the output i need, especially for users that have multiple categories in the select period. it selects both the categories for the user in this case.

VAR __summarytable =
 //   ADDCOLUMNS (
        SUMMARIZE ( Test2
                        , Test2[User]
                        , Test2[Category Id] 
                        , "MaxxCount",CALCULATE ( COUNTROWS(Test2))
                        , "TWCount",CALCULATE ( COUNTROWS(Test2), FILTER(Test2,Test2[Category Id] = 1))
                        , "AFCount",CALCULATE ( COUNTROWS(Test2), FILTER(Test2,Test2[Category Id] = 2))
                        , "VisitCount",CALCULATE ( COUNTROWS(Test2), FILTER(Test2,Test2[Category Id] = 4))
        )
    
    VAR __Max = MAXX(__summarytable,[MaxxCount])


RETURN
    COUNTROWS(FILTER(__summarytable,[MaxxCount] = __Max || [AFCount] >= [TWCount]))

 

@Anonymous - Can you post sample data as text and expected output? So much easier to troubleshoot if I can recreate the problem locally.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Greg, V-lianl-msft,

Apologies, i created a duplicate post. 

 

@Greg_Deckler  - please take a look at this thread. I've tried to explain the scenario and added sample data, with expected result
https://community.powerbi.com/t5/Desktop/Compare-counts-with-in-group-rank-and-count-the-top-values/...

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.