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
Anonymous
Not applicable

Need a semi complex if / Else statement

I'm trying to create a new column that looks at an existing column called % Completed (if contains numerical values from 0 to 1 and blanks) and then outputs text based off what value is in that column.  I've tried a bunch of formats.  No luck.  Here is the gist...

 

If % Completed = 1, "100% Completed"

elseif % Completed <1 but > .66, "Knowledge Stage Completed"

elseif % Completed <.661 but > 0, "Learning Started"

elseif % Completed = 0, "Not Started"

elseif % Competed = blank/null, "Account created, Not Started"

 

 

10 REPLIES 10
jthomson
Solution Sage
Solution Sage

It'd certainly be easier to code if you trimmed down your logic, you don't need to say something is both <1 and >0.66 if you've already assigned everything that is 1 to 100% completed, similar with the next step, you just need to say >0
Greg_Deckler
Super User
Super User

Are you doing this in DAX or M? I'd use a SWITCH statement in DAX, SWITCH(TRUE()...)


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thanks Greg, always happy when I see you on a thread!

I am using DAX, and had tried the SWITCH statement.  My challenge was more around all the conditions - I'm getting a "Too many arguments were passed to the AND function.  The maximum argument count for the function is 2.".  I had hoped I had done enough isolation of the IF(AND statements to allow for this to work.  here is my formula...

 

Column = SWITCH (TRUE (), [% Completed] = 1, "100% Completed",
[% Completed] = 0, "Not Started",
(IF(AND([% Completed] < 1, [% Completed] >.66, "Knowledge Stage Completed"))),
(IF(AND([% Completed] > 0, [% Completed] <.661, "Learning Started"))),
[% Completed] = "", "Account Created, Not Started")
 
Any advice on how to fix this so I get my desired outputs?

HI @Anonymous,

 

I'd like to recommend you to use if statement to nested switch function instead of combine them in switch function, switch function not suitable to compare with data range.

Column =
IF (
    AND ( [% Completed] > 0, [% Completed] < .661 ),
    "Learning Started",
    IF (
        AND ( [% Completed] > .66, [% Completed] < 1 ),
        "Knowledge Stage Completed",
        SWITCH (
            [% Completed],
            1, "100% Completed",
            0, "Not Started",
            "", "Account Created, Not Started"
        )
    )
)

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Thank you for this.  When I entered that formula, I got the following error:

 

Function 'SWITCH' does not support comparing values of type Number with values of type Text. Consider using the VALUE or FORMAT function to convert one of the values.

HI @Anonymous,

 

I add value function to switch to convert value to number, maybe you can try to use following formula:

Column =
IF (
    AND ( [% Completed] > 0, [% Completed] < .661 ),
    "Learning Started",
    IF (
        AND ( [% Completed] > .66, [% Completed] < 1 ),
        "Knowledge Stage Completed",
        SWITCH (
            VALUE ( Table[% Completed] ),
            1, "100% Completed",
            0, "Not Started",
            "", "Account Created, Not Started"
        )
    )
)

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Thank you.  Came back with another error - The syntax for 'Table' is incorrect. (DAX(IF ( AND ( [% Completed] > 0, [% Completed] < .661 ), "Learning Started", IF ( AND ( [% Completed] > .66, [% Completed] < 1 ), "Knowledge Stage Completed", SWITCH ( VALUE ( Table [% Completed] ), 1, "100% Completed", 0, "Not Started", "", "Account Created, Not Started" ) )))).

 

I couldn't find an expression called Table

Hi @Anonymous,

 

Table means your table name who host the column.(where [% Completed] field exists)

 

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Column =
IF (
    AND ( [% Completed] > 0, [% Completed] < .661 ),
    "Learning Started",
    IF (
        AND ( [% Completed] > .66, [% Completed] < 1 ),
        "Knowledge Stage Completed",
        SWITCH (
            VALUE ( Table[% Completed] ),
            1, "100% Completed",
            0, "Not Started",
            "", "Account Created, Not Started"
        )
    )
)

I'm now seeing a Function 'SWITCH' does not support comparing values of type Number with values of type Text error. Consider using the VALUE or FORMAT function to convert one of the values.  I tried formatting the column (which was set to Decimal Number and General) to Decimal Number and Decimal Number - didn't help.  

Anonymous
Not applicable

Doh.  Forrest for the trees....

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.