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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
GuillaumePower
Resolver I
Resolver I

Use of a Var in a "switch" with " SelectedValue"

Hi, I don't  understand how my var PosNeg get the value for sign for each line of the table ?

I thougth that the var was "constant" in  the code.

FV = 
var PosNeg = MIN( MaTable[Signe])
var result = 
switch ( SELECTEDVALUE(MaTable[Libellé]),
"A",sum(MaTable[Valeur])*PosNeg,
"B",sum(MaTable[Valeur])*PosNeg,
"C",sum(MaTable[Valeur])*PosNeg,
"D",sum(MaTable[Valeur])*PosNeg

)
return result

 

 

TableSwitch.png

 

I 've got this result whith the Dax code :

Result.png

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @GuillaumePower 
Each cell in the table visual has it's own filter context. In you example the first cell is subjected to a filter context that contains 'Table'[Libelle] IN { "A" } and so on. Measures are evaluated in each cell independently, therefore, the variable will be evaluated differently in each cell based on its filter context. However, inside a particular cell the variable is evaluated only once and cannot be recalculated. For example, the following measure will return SUM ( 'Table'[Column1] ) and the ALL modifier will not have any effect on the variable as it will never be recalculated hence the first evaluation will always be returned.

Measure =
VAR A = SUM ( 'Table'[Column1] )
RETURN
    CALCULATE ( A, ALL ( 'Table'[Column2] ) )

 

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

Hi @GuillaumePower 
Each cell in the table visual has it's own filter context. In you example the first cell is subjected to a filter context that contains 'Table'[Libelle] IN { "A" } and so on. Measures are evaluated in each cell independently, therefore, the variable will be evaluated differently in each cell based on its filter context. However, inside a particular cell the variable is evaluated only once and cannot be recalculated. For example, the following measure will return SUM ( 'Table'[Column1] ) and the ALL modifier will not have any effect on the variable as it will never be recalculated hence the first evaluation will always be returned.

Measure =
VAR A = SUM ( 'Table'[Column1] )
RETURN
    CALCULATE ( A, ALL ( 'Table'[Column2] ) )

 

johnt75
Super User
Super User

It is constant in that it is only evaluated once within the measure, but the measure itself is being evaluated for each row of your table. If you want to get the minimum across all rows you need to use REMOVEFILTERS(),

FV =
VAR PosNeg =
    CALCULATE ( MIN ( MaTable[Signe] ), REMOVEFILTERS () )
VAR result =
    SWITCH (
        SELECTEDVALUE ( MaTable[Libellé] ),
        "A", SUM ( MaTable[Valeur] ) * PosNeg,
        "B", SUM ( MaTable[Valeur] ) * PosNeg,
        "C", SUM ( MaTable[Valeur] ) * PosNeg,
        "D", SUM ( MaTable[Valeur] ) * PosNeg
    )
RETURN
    result

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

May 2025 Monthly Update

Fabric Community Update - May 2025

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