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
memarquez
New Member

IF or SWITCH

Hello,

 

Can someone help us to correct our DAX provided using SWITCH?

 

SWITCH (

   TRUE (),

   Sheet1[encoded] = 1, 0,

   Sheet1[learnersbringdrinkingwater] = 1, 1,

   Sheet1[withdrinkingwaterbutnotregular] = 1

       && ( Sheet1[hassafewaterthatistested] = 1

       || Sheet1[schoolrequireswaterqualitycertif] = 1

       || Sheet1[waterisboiled] = 1

       || Sheet1[waterisfiltered] = 1 ), 2,

   Sheet1[withdrinkingwaterallthetime] = 1

       && ( Sheet1[hassafewaterthatistested] = 1

       || Sheet1[schoolrequireswaterqualitycertif] = 1

       || Sheet1[waterisboiled] = 1

       || Sheet1[waterisfiltered] = 1 ), 2,

   Sheet1[withfreewater] = 1

       && Sheet1[withdrinkingwaterallthetime] = 1

       && ( Sheet1[hassafewaterthatistested] = 1

       || Sheet1[schoolrequireswaterqualitycertif] = 1

       || Sheet1[waterisboiled] = 1

       || Sheet1[waterisfiltered] = 1 ), 3,

   BLANK ()

)

 

It is not working. The goal is to come up with different scores of 0-3 using the following logic:

Remains blank if Encoded = 0

1 star if Learners bring water =1

2 stars if with drinking water but not regular =1 or if with drinking water all the time =1 AND (If water has tested=1 OR certified water=1 OR water is filtered =1 OR water is boiled)

3 stars if with free water =1 AND if with drinking water all the time =1 AND (If water has tested=1 OR certified water=1 OR water is filtered =1 OR water is boiled)

0 star if OTHERWISE

 

 

1 ACCEPTED SOLUTION
Nathaniel_C
Super User
Super User

Hi @memarquez ,
Ran this through the Dax Formatter Daxformatter.com. Try it now, and if does not work, let us know what the output is.
Let me know if you have any questions. Remember if it finds a true as it drops through the tests, it stops and returns that value. You may want to break up the code into separate lines even if they have the same values. e.g (2) for readability and debugging.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel

Measure =
SWITCH (
    TRUE (),
    Sheet1[encoded] = 1, 0,
    Sheet1[learnersbringdrinkingwater] = 1, 1,
    Sheet1[withdrinkingwaterbutnotregular] = 1
        && ( Sheet1[hassafewaterthatistested] = 1
        || Sheet1[schoolrequireswaterqualitycertif] = 1
        || Sheet1[waterisboiled] = 1
        || Sheet1[waterisfiltered] = 1 ), 2,
    Sheet1[withdrinkingwaterallthetime] = 1
        && ( Sheet1[hassafewaterthatistested] = 1
        || Sheet1[schoolrequireswaterqualitycertif] = 1
        || Sheet1[waterisboiled] = 1
        || Sheet1[waterisfiltered] = 1 ), 2,
    Sheet1[withfreewater] = 1
        && Sheet1[withdrinkingwaterallthetime] = 1
        && Sheet1[hassafewaterthatistested] = 1
        || Sheet1[schoolrequireswaterqualitycertif] = 1
        || Sheet1[waterisboiled] = 1
        || Sheet1[waterisfiltered] = 1, 3,
    BLANK ()
)

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

6 REPLIES 6
Nathaniel_C
Super User
Super User

Hi @memarquez ,
Ran this through the Dax Formatter Daxformatter.com. Try it now, and if does not work, let us know what the output is.
Let me know if you have any questions. Remember if it finds a true as it drops through the tests, it stops and returns that value. You may want to break up the code into separate lines even if they have the same values. e.g (2) for readability and debugging.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel

Measure =
SWITCH (
    TRUE (),
    Sheet1[encoded] = 1, 0,
    Sheet1[learnersbringdrinkingwater] = 1, 1,
    Sheet1[withdrinkingwaterbutnotregular] = 1
        && ( Sheet1[hassafewaterthatistested] = 1
        || Sheet1[schoolrequireswaterqualitycertif] = 1
        || Sheet1[waterisboiled] = 1
        || Sheet1[waterisfiltered] = 1 ), 2,
    Sheet1[withdrinkingwaterallthetime] = 1
        && ( Sheet1[hassafewaterthatistested] = 1
        || Sheet1[schoolrequireswaterqualitycertif] = 1
        || Sheet1[waterisboiled] = 1
        || Sheet1[waterisfiltered] = 1 ), 2,
    Sheet1[withfreewater] = 1
        && Sheet1[withdrinkingwaterallthetime] = 1
        && Sheet1[hassafewaterthatistested] = 1
        || Sheet1[schoolrequireswaterqualitycertif] = 1
        || Sheet1[waterisboiled] = 1
        || Sheet1[waterisfiltered] = 1, 3,
    BLANK ()
)

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @Nathaniel_C

 

I've tried using your proposed syntax. It works. I found no error using the DAX formatter; however, once I plugged that syntax in the Power BI desktop, the results turn to 0-1 range which that should not be the case. As mentioned, the results must be from 0-3 (including blanks if encoded ==0). Much appreciated if you can still help us on this.

 

I shared to @bpsearle the sample dataset. 

 

Thanks,
Marvin

 

 

 

Hi Marvin

 

The data provided is missing withdrinkingwaterallthetime so I'll modify the DAX to ignore this condition, if you can provide what the values should be as a single column then I can test it fully

 

Thanks, Brian

Hi Marvin

 

Looking at the result of the expression, if you want it to return values 0 to 3 I would say the expression is incorrect. If you supply a separate column with what the expected values are then we can rework the expression to match.

 

Thanks, Brian

bpsearle
Resolver II
Resolver II

Hi @memarquez 

 

Can you provide a sample of your data and I will plug your formula into it and try and see if I can figure this out

 

Thanks, Brian

Hello @bpsearle 

 

Here's the sample of our data we are currently working. We'll be glad if you can help us on this. 

 

Thanks,

Marvin

 

 

withdrinkingwaterbutnotregularwithnodrinkingwaterwithfreewaterlearnersbringdrinkingwaterschoolrequireswaterqualitycertifwaterisboiledwaterisfilteredhassafewaterthatistested
10110101
10110100
00110010
10110000
        
00111111
10110101
10111111
        
        
10110001
00111011
00111001
00010000
00111000
10110000
        
10110000
10110000

Helpful resources

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

Top Solution Authors