Formula HELP - SWITCH, TRUE()

Hi All,

Looking for some help on the formula below.

My understanding is that the formula would evaluate each condition to check if the result was “TRUE” and then return a “Fail” if any of the conditions resulted in “FALSE”.

The below is what is supposed to happen:

(1) SLA E2HR ORIGINAL =
SWITCH (
TRUE (),
AND (
SELECTEDVALUE ( ‘MTSE (Completed)’[Maintenance Type] ) = “Emergency 2 Hr”, //Check to see if it Maintenance Type is Emergency 2 HR
SELECTEDVALUE ( ‘MTSE (Completed)’[Is Working Day] ) = “Workday” //Check to see if it is a Working and not a Non Working Day
), “Pass”,
AND (
[Completed Date] <= [Due Date], //Check to see if the works are completed before the Due Date
[Completed Time] <= [Emergency 2HR Time Target] //Check to see if the works are completed before the Due Timeframe
), “Pass”,
AND (
[Admin Date] <= [Next Working Day], //Check to see if the works have been updated on the system before the next working day
[Admin Time] <= [Target Time] //Check to see if the works have been updated on the system before the next working day Target Time
), “Pass”,

"Fail" //If non of the above criteria match then this Fails the SLA

)

This Formula is providing mixed results and im not sure where or why its going wrong, ive attached some screenshots as to the results of what is happening with the formula.

And you will see that it is “Passing” on Weekends and Passing when the Maintenance Type is not Emergency 2HR.

Any help, guidance would be greatly appreciated.

PS - pretty new to PBI & DAX so any help, guidance would be greatly appreciated.

Thanks in Advance

Martyn

Hi @martynross85.

The SWITCH function will stop evaluating conditions the first time it finds a match, so in your case, if the first “AND” is true, then it will pass; if one of the 3 “ANDs” is true in all cases, then you’ll never get to the “Fail”. (Hard to confirm without a PBIX, and no time to make-up a sample, but this is the way “cases” are evaluated in Microsoft SQL Server, and AFAIK DAX SWITCH works the same way.)

If all 3 of your conditions must be TRUE in order to be a “Pass”, then wrap all 3 of them in another “AND” (something like, just threw this into the DAX Clean up tool)

( 1 ) SLA E2HR ORIGINAL =
SWITCH(
    TRUE(),
    AND(
        AND(
            SELECTEDVALUE( ‘ MTSE( Completed ) ’ [Maintenance Type] ) = “ Emergency 2 Hr ”,
            //Check to see IF it Maintenance Type is Emergency 2 HR
            SELECTEDVALUE( ‘ MTSE( Completed ) ’ [Is Working Day] ) = “ Workday ” //Check to see IF it is a Working AND NOT a Non Working DAY
        ),
        AND(
            [Completed Date] <= [Due Date],
            //Check to see IF the works are completed before the Due DATE
            [Completed Time] <= [Emergency 2HR TIME Target] //Check to see IF the works are completed before the Due Timeframe
        ),
        AND(
            [Admin Date] <= [Next Working Day],
            //Check to see IF the works have been updated on the system before the next working DAY
            [Admin Time] <= [Target Time] //Check to see IF the works have been updated on the system before the next working DAY Target TIME
        ),
    ),
    “ Pass ”,
    "Fail" //If non of the above criteria match then this Fails the SLA
)

Hope this helps.
Greg

1 Like

Hi @Greg,

Thanks for getting back to me much appreciated.

I am receiving the following error:

Too many arguments were passed to the AND function. The maximum argument count for the function is 2.

(1) SLA E2HR v5 = 
SWITCH (
    TRUE (),
        AND (
            AND(
                SELECTEDVALUE ( 'MTSE (Completed)'[Maintenance Type] ) = "Emergency 2 Hr", //Check to see if it Maintenance Type is Emergency 2 HR
                SELECTEDVALUE ( 'MTSE (Completed)'[Is Working Day] ) = "Workday" //Check to see if it is a Working and not a Non Working Day
                ),
            AND (
                [Completed Date] <= [Due Date], //Check to see if the works are completed before the Due Date
                [Completed Time] <= [Emergency 2HR Time Target] //Check to see if the works are completed before the Due Timeframe
                ),
            AND (
                [Admin Date] <= [Next Working Day], //Check to see if the works have been updated on the system before the next working day
                [Admin Time] <= [Target Time] //Check to see if the works have been updated on the system before the next working day Target Time
                ),
             ),
        "Pass", 
        "Fail" //If non of the above criteria match then this Fails the SLA
        )

Thanks again for your help!

Hi @martynross85,

It’s hard to replicate same without sample file but I suggest you use && instead of AND function. Replicated Greg’s code with &&

(1) SLA E2HR v5 = 
SWITCH (
    TRUE (),  
    (   SELECTEDVALUE ( 'MTSE (Completed)'[Maintenance Type] ) = "Emergency 2 Hr" 
        && //Check to see if it Maintenance Type is Emergency 2 HR
        SELECTEDVALUE ( 'MTSE (Completed)'[Is Working Day] ) = "Workday" //Check to see if it is a Working and not a Non Working Day
    ) 

    && 

    (
        [Completed Date] <= [Due Date] 
        && //Check to see if the works are completed before the Due Date
        [Completed Time] <= [Emergency 2HR Time Target] //Check to see if the works are completed before the Due Timeframe
    )

    &&

    (
        [Admin Date] <= [Next Working Day] 
        && //Check to see if the works have been updated on the system before the next working day
        [Admin Time] <= [Target Time] //Check to see if the works have been updated on the system before the next working day Target Time
    ),
        
    "Pass", 
    "Fail" //If non of the above criteria match then this Fails the SLA
)
2 Likes

@martynross85 I believe you should write your measure like this:

SLA E2HR ORIGINAL =
IF (
    SELECTEDVALUE ( 'MTSE (Completed)'[Maintenance Type] ) = "Emergency 2 Hr"
        && SELECTEDVALUE ( 'MTSE (Completed)'[Is Working Day] ) = "Workday"
        && [Completed Date] <= [Due Date]
        && [Completed Time] <= [Emergency 2HR Time Target]
        && [Admin Date] <= [Next Working Day]
        && [Admin Time] <= [Target Time],
    "Pass",
    "Fail"
)
2 Likes

Hi @MK3010,

Thanks for reaching out.

I have tried the code snippet and it flows without error.

However , all results are now returning “FAIL”

Hi @martynross85

Can you show the target time column?

Hi @AntrikshSharma

Thanks for having a look at this .

The measure you have provided flows correctly without error, however provides the same result as @MK3010 in that all entries results to “FAIL”

HI MK3010,

So the Target Time is always 10:00:00.

ie. the admin time is when they load it onto the system, however they need to ensure that this is done by 10am the next working day

hope this makes sense

Thanks for all your help so far - much appreciated

Martyn

HI @martynross85,

Please use below code as a calculated column. You can see the output also in the screen shot.

SLA E2HR ORIGINAL = 
IF (
    [Maintenance Type]  = "Emergency 2 Hr"
        && [ls Working Day]  = "Working Day"
        && [Completed Date] <= [Due Date]
        && [Completed Time] <= [Emergency 2HR Time Target]
        && [Admin Date] <= [Next Working Day]
        && [Admin Time] <= [Target Time],
    "Pass",
    "Fail"
)

Hi @martynross85, did the response provided by the experts help you solve your query? If not, how far did you get and what kind of help you need further? If yes, kindly mark the thread as solved. Thanks!

Hi MK3010,

Thanks again for looking at this - however my results are resulting in an error, where the [is working day] column is not being recognised.

see attached screenshot

thanks
Martyn

Hi, Unfortunately not.

The formula provided doesn’t seem to recognise the calculated column [Is Working Day] as a valid column.

Thanks
Martyn

I have tried to Add a MAXX (TableName, [is working day] - incase that was an issue - however i still receive an error

Hi @martynross85.

It looks like the references to the [Is Working Day] column may not be working because the column has a different name (a “one-s” in the DAX formula in post 9 and an “eye-s” is used in post 12); perhaps that’s the issue and it will disappear once you confirm spelling.

The forum members are taking experienced guesses but could really benefit from both a PBIX and a mock-up of your desired outcome if you’d like to take this further. Can you please provide?

Thanks,
Greg

Hi @martynross85,

As @Greg mentioned I tried to replicate exactly what you have shown in the snapshot but there might be chances that you have a blank and which is hard to figure out only by referring snapshot.Also I must admit that it was really hard to manually type each data and create a sample for this.

What you can do is that you go to your header and copy “Is Working day” and replace in the formula that way it will work same applies to others also if you get any error. Note the above formula must be calculated column not measure.

1 Like

Hi @MK3010 & @Greg @AntrikshSharma ,

Thanks for looking into this for me - much appreciated. After much frustration of this failing for me - i decided to go back to the drawing board (As the errors being received must have been a result of something that i have done - without knowing)

I have rebuilt the report from scratch and the only difference i can tell is that i have created the calculated columns in the “Report Area” rather than the “Data Area”.

I didn’t think this would have any impact whatsoever - however it seems to have done the job.

Also much appreciation for the below formula - which now gives us the desired results so thanks guys.

i had to add a MAXX at the start in-order for it to run for me

SLA (1) 2 HR Emergency (Working Hours) = 
MAXX(MTSE,
IF (
    SELECTEDVALUE(MTSE[Maintenance Type])= "Emergency 2 Hr"
        && SELECTEDVALUE ( MTSE[Is Working Day] ) = "Working Day"
        && SELECTEDVALUE(MTSE[Office Hours]) = "Working Hours"
        && MTSE[Completed Date - Date] <= MTSE[Due Date - Date]
        && MTSE[Completed Date - Time] <= MTSE[Emergency 2HR Target]
        && MTSE[Admin Date - Date] <= MTSE[NextWorkingDay]
        && MTSE[Admin Date - Time] <= MTSE[Target Time ] ,
    "Pass",
    "Fail"
))

So MASSIVE thanks from me to everyone who helped with this.

Aappreciate a PBIX file would have helped, however it is data from a client so i have had to anonymize data / reference points where possible - hope you understand.

1 Like

HI @martynross85,

Glad you got it working. Have a great day… :slight_smile:

Thanks
Mukesh

Hi @martynross85, a response on this post has been tagged as “Solution”. If you have a follow question or concern related to this topic, please remove the Solution tag first by clicking the three dots beside Reply and then untick the check box. Thanks!