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

How to calculate duration by dates

Hi,

Can you help me? 🙂

 

My data in the "Procurement" table:

TitleScheduled end dateEnd date
PC2020-06-022020-08-03
Monitor2020-07-032020-07-03
PS42020-08-28 

 

The following must be calculated:

- Check that the end date is entered. If yes - calculate whether there is a delay. If not - take a date today and calculate if there is a delay. If there is a delay - show in new field 1, if not - BLANK (). 

- Show in next field: if there is a delay - show how many working days are delayed. Calculate Networkdays by estimating all holidays. Holidays are stored in my another table.

 

In this case, the result should look like this:

TitleScheduled end dateEnd dateDelayDays
PC2020-06-022020-08-03143*
Monitor2020-07-032020-07-03  
PS42020-08-28 17**

*43, because 2020-06-24, 2020-07-06 days are holidays.

**7, because today is 2020-09-07. There will be a day more every day: tomorrow - 8, etc.

7 REPLIES 7
v-stephen-msft
Community Support
Community Support

Hi @Anonymous ,

 

1.Create a calendar table. There is no relationship between two tables.

Date = 
ADDCOLUMNS (
    CALENDAR ( DATE ( 2020, 6, 1 ), DATE ( 2020, 9, 30 ) ),
    "Holiday",
        IF (
            WEEKDAY ( [Date], 2 ) = 6
                || WEEKDAY ( [Date], 2 ) = 7
                || [Date] = DATE ( 2020, 6, 24 )
                || DATE ( 2020, 7, 6 ) = [Date],
            "Yes",
            "No"
        )
)

1.png2.png

 

2.Create measures.

Delay =
IF (
    MAX ( 'Procurement'[End date] ) = MAX ( 'Procurement'[Scheduled end date] ),
    BLANK (),
    1
)
Days =
VAR t1 =
    CALCULATE (
        COUNTROWS ( 'Date' ),
        FILTER (
            'Date',
            [Date] <= MAX ( 'Procurement'[End date] )
                && [Date] >= MAX ( 'Procurement'[Scheduled end date] )
                && [Holiday] = "No"
        )
    )
VAR t2 =
    CALCULATE (
        COUNTROWS ( 'Date' ),
        FILTER (
            'Date',
            [Date] <= TODAY ()
                && [Date] >= MAX ( 'Procurement'[Scheduled end date] )
                && [Holiday] = "No"
        )
    )
RETURN
    SWITCH (
        TRUE (),
        [Delay] = BLANK (), BLANK (),
        [Delay] = 1
            && MAX ( 'Procurement'[End date] ) <> BLANK (), t1,
        [Delay] = 1
            && MAX ( 'Procurement'[End date] ) = BLANK (), t2
    )

 

3.The result is as follows.

3.png

 

 

You can check more details from here.

 

 

 

Best Regards,

Stephen Tao

 

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

Anonymous
Not applicable

@v-stephen-msft , thanks, but not everything is clear to me.
I will explain what I have now calculated and what I need.

I have already created two tables: Procurement and Holidays. I have also calculated the delay using measure.

 

Procurement table:

TitleScheduled end dateEnd date
PC2020-06-022020-08-03
Monitor2020-07-032020-07-03
PS42020-08-28 

 

Holidays table:

DateValue
2020-01-011
2020-03-011
2020-04-131
etc... 

 

Delay (I don't know if it's correct, or no, because I'm using two logic in this formula):

Delay = IF(('Procurement'[Scheduled end date])>=('Procurement'[End date]) || ('Procurement'[Procurement are made on schedule] ) = 1;BLANK();1)

 

I need to calculate:

- if I don't have an end date - how can I calculate the delay until today?

- and how can I calculate days? I need to show how many working days are delayed? I calculated Networkdays in another table. It may be possible to make a similar calculation: 

 

NetworkDays =
Var TBL_Date=CALENDAR(IF('Application card'[Start date] = BLANK();'Application card'[Creation date];'Application card'[Start date]);'Application card'[Registration date])
var TBL_FinalDate=ADDCOLUMNS(TBL_Date;"Working day";IF(WEEKDAY([Date];2)>=6;0;1);"Holiday";IFERROR(LOOKUPVALUE('Holidays'[Value];'Holidays'[Date];[Date]);0))
return
SUMX(TBL_FinalDate;if([Working day]=1&&[Holiday]=0;1;0))

Hi @Anonymous ,

 

You have given a special holiday table. You can try to use the RELATED function to reference the columns of the holiday table in your calendar table, provided that the two tables have a relationship.

 

1.Calendar table. And there is a relationship between Calendar table and Holidays table.

Calendar = CALENDAR ( MIN('Procurement'[Scheduled end date]),TODAY() )

9.png

 

2.Create calculated columns in Calendar table. The Weekday column returns a number from 1 to 7,week begins on Monday (1) and ends on Sunday (7). The Weekend/Holiday column returns 1 if the date is a weekend or holiday, otherwise it returns 0.

Holiday = RELATED(Holidays[Date])
Weekday = WEEKDAY([Date],2)
Weekend/Holiday = IF([Weekday]=6||[Weekday]=7||[Holiday]=[Date],1,0)

10.png

 

3.According to your explanation, I modified the Delay measure. Since I don’t have a [Procurement are made on schedule] column, I use the End Date column as the judgment condition.

Delay =
IF (
    MAX ( 'Procurement'[Scheduled end date] ) < MAX ( 'Procurement'[End date] )
        || MAX ( 'Procurement'[End date] ) = BLANK (),
    1,
    BLANK ()
)

 

4.t1 is the number of days from the scheduled end date to the end date and excluding weekends and holidays.

   t2 is the number of days from the scheduled end date to today and excluding weekends and holidays.

   Then call t1 or t2 according to the return value of the Delay measure and whether the End date column is empty as the judgment condition.

Days =
VAR t1 =
    CALCULATE (
        COUNTROWS ( 'Calendar' ),
        FILTER (
            'Calendar',
            [Date] <= MAX ( 'Procurement'[End date] )
                && [Date] >= MAX ( 'Procurement'[Scheduled end date] )
                && [Weekend/Holiday] = 0
        )
    )
VAR t2 =
    CALCULATE (
        COUNTROWS ( 'Calendar' ),
        FILTER (
            'Calendar',
            [Date] <= TODAY ()
                && [Date] >= MAX ( 'Procurement'[Scheduled end date] )
                && [Weekend/Holiday] = 0
        )
    )
RETURN
    SWITCH (
        TRUE (),
        [Delay] = BLANK (), BLANK (),
        [Delay] = 1
            && MAX ( 'Procurement'[End date] ) <> BLANK (), t1,
        [Delay] = 1
            && MAX ( 'Procurement'[End date] ) = BLANK (), t2
    )

 11.png

 

You can check more details from here.

 

Best Regards,
Stephen Tao

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

Anonymous
Not applicable

@v-stephen-msft , thank you. Everything looks good.

I want to ask a few more questions:

The calculation of the delay must include the logic (exception) that I apply to this day, if the column "Procurement is scheduled" is 1, do not apply the delay. For example:

TitleScheduled end dateEnd dateDelayDaysProcurement are made on schedule
PC2020-06-022020-08-03143 
Monitor2020-07-032020-07-03   
PS42020-08-28 17 
TV2020-07-272020-08-03 61

 

And one more question:

In the table, except for that logic at the top, everything counts correctly. However, when I try to compare delays procurement with ongoing procurement or show how much is delay and how many are not delay, it doesn’t count.

table.png

 

 

 

 

procurement.png

 

 

 

 

 

 

 

 

 

not delay.png

 

Also in the Procurement table I see the scheduled end date of the procurement in the future and I see that they are already delay, even though those dates have not yet come, e.g. Scheduled end date: 2020-09-14

Greg_Deckler
Super User
Super User

@Anonymous - I didn't quite get all of that, but you can use Net Work Days to calculate the number of days between scheduled end and end. https://community.powerbi.com/t5/Quick-Measures-Gallery/Net-Work-Days/m-p/367362#M109


@ 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...
amitchandak
Super User
Super User

Anonymous
Not applicable

@amitchandak, I can't open your file, because my PBI Desktop version is older. 

Maybe it's possible to present the content in this file differently, for example in pics? Thank you.

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.