简体   繁体   中英

I'm currently trying to calculate the difference between two dates in sales table excluding weekend in power BI

Can anyone help please? I'm currently trying to calculate the difference between two dates in sales table excluding weekend in power BI sample data

在此处输入图像描述

I created a calculated column but I think you could create a Measure as well:

Business_Days =

    --SUNDAY(1) // MONDAY(2) // ... // SATURDAY(7)

    VAR Case_1 = WEEKDAY([Start Date]) = 7 && WEEKDAY([End Date]) = 7
    VAR Case_2 = WEEKDAY([Start Date]) = 7 && WEEKDAY([End Date]) = 1
    VAR Case_3 = WEEKDAY([Start Date]) = 1 && WEEKDAY([End Date]) = 7
    VAR Case_4 = WEEKDAY([Start Date]) = 1 && WEEKDAY([End Date]) = 1
    VAR Case_5 = WEEKDAY([Start Date]) = 1 && WEEKDAY([End Date]) IN {2,3,4,5,6}
    VAR Case_6 = WEEKDAY([Start Date]) = 7 && WEEKDAY([Start Date]) IN {2,3,4,5,6}
    VAR Case_7 = WEEKDAY([Start Date]) IN {2,3,4,5,6} && WEEKDAY([End Date]) = 1
    VAR Case_8 = WEEKDAY([Start Date]) IN {2,3,4,5,6} && WEEKDAY([End Date]) = 7
    
    RETURN

    IF( Case_1 , DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK) + n1,
    IF( Case_2 , DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK) + n2,
    IF( Case_3 , DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK) + n3,
    IF( Case_4 , DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK) + n4,
    IF( Case_5 , DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK) + n5,
    IF( Case_6 , DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK) + n6,
    IF( Case_7 , DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK) + n7,
    IF( Case_8 , DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK) + n8,
    DATEDIFF([Start Date],[End Date],DAY)-2*DATEDIFF([Start Date],[End Date],WEEK))))))))

Comments:

  • n1, n2, n3... are integers. You have to replace them by their real value.
  • There may be more special cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM