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

Nested If Statement

Hi,

 

I'm trying to achieve the following outcome in Bi using nested If Statements.

 

If a items status is invalid I want to do a datediff formula (This part works) 

If the status is invalid and the cert date is blank instead of a date diff be 0

If the status is not Invalid instead of the date diff be 0

 

This is the Dax Statement I've written. 

 

IF(JEB_GAS[pdf gas cert status (groups)]="Invalid",DATEDIFF(JEB_GAS[pdf cert date],JEB_GAS[index run date].[Date],dAY), IF(JEB_GAS[pdf gas cert status (groups)]="Invalid" && JEB_GAS[pdf cert date].[Date] = "","0","0"))

 

 

 

 

 

 

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

HI @Anonymous ,

I'd like to suggest you use if statement version, it looks simpler than switch function:

Formula =
IF (
    JEB_GAS[pdf gas cert status (groups)] = "Invalid",
    IF (
        JEB_GAS[pdf cert date].[Date] = "",
        "0",
        DATEDIFF ( JEB_GAS[pdf cert date], JEB_GAS[index run date].[Date], DAY )
    ),
    "0"
)

Regards,

Xiaoxin Sheng

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

View solution in original post

6 REPLIES 6
v-shex-msft
Community Support
Community Support

HI @Anonymous ,

I'd like to suggest you use if statement version, it looks simpler than switch function:

Formula =
IF (
    JEB_GAS[pdf gas cert status (groups)] = "Invalid",
    IF (
        JEB_GAS[pdf cert date].[Date] = "",
        "0",
        DATEDIFF ( JEB_GAS[pdf cert date], JEB_GAS[index run date].[Date], DAY )
    ),
    "0"
)

Regards,

Xiaoxin Sheng

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

Hi @Anonymous,

 

you are not saying anything about what's not working.

 

I have rewritten your code slightly, but not sure if it solves anything:

a =
SWITCH (
    TRUE (),
    JEB_GAS[pdf gas cert status (groups)] = "Invalid", DATEDIFF ( JEB_GAS[pdf cert date], JEB_GAS[index run date], DAY ),
    JEB_GAS[pdf gas cert status (groups)] = "Invalid"
        && ISBLANK ( JEB_GAS[pdf cert date] ), 0,
    JEB_GAS[pdf gas cert status (groups)] <> "Invalid", 0,
    0 --else part of the statement
)

 

Cheers,
Sturla

If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

Anonymous
Not applicable

Hi @sturlaws,

 

Thanks for this, I hope this finds you well its working but i'm getting some blanks.

 

The cert status groups originate from these three statuses 

Missing Data = Invalid

Expired = Invalid

Valid = Valid

 

For some odd reason the blanks are only relating to the Missing data records.

 

Would it be easier to amend the DAX to look at each individual status and not the groups to make sure there are no blanks?

sorry, I need more information in order to help you. Can you share a screen shot of where you get your blanks? Or even better share a copy of your report?(only if it doesn't contain sensitive data. If it does, consider making a relevant samle report)

 

Cheers,
Sturla

Anonymous
Not applicable

Hi @sturlaws 

 

I've used your formula which has helped me massively, Please see below.

When I go into the data view when I select the drop down to see all the results in the new column I can select blanks. 

 

I would like those blanks to be zeros but i'm not quite sure why they are showing up as they are part of the Invalid group

 

 

my bad, change the order of the statements to this:

a =
SWITCH (
    TRUE (),
    JEB_GAS[pdf gas cert status (groups)] = "Invalid"
        && ISBLANK ( JEB_GAS[pdf cert date] ), 0,
    JEB_GAS[pdf gas cert status (groups)] = "Invalid", DATEDIFF ( JEB_GAS[pdf cert date], JEB_GAS[index run date], DAY ),
    JEB_GAS[pdf gas cert status (groups)] <> "Invalid", 0,
    0
)

In the Switch-statement, the statements are evaluated from top to bottom, and ends when a statement evaluates to true. Previously, when it the first statement evaluated to true when [pdf gas cert status(groups)]="Invalid", it calculated and returned datediff even if JEB_GAS[pdf cert date] is blank. Now the order is reversed, so it should work. However, you will still get blanks if JEB_GAS[index run date] is blank.

 

-s

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.