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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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