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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Display Issue

Hi All,

 

I am trying to show difference between average of top 3 customers value and customer value selected in slicer for a particular selected quarter using below formula which is working fine.

 

The challenge I am facing is if the selected Customer is in top 3 or if the differnce we derived is a negative value then we should not display the result

 

var y = CALCULATE( SUMX(TOPN(3, ALLSELECTED(Master[Customer Name]), [Total_value], DESC),Master[Total_value]),ALL(Master[Broker Name]))


var x =
IF (
SELECTEDVALUE ( Master[Customer Name] )
= SELECTEDVALUE (Master[Customer Name]),
CALCULATE (
VAR _seleYear =
SELECTEDVALUE ( 'Slicer Table'[Year] )
VAR _seleQuar =
SELECTEDVALUE ( 'Slicer Table'[Quarter] )
RETURN
IF (
SELECTEDVALUE( Master[Year] ) = _seleYear
&& SELECTEDVALUE( Master[Quarter]) = _seleQuar,
SUM ( Master[Total] ))
)
)
return (y/3)-x

 

Can someone please suggest on how to achieve this.

 

Thanks in advance

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @Anonymous ,

According to your description, your problem is if the selected Customer is in top 3 or if the result is a negative value then return blank. Here's my solution.

Modify the formula.

Measure =
VAR y =
    CALCULATE (
        SUMX (
            TOPN ( 3, ALLSELECTED ( Master[Customer Name] ), [Total_value], DESC ),
            Master[Total_value]
        ),
        ALL ( Master[Broker Name] )
    )
VAR x =
    IF (
        SELECTEDVALUE ( Master[Customer Name] )
            = SELECTEDVALUE ( Master[Customer Name] ),
        CALCULATE (
            VAR _seleYear =
                SELECTEDVALUE ( 'Slicer Table'[Year] )
            VAR _seleQuar =
                SELECTEDVALUE ( 'Slicer Table'[Quarter] )
            RETURN
                IF (
                    SELECTEDVALUE ( Master[Year] ) = _seleYear
                        && SELECTEDVALUE ( Master[Quarter] ) = _seleQuar,
                    SUM ( Master[Total] )
                )
        )
    )
VAR _Top =
    SELECTCOLUMNS (
        TOPN ( 3, ALL ( 'Master' ), 'Master'[Total_value] ),
        "Customer", [Customer Name]
    )
RETURN
    IF (
        COUNTROWS (
            FILTER ( ALLSELECTED ( 'Master' ), 'Master'[Customer Name] IN _Top )
        ) > 0,
        BLANK (),
        IF ( ( y / 3 ) - x < 0, BLANK (), ( y / 3 ) - x )
    )

As you say, your original formula works partly, I only create a sample to verify the condition "if the selected Customer is in top 3":

vkalyjmsft_0-1664177316819.png

Get correct result.

vkalyjmsft_1-1664177403129.png

vkalyjmsft_2-1664177416421.png

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

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

View solution in original post

2 REPLIES 2
v-yanjiang-msft
Community Support
Community Support

Hi @Anonymous ,

According to your description, your problem is if the selected Customer is in top 3 or if the result is a negative value then return blank. Here's my solution.

Modify the formula.

Measure =
VAR y =
    CALCULATE (
        SUMX (
            TOPN ( 3, ALLSELECTED ( Master[Customer Name] ), [Total_value], DESC ),
            Master[Total_value]
        ),
        ALL ( Master[Broker Name] )
    )
VAR x =
    IF (
        SELECTEDVALUE ( Master[Customer Name] )
            = SELECTEDVALUE ( Master[Customer Name] ),
        CALCULATE (
            VAR _seleYear =
                SELECTEDVALUE ( 'Slicer Table'[Year] )
            VAR _seleQuar =
                SELECTEDVALUE ( 'Slicer Table'[Quarter] )
            RETURN
                IF (
                    SELECTEDVALUE ( Master[Year] ) = _seleYear
                        && SELECTEDVALUE ( Master[Quarter] ) = _seleQuar,
                    SUM ( Master[Total] )
                )
        )
    )
VAR _Top =
    SELECTCOLUMNS (
        TOPN ( 3, ALL ( 'Master' ), 'Master'[Total_value] ),
        "Customer", [Customer Name]
    )
RETURN
    IF (
        COUNTROWS (
            FILTER ( ALLSELECTED ( 'Master' ), 'Master'[Customer Name] IN _Top )
        ) > 0,
        BLANK (),
        IF ( ( y / 3 ) - x < 0, BLANK (), ( y / 3 ) - x )
    )

As you say, your original formula works partly, I only create a sample to verify the condition "if the selected Customer is in top 3":

vkalyjmsft_0-1664177316819.png

Get correct result.

vkalyjmsft_1-1664177403129.png

vkalyjmsft_2-1664177416421.png

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

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

lbendlin
Super User
Super User

Please provide sanitized sample data that fully covers your issue.
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors