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

Calculate number of days with IF condition

Hello.

 

I'm relatively new to PBI and DAX language and I'm having some difficulties finding a way to calculate the number of days when a certain condition happens.

 

In order to make it easier to understand, I'll post an example of what I'm trying to calculate.

 

For this example, I'm just using data for 1 process, but the excel has many more "Process_ID's" and subsequent information.

 

Process_IDAppointment_IDAppointment_DateIncapacity_ID
221673118960609/01/2019ITA
221673118960814/01/2019ITA
221673118961118/01/2019ITA
221673118992421/01/2019ITP
221673119166523/01/2019ITA
221673119302505/02/2019ITP
221673120031308/02/2019SI
221673120172912/02/2019ITA
221673121991517/02/2019ITP
221673130480625/02/2019ACSD

 

So, I need to calculate the number of days when the incapacity_ID is "ITA", but as you can see, there are a few dates in between that have "incapacity_ID" different than "ITA".


For this particular example, the final result should be 30 days of ITA... since that the first 3 appointments were given "ITA" we already have 12 days for this period (between the first date - 9/01/2019 - and the date where the ITA "ends" - 21/01/2019).

Then...on the 5th appointment, we got another ITA so this would add 13 more days (difference between the date ITA starts and ends, in this case starting date is 23/01/2019 and end date 05/02/2019). So, right now, we have 12 days from the first 3 appointments and 13 days from the 5th appointment.

Then, on 8th apppointment you can see that there is once again the ITA incapacity, so using the same logic as before, this would add up 5 more days ( days between 12/2/2019 and 17/02/2019),  resulting in a total of 30 days of ITA.

 

Now, my question is if there is any way I could write a DAX formula that makes this calculation automatically and for many more "Process_ID's"?

 

Thanks in advance.

2 ACCEPTED SOLUTIONS
smpa01
Super User
Super User

@Anonymous  I think I might have what you need

 

If you have a data like this

 

Process_ID Appointment_ID Appointment_Date Incapacity_ID
221673 1189606 1/9/2019 ITA
221673 1189608 1/14/2019 ITA
221673 1189611 1/18/2019 ITA
221673 1189924 1/21/2019 ITP
221673 1191665 1/23/2019 ITA
221673 1193025 2/5/2019 ITP
221673 1200313 2/8/2019 SI
221673 1201729 2/12/2019 ITA
221673 1219915 2/17/2019 ITP
221673 1304806 2/25/2019 ACSD
221674 1189606 1/9/2019 ITA
221674 1189608 1/14/2019 ITA
221674 1189611 1/18/2019 ITA
221674 1189924 1/21/2019 ITP
221674 1191665 1/23/2019 ITA
221674 1193025 2/5/2019 ITP
221674 1200313 2/8/2019 SI
221674 1201729 2/12/2019 ITA
221674 1219915 2/17/2019 ITA
221674 1304806 2/25/2019 ITA

 

You can get the output by both as a calculated table or a measure following the attached.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can add an index column first.

test_index_0302.PNG

Use the if statement to calculate the next date corresponding to "ITA".

IS_ita = IF(MAX('Table'[Incapacity_ID])="ITA",CALCULATE(FIRSTNONBLANK('Table'[Appointment_Date],1),FILTER(ALLEXCEPT('Table','Table'[Process_ID]),'Table'[Index]=SELECTEDVALUE('Table'[Index])+1)))
 
Then use "DateDiff" to calculate the number of days between two dates:
datediff_ = DATEDIFF(MAX('Table'[Appointment_Date]),[IS_ita],DAY)
 
For more details,please refer to the pbix.

 

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can add an index column first.

test_index_0302.PNG

Use the if statement to calculate the next date corresponding to "ITA".

IS_ita = IF(MAX('Table'[Incapacity_ID])="ITA",CALCULATE(FIRSTNONBLANK('Table'[Appointment_Date],1),FILTER(ALLEXCEPT('Table','Table'[Process_ID]),'Table'[Index]=SELECTEDVALUE('Table'[Index])+1)))
 
Then use "DateDiff" to calculate the number of days between two dates:
datediff_ = DATEDIFF(MAX('Table'[Appointment_Date]),[IS_ita],DAY)
 
For more details,please refer to the pbix.

 

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thank you so much!

Both solutions worked!  (from @smpa01  and @V-lianl-msft )

 

I found the solution from @V-lianl-msft a bit easier to understand though (probably because I'm still kinda new on PBI), nevetheless, I was wondering if you guys could help me with one more question....

 

Let's take the same example, but with a few differences...Let's say I have the following data:

 

221673118960609/01/2019ITA
221673118960814/01/2019ITA
221673118961118/01/2019ITA
221673118992421/01/2019ITP
221673119166523/01/2019ITA
221673119302505/02/2019ITP
221673120031308/02/2019SI
221673120172912/02/2019ITA
221673121991517/02/2019ITP
221673130480625/02/2019ACSD
221673138969703/03/2019ITA
221673147458807/03/2019SI
221673155947915/03/2019ACSD

 

As you can see, on 25/2/2019 there's and incapacity_ID named "ACSD"....Is there any way to calculate the number of days of ITA incapacity untill the first ASCD incapacity ID shows up? In this case we would have something like 30 days until the first ACSD  and 4 days until the second ASCD.

 

Basically, everytime I get an "ACSD", I need the calculation of ITA days to be reseted.

 

Is that possible?

 

Thanks once again for the help!

smpa01
Super User
Super User

@Anonymous  I think I might have what you need

 

If you have a data like this

 

Process_ID Appointment_ID Appointment_Date Incapacity_ID
221673 1189606 1/9/2019 ITA
221673 1189608 1/14/2019 ITA
221673 1189611 1/18/2019 ITA
221673 1189924 1/21/2019 ITP
221673 1191665 1/23/2019 ITA
221673 1193025 2/5/2019 ITP
221673 1200313 2/8/2019 SI
221673 1201729 2/12/2019 ITA
221673 1219915 2/17/2019 ITP
221673 1304806 2/25/2019 ACSD
221674 1189606 1/9/2019 ITA
221674 1189608 1/14/2019 ITA
221674 1189611 1/18/2019 ITA
221674 1189924 1/21/2019 ITP
221674 1191665 1/23/2019 ITA
221674 1193025 2/5/2019 ITP
221674 1200313 2/8/2019 SI
221674 1201729 2/12/2019 ITA
221674 1219915 2/17/2019 ITA
221674 1304806 2/25/2019 ITA

 

You can get the output by both as a calculated table or a measure following the attached.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

@Anonymous did you try the solution yet. Please let me know if this is what you are looking for.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Greg_Deckler
Super User
Super User

Well, what I would do would be to create a calculated column using EARLIER like in my MTBF example. See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...


@ 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

I tried using the formula you used on your post, but it's not working. It's returning a syntax error.

I download the data set you shared in the example given and tried to apply the formula in there but it's also returning syntax error. 

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.