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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Nested IF formula optimization

Maybe it's obvious but somehow I got stuck with this formula. I'm convinced there is a better way to formulate this, but just can't figure it out. To make it worse, it doesn't work correctly Smiley Frustrated

 

The measure looks at the last modified date of a source file and returns a status.

- if it's more than 7 days (168 hours) old, the file has been retired.

- on a Saturday, if it's more than 48 hours old, it's missing

- on a Sunday, if it's more than 72 hours old, it's missing

- on a working day, if it's more than 24 hours old, it's missing

else

- if it was modified before 10 AM, it's OK

- if it was modified before 12 PM, it's pending

- if it was modified after 12 AM, it's delayed

 

Ok so my

question #1: Is there a better way of writing a formula that gives the desired results

question #2: Even if it's older than 24 hours on a workday, if it's last modified date is <10 AM or <12 PM, it still returns either OK or pending, instead of missing. What's wrong? Thanks for any help!

 

 

backlogHealth =
IF (
    DATEDIFF ( backlogRecord[Date modified], NOW (), HOUR ) > 168,
    "Retired",
    IF (
        WEEKDAY ( NOW () ) = 7,
        IF (
            DATEDIFF ( backlogRecord[Date modified], NOW (), HOUR ) > 48,
            "Missing",
            IF (
                HOUR ( backlogRecord[Date modified] ) < 10,
                "OK",
                IF ( HOUR ( backlogRecord[Date modified] ) < 12, "Pending", "Delayed" )
            )
        ),
        IF (
            WEEKDAY ( NOW () ) = 1,
            IF (
                DATEDIFF ( backlogRecord[Date modified], NOW (), HOUR ) > 72,
                "Missing",
                IF (
                    HOUR ( backlogRecord[Date modified] ) < 10,
                    "OK",
                    IF ( HOUR ( backlogRecord[Date modified] ) < 12, "Pending", "Delayed" )
                )
            ),
            IF (
                DATEDIFF ( backlogRecord[Date modified], NOW (), HOUR ) > 24,
                "Missing",
                IF (
                    HOUR ( backlogRecord[Date modified] ) < 10,
                    "OK",
                    IF ( HOUR ( backlogRecord[Date modified] ) < 12, "Pending", "Delayed" )
                )
            )
        )
    )
)

 

 

2 ACCEPTED SOLUTIONS
Greg_Deckler
Community Champion
Community Champion

One thing you could do would be to define this variable:

 

VAR LastModifiedDuration = backlogRecord[Date modified], NOW (), HOUR )

 

Also, I would switch to a SWITCH statement.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

Anonymous
Not applicable

@Greg_Deckleryes it did the trick! Thank you. Here's my new formula for that calculated column:

 

backlogHealth =
VAR hoursElapsed =
    DATEDIFF ( backlogRecord[Date modified], NOW (), HOUR )
RETURN
    SWITCH (
        TRUE (),
        hoursElapsed > 168, "Retired",
        WEEKDAY ( NOW () ) = 7
            && hoursElapsed > 48, "Missing",
        WEEKDAY ( NOW () ) = 1
            && hoursElapsed > 72, "Missing",
        WEEKDAY ( NOW () ) < 7
            && hoursElapsed > 24, "Missing",
        HOUR ( backlogRecord[Date modified] ) < 10, "OK",
        HOUR ( backlogRecord[Date modified] ) < 12, "Pending",
        "Delayed"
    )

View solution in original post

5 REPLIES 5
Greg_Deckler
Community Champion
Community Champion

One thing you could do would be to define this variable:

 

VAR LastModifiedDuration = backlogRecord[Date modified], NOW (), HOUR )

 

Also, I would switch to a SWITCH statement.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

Wondering if you can offer input to a similar scenario. I have a measure using nested if's that referene 3 other measures and 3 different fields, so a SWITCH won't work (to my knowledge). Independantly, the measures perform fine, but in the nested if they choke.

Any feedback or suggestions would be great.

Thank you.

 

IF(
    [Work Order Invoice Total] > 0
    ,[Work Order Invoice Total]
    ,IF(
        [Work Order ISP Charge] > 0
        ,[Work Order ISP Charge] * 1.3
        ,IF(
            [Work Order Client NTE Total] > 0
            ,[Work Order Client NTE Total]
            ,350
        )
    )
)
Anonymous
Not applicable

I would use the nested if to calculate a column:

 

IF(
    [Work Order Invoice Total] > 0
    ,"A"
    ,IF(
        [Work Order ISP Charge] > 0
        ,"B"
        ,IF(
            [Work Order Client NTE Total] > 0
            ,"C"
            ,"D"
        )
    )
)

 Now you can use SWITCH based on A, B, C, D values in the calculated column.

Anonymous
Not applicable

Thanks for your prompt reply @Greg_Deckler!

 

SWITCH will likely do the trick. I was looking for CASE but couldn't find the DAX equivalent. Now I think I got it. Will let you know as soon as I get a chance to test it.

Anonymous
Not applicable

@Greg_Deckleryes it did the trick! Thank you. Here's my new formula for that calculated column:

 

backlogHealth =
VAR hoursElapsed =
    DATEDIFF ( backlogRecord[Date modified], NOW (), HOUR )
RETURN
    SWITCH (
        TRUE (),
        hoursElapsed > 168, "Retired",
        WEEKDAY ( NOW () ) = 7
            && hoursElapsed > 48, "Missing",
        WEEKDAY ( NOW () ) = 1
            && hoursElapsed > 72, "Missing",
        WEEKDAY ( NOW () ) < 7
            && hoursElapsed > 24, "Missing",
        HOUR ( backlogRecord[Date modified] ) < 10, "OK",
        HOUR ( backlogRecord[Date modified] ) < 12, "Pending",
        "Delayed"
    )

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors