Forum Discussion

labuser1235's avatar
labuser1235
Icon for Helper IV rankHelper IV
6 years ago
Solved

Date difference for working days excluding sundays

Hi All,

 

I would like to created a calculated column where it calculates date difference between two columns and return if it falls under 0-24 hrs or 24-48 hrs or 48-72 hrs or above 72hrs. Excluding only Sundays.

Table:

Start DateFruit NameTypePrice SoldEnd Date
01-31-2020OrangeType-2702-01-2020
01-31-2020Apple Type-3802-01-2020
01-31-2020GrapesType-1202-03-2020
01-31-2020OrangeType-1502-03-2020
01-31-2020OrangeType-31002-03-2020
01-31-2020Apple Type-1302-01-2020
01-31-2020GrapesType-2402-01-2020
01-31-2020GrapesType-3602-03-2020
01-31-2020OrangeType-1501-31-2020
01-31-2020OrangeType-2701-31-2020
01-31-2020Apple Type-3801-31-2020
01-31-2020GrapesType-1201-31-2020
01-30-2020OrangeType-1502-03-2020
01-30-2020OrangeType-31002-03-2020
01-30-2020Apple Type-1302-03-2020
01-30-2020GrapesType-2402-01-2020
01-30-2020GrapesType-3602-01-2020
01-30-2020OrangeType-1502-01-2020

 

Expected Output:

Start DateFruit NameTypePrice SoldEnd DateExpected Output
01-31-2020OrangeType-2702-01-202024 to 48 Hrs
01-31-2020Apple Type-3802-01-202024 to 48 Hrs
01-31-2020GrapesType-1202-03-202048 to 72 Hrs
01-31-2020OrangeType-1502-03-202048 to 72 Hrs
01-31-2020OrangeType-31002-03-202048 to 72 Hrs
01-31-2020Apple Type-1302-01-202024 to 48 Hrs
01-31-2020GrapesType-2402-01-202024 to 48 Hrs
01-31-2020GrapesType-3602-03-202048 to 72 Hrs
01-31-2020OrangeType-1501-31-20200 to 24 Hrs
01-31-2020OrangeType-2701-31-20200 to 24 Hrs
01-31-2020Apple Type-3801-31-20200 to 24 Hrs
01-31-2020GrapesType-1201-31-20200 to 24 Hrs
01-30-2020OrangeType-1502-03-2020More than 72 Hrs
01-30-2020OrangeType-31002-03-2020More than 72 Hrs
01-30-2020Apple Type-1302-03-2020More than 72 Hrs
01-30-2020GrapesType-2402-01-202048 to 72 Hrs
01-30-2020GrapesType-3602-01-202048 to 72 Hrs
01-30-2020OrangeType-1502-01-202048 to 72 Hrs
  • Hi labuser1235 ,

     

    In your scenario, we should create a date table like that.

    date = ADDCOLUMNS(CALENDARAUTO(),"Sunday",IF(WEEKDAY([Date],2) = 7,1,0))

     

    Then we can create a calculated column in our fact table as below.

    Column = 
    VAR workingdays =
        CALCULATE (
            COUNTROWS ( 'date' ),
            FILTER (
                'date',
                'date'[Date] > 'Table'[Start Date]
                    && 'date'[Date] <= 'Table'[End Date]
                    && 'date'[Sunday] <> 1
            )
        )
    RETURN
        SWITCH (
            TRUE (),
            workingdays = 1, "0 to 24 Hrs",
            workingdays = 2, "24 to 48 Hrs",
            workingdays = 3, "48 to 72 Hrs",
            "More that 62 Hrs"
        )
    

     

    For more details, please check the pbix as attached.

     

7 Replies

  • Hi labuser1235, sounds like an if formula is needed here.

     

    https://docs.microsoft.com/en-us/dax/if-function-dax

     

    =if ( datediff ([StartDate],[EndDate],HOUR) < 24, "Less Than 24 Hours",
    if (datediff ([StartDate],[EndDate],HOUR)>=24 and datediff ([StartDate],[EndDate],HOUR)<48,"24-48 Hours",
    if (datediff ([StartDate],[EndDate],HOUR)>=48 and datediff ([StartDate],[EndDate],HOUR)<72,"48-72 Hours","More Than 72 Hours")

     

    Alternatively you could change the datediff to days, which would return whole integers (0,1,2,3 etc) that could be used in a SWITCH function. you can read more, here;

     

    https://docs.microsoft.com/en-us/dax/switch-function-dax

     

    Does that help?

  • I've just realised you want Sundays excluded also. Do you have a Date Dimensions table in use?

     

    If so, you can add an additional column, IsWeekDay, using the SWITCH function;

    IsWeekDay =
    SWITCH (
        WEEKDAY ( [Date] ),
        1, 0,
        1
    )

     This basically says if weekday is 1 (Sunday) then 0 (false), else 1 (True), which can be referenced in a second measure;

    =
    CALCULATE (
        SUM ( DateTable[IsWorkday] ),
        DATESBETWEEN (
            DateTable[Date],
            'Source'[StartDate],
            'Source'[EndDate]
        )
    )

    Finally, this can be referenced in the original If formula or a new SWITCH to convert the integers into your string for binning.

     

    I hope that helps a little more for you.

  • v-frfei-msft's avatar
    v-frfei-msft
    Icon for Community Support rankCommunity Support

    Hi labuser1235 ,

     

    In your scenario, we should create a date table like that.

    date = ADDCOLUMNS(CALENDARAUTO(),"Sunday",IF(WEEKDAY([Date],2) = 7,1,0))

     

    Then we can create a calculated column in our fact table as below.

    Column = 
    VAR workingdays =
        CALCULATE (
            COUNTROWS ( 'date' ),
            FILTER (
                'date',
                'date'[Date] > 'Table'[Start Date]
                    && 'date'[Date] <= 'Table'[End Date]
                    && 'date'[Sunday] <> 1
            )
        )
    RETURN
        SWITCH (
            TRUE (),
            workingdays = 1, "0 to 24 Hrs",
            workingdays = 2, "24 to 48 Hrs",
            workingdays = 3, "48 to 72 Hrs",
            "More that 62 Hrs"
        )
    

     

    For more details, please check the pbix as attached.