Forum Discussion

sanc_152's avatar
sanc_152
Helper I
3 years ago
Solved

How can I replace switch using dax

Hi Eveeryone,
I am facing measure issue whilest creating switch condiiotn in measure.I am using this measure in field parameter but it is taking longer time to load and at the end it is failing .

1

ProductSales actualsFlag
110601
220700
33080blank()
440901
5501000

 

Written measure=
 1. Sales=Sum(sales)
 2. Actuals=sum(Actuals)
 3. Swith_level_flag=
Var Selected_=selectedvalue(product[flag])
return

 SWITCH(TRUE(),
Selected_=1,[Actuals],
Selected_=0 ||Selected_=BLANK(),[Sales],
 [Sales]
 )

)
I know it is simple measure but when I mausing with parameter it is failing .Is there any workaround?, I write in other way so it will be more fasterv-yueyunzh-msft 
amitchandak 
Guys can you please help me on this.Is there any workaround I can write in another way.

 

  • Hi, sanc_152 

     

    You can try the following methods.

    Written measure = 
    Var _Sales=SUM('Table'[Sales])
    Var _Actuals=SUM('Table'[Actuals])
    Var _Selected_=SELECTEDVALUE('Table'[Flag])
    return
    SWITCH(TRUE(),
    _Selected_=1,_Actuals,
    OR(_Selected_=0,_Selected_=BLANK()),_Sales,
     _Sales
     )

    Is this the result you expect?

     

    Best Regards,

    Community Support Team _Charlotte

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • There are a few things that could be causing performance issues with your measure. One thing to consider is the use of the selectedvalue() function, which can be expensive and slow down your measure. Instead, you could try using a disconnected table to store the flag values and use a slicer to select the desired flag value.

    Additionally, you could try simplifying your measure by combining the Sales and Actuals measures into one measure using an IF statement, rather than using the SWITCH function. Here's an example of how you could write the measure:

    Sales and Actuals Measure = VAR SelectedFlag = SELECTEDVALUE(Product[Flag]) RETURN IF( SelectedFlag = 1, SUM(Actuals), SUM(Sales) )

    This measure uses a single IF statement to check the selected flag value and return the appropriate sum value based on the flag. This should be simpler and more efficient than using the SWITCH function.

2 Replies

  • v-zhangti's avatar
    v-zhangti
    Community Support

    Hi, sanc_152 

     

    You can try the following methods.

    Written measure = 
    Var _Sales=SUM('Table'[Sales])
    Var _Actuals=SUM('Table'[Actuals])
    Var _Selected_=SELECTEDVALUE('Table'[Flag])
    return
    SWITCH(TRUE(),
    _Selected_=1,_Actuals,
    OR(_Selected_=0,_Selected_=BLANK()),_Sales,
     _Sales
     )

    Is this the result you expect?

     

    Best Regards,

    Community Support Team _Charlotte

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Adamboer's avatar
    Adamboer
    Responsive Resident

    There are a few things that could be causing performance issues with your measure. One thing to consider is the use of the selectedvalue() function, which can be expensive and slow down your measure. Instead, you could try using a disconnected table to store the flag values and use a slicer to select the desired flag value.

    Additionally, you could try simplifying your measure by combining the Sales and Actuals measures into one measure using an IF statement, rather than using the SWITCH function. Here's an example of how you could write the measure:

    Sales and Actuals Measure = VAR SelectedFlag = SELECTEDVALUE(Product[Flag]) RETURN IF( SelectedFlag = 1, SUM(Actuals), SUM(Sales) )

    This measure uses a single IF statement to check the selected flag value and return the appropriate sum value based on the flag. This should be simpler and more efficient than using the SWITCH function.