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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
BIswajit_Das
Solution Supplier
Solution Supplier

Power bi DAX findout 2nd MAX value

Hello Guys,
I have a table like

itemresa
1a30.98
1b30.98
1c20.34
1d

17.7

and
my required table is like

itemresamax2ndgap
1a30.9830.9830.980
1b30.9830.9830.980
1c20.3430.9830.980
1d17.730.9830.980

when i calculated its hard to detect max cause both a and b have same values so the coming 2nd max result is wrong
i.e the coming result is like

itemresamax2ndgap
1a30.9830.9820.3410.64
1b30.9830.9820.3410.64
1c20.3430.9820.3410.64
1d17.730.9820.3410.64

 

Thanks & Regrads...

1 ACCEPTED SOLUTION
BIswajit_Das
Solution Supplier
Solution Supplier

Hello I've finally figures it out 
Here's the used related DAX

VAR _max_count =
    MAXX (
        SUMMARIZE (
            'table_name',
            'table_name'[item],
            'table_name'[res],
            "count",
            COUNTROWS ( 'table_name' )
        ),
        [count]
    )
VAR _value =
    MAXX (
        FILTER (
            SUMMARIZE (
                'table_name',
                'table_name'[item],
       
                'table_name'[res],
                "@count",
                COUNTROWS ( 'table_name' )
            ),
            [@count] = _max_count
        ),
        'table_name'[res]
    )
VAR _sec_max_count =
    MAXX (
        FILTER (
            SUMMARIZE (
                'table_name',
                'table_name'[item],
       
                'table_name'[res],
                "count2",
                COUNTROWS ( 'table_name' )
            ),
            'table_name'[res] <> _value &&
            'table_name'[item] = EARLIER('table_name'[item])
   
        ),
        [count2]
    )

VAR _total =
CALCULATE (
    COUNT ( 'table_name'[res] ),
    FILTER (
        'table_name',
        'table_name'[item] = EARLIER ( 'table_name'[item] )

        )
    )
RETURN
    DIVIDE(_sec_max_count,_total,0)

View solution in original post

6 REPLIES 6
BIswajit_Das
Solution Supplier
Solution Supplier

#EP_MAX_PARTYCOUNT% =
VAR _value =
    MAXX (
        FILTER (
            SUMMARIZE (
                'Z_EP_DATA',
                'Z_EP_DATA'[PC_ID],
                'Z_EP_DATA'[RESPONSE1],
                "@count",
                COUNTROWS ( 'Z_EP_DATA' )
            ),
            'Z_EP_DATA'[PC_ID] = EARLIER('Z_EP_DATA'[PC_ID])
        ),
        [@count]
    )
VAR _total =
CALCULATE (
    DISTINCTCOUNT( 'Z_EP_DATA'[MOBILE] ),
    FILTER (
        'Z_EP_DATA',
        'Z_EP_DATA'[PC_ID] = EARLIER ( 'Z_EP_DATA'[PC_ID] )
        )
    )
RETURN
    DIVIDE(_value,_total,0)
BIswajit_Das
Solution Supplier
Solution Supplier

Hello I've finally figures it out 
Here's the used related DAX

VAR _max_count =
    MAXX (
        SUMMARIZE (
            'table_name',
            'table_name'[item],
            'table_name'[res],
            "count",
            COUNTROWS ( 'table_name' )
        ),
        [count]
    )
VAR _value =
    MAXX (
        FILTER (
            SUMMARIZE (
                'table_name',
                'table_name'[item],
       
                'table_name'[res],
                "@count",
                COUNTROWS ( 'table_name' )
            ),
            [@count] = _max_count
        ),
        'table_name'[res]
    )
VAR _sec_max_count =
    MAXX (
        FILTER (
            SUMMARIZE (
                'table_name',
                'table_name'[item],
       
                'table_name'[res],
                "count2",
                COUNTROWS ( 'table_name' )
            ),
            'table_name'[res] <> _value &&
            'table_name'[item] = EARLIER('table_name'[item])
   
        ),
        [count2]
    )

VAR _total =
CALCULATE (
    COUNT ( 'table_name'[res] ),
    FILTER (
        'table_name',
        'table_name'[item] = EARLIER ( 'table_name'[item] )

        )
    )
RETURN
    DIVIDE(_sec_max_count,_total,0)
BIswajit_Das
Solution Supplier
Solution Supplier

Hello @Ahmedx thanks for responding but

my required result table should like

itemresamax2ndgap
1a30.9830.9830.980
1b30.9830.9830.980
1c20.3430.9830.980
1d17.730.9830.980
Greg_Deckler
Super User
Super User

@BIswajit_Das Try:

max a 2nd = 
    VAR __Item = MAX('Table'[item])
    VAR __Table = FILTER(ALL('Table'), [item] = __Item)
    VAR __Summarized = SUMMARIZE(__Table, [item], [a], "__Count", COUNTROWS('Table'))
    VAR __Max = MAXX(__Table, [a])
    VAR __Result = 
        IF(
            MAXX(FILTER(__Summarized, [a] = __Max),[__Count]) > 1,
            __Max,
            MAXX(FILTER(__Summarized, [a] < __Max), [a])
        )
RETURN
    __Result


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hello @Greg_Deckler  Thanks for responding but what if i have multiple Items
i.e

itemresamax2ndgap
1a30.9830.9830.980
1b30.9830.9830.980
1c20.3430.9830.980
1d17.730.9830.980
2a55554510
2b45554510
3a35653530
3c65653530
Ahmedx
Super User
Super User

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Power bi DAX findout 2nd MAX value.pbix

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.