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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
the001
Frequent Visitor

Store "IF" condition in variable

Hi,

 

I'm wondering, is power bi capable to store IF conditions in a variable?

example

 

 

 

var kondisi = 1 //using a dropdown, it will change value either 1 or 2

VAR param_kondisi = 
    IF(kondisi = 1,
      'Table'[Population 1] <> BLANK(),
      'Table'[Population 2] <> BLANK()
     )

Var Calculate_Result =
    CALCULATE(
        COUNT('Table'[primkey]),
        KEEPFILTERS(
            'Table'[client_name] = "Superman"
            && param_kondisi

        )
    )

 

 

 

any suggestion would be very appreciated,

thank you

5 REPLIES 5
amitchandak
Super User
Super User

@the001 , it should return true or false

 

try like

Calculate_Result =
CALCULATE(
COUNT('00. SPK'[primkey]),
KEEPFILTERS(
'Table'[client_name] = "Superman"
&& ((kondisi = 1 && 'Table'[Population 1] <> BLANK()) || (kondisi <> 1 && 'Table'[Population 2] <> BLANK()))

)
)

thanks! it returns correctly

but is there any chance that pattern as I mentioned earlier, is possible in power bi?

the001_0-1642056787350.png

 

@the001 , Just make sure it returns true or false

 

like

IF(kondisi = 1,
if('Table'[Population 1] <> BLANK(),true(), false())
if('Table'[Population 2] <> BLANK(),true(), false())
)

it does not return true or false, but it is returned as a condition inside the variable calculate_result

it is returned as it was like below :

 

 

 

Var Calculate_Result =
    CALCULATE(
        COUNT('Table'[primkey]),
        KEEPFILTERS(
            'Table'[client_name] = "Superman"
            && IF(kondisi = 1,
                 'Table'[Population 1] <> BLANK(),
                 'Table'[Population 2] <> BLANK()
               )

        )
    )

 

 

 

Hi @the001 ,

I'm not quite sure what you want... According to the formula you provided, the result returned by the variable Calculate_Result should be a numeric value: the number of primkeys. And the formula syntax of the variable Calculate_Result is also correct. Could you please provide a simple sample data and the final result you want? Maybe we can implement your requirement in some other way later. Thank you.

Best Regards

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

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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