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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.