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
GuillaumePower
Frequent Visitor

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
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.