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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Calculae measure based on slicer selection

Hi all

I have a table with two columns, one has names (18 different names), but are repeated multiple times,  and the other has values $

Name       Values

TOM     12.345
MARK     65.467
SARA     34.556
ELSA     34.356

 

What I want to do in my report is, when I select two names in a slicer the variance between the two names is calculated and posted on a card.

 

I tried to create a measure to find the fisrt and the last value but is not working:

 
AllValues: Sum[Values]
 
FirstValue =
CALCULATE (
    [AllValues];
    TOPN (
        1;
        FILTER(
            VALUES ( 'Base'[Name] );
            COUNTROWS ( VALUES ( 'Base'[Name] ) )
        )
    )
)

 

maybe there is a better way! open to options

thanks!

1 ACCEPTED SOLUTION

@Anonymous try this measure. 

 

Total = SUM( Table1[Values] )
Difference in Top 2 = 
VAR __Name1st = CALCULATE( [Total], TOPN( 1, VALUES(Table1[Name] ), [Total], DESC ) )
VAR __Name2ndTable = TOPN( 2, VALUES(Table1[Name] ), [Total], DESC ) 
VAR __Name2nd = MINX( __Name2ndTable, [Total] )
RETURN __Name1st - __Name2nd

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

5 REPLIES 5
parry2k
Super User
Super User

@Anonymous what happens if not two name are selected (1 or more than 2 selected)?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Hi @parry2k  

well, I was trying to solve my problem by creating a table in a measure and using the measure to bring the first row  value of the table and then use a second measure to bring the second row value, therefore if more than two names are selected, the measures are going to ignore the order values.

my current measures are:

 

 

FirstValue =

 

CALCULATE (

 

    [AllValues];

 

    TOPN (

 

        1;

 

        FILTER(

 

            VALUES ( 'Base'[Name] );

 

            COUNTROWS ( VALUES ( 'Base'[Name] ))

 

        )

 

    )

 

)

 

 

 

 

 

SndValue = CALCULATE (

 

    [AllValues];

 

    TOPN (

 

        2;

 

        FILTER(

 

            VALUES ( 'Base'[Name] );

 

            COUNTROWS ( VALUES ( 'Base'[Name] ) )

 

        )

 

    )

 

)

 

- [FirstValue]

 

 

 

But using topN my measure is adding the values of the table, so I had to rest the first value to adjust.

 

and then: %Var:= SndValue/FirstValue-1

 

thanks for your comments, if you think that there is a better way of doing this, I am open to suggestions.

 

Fabian.

@Anonymous try this measure. 

 

Total = SUM( Table1[Values] )
Difference in Top 2 = 
VAR __Name1st = CALCULATE( [Total], TOPN( 1, VALUES(Table1[Name] ), [Total], DESC ) )
VAR __Name2ndTable = TOPN( 2, VALUES(Table1[Name] ), [Total], DESC ) 
VAR __Name2nd = MINX( __Name2ndTable, [Total] )
RETURN __Name1st - __Name2nd

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hello @parry2k 

 

This measure has helped me with a similar problem however I do have a question about it, please:

 

When the variance is negative, it's showing it as positve still but I need it to show if it's negative.

 

My original data:

 

RANGENEED DIFF PREV WK (PCS)NEED DIFF PREV WK (%)
Product 1-3,848-3.63%
Product 24880.06%
Product 39350.85%
Product 41,0082.71%
Product 51,5081.25%
Product 62,0541.36%
Product 74,4610.80%

 

Data with your solution:

 

RANGEDifference in Top 2Difference in Top 2 %
Product 13,8483.77%
Product 24880.06%
Product 39350.85%
Product 41,0082.71%
Product 51,5081.25%
Product 62,0541.36%
Product 74,4610.80%

 

As you can see, it's not showing the variance as negative value (like it should be) therefore the % calculation is not correct. 

 

Would you have a solution for this?

Thanks!

Anonymous
Not applicable

@parry2kBrilliant!
Thanks a lot!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.