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
annalisekerr
Regular Visitor

Calculating number of working days between TODAY and End Date (where TODAY is before/after End Date)

Hi, I am trying to add a new colum to a table which calculates the number of days between a particular (end) date and today.  My problem is that some of the dates in the end date, is before todays date so I get an #ERROR message.

 

Ideally i'd like

If the End date is > than todays date, return the number of days

If the End Date is < than todays date, return a -number of days

 

I have tried to use the following 

annalisekerr_1-1647019323929.png

 

Please can anyone help??  I'm still very new to Power BI and DAX so still finding my feet 🤗

 
2 ACCEPTED SOLUTIONS

Hi @annalisekerr ,

 

I have a test tamerj1 's code and I find there is something wrong in it. It will show error : The start date in Calendar function can not be later than the end date.

1.png

Update Code:

Workdays between today and end date = 
VAR _CheckCondition = 'Iterations (2)'[Correct End Date] > 'Iterations (2)'[Todays Date]
VAR _MinDate =
    IF (
        _CheckCondition ,
        'Iterations (2)'[Todays Date],
        'Iterations (2)'[Correct End Date]
    )
VAR _MaxDate =
    IF (
        _CheckCondition ,
        'Iterations (2)'[Correct End Date],
        'Iterations (2)'[Todays Date]
    )
VAR _Result =
    COUNTROWS (
        FILTER (
            ADDCOLUMNS (
                CALENDAR ( _MinDate, _MaxDate ),
                "Day Of Week", WEEKDAY ( [Date], 2 )
            ),
            NOT ( [Day Of Week] IN { 6, 7 } )
        )
    )
RETURN
    IF (_CheckCondition,_Result,-_Result)

Result is as below.

1.png

 

Best Regards,
Rico Zhou

 

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

Thank you for the correction. I've created the variables but missed to use them 🙂 I will update my reply accordingly

View solution in original post

8 REPLIES 8
tamerj1
Super User
Super User

Hi @annalisekerr 
One way to that:

 

 

Workdays between today and end date =
VAR CheckCondition = 'Iterations (2)'[Correct End Date] > 'Iterations (2)'[Todays Date]
VAR MinDate =
    IF (
        CheckCondition ,
        'Iterations (2)'[Todays Date],
        'Iterations (2)'[Correct End Date]
    )
VAR MaxDate =
    IF (
        CheckCondition ,
        'Iterations (2)'[Correct End Date],
        'Iterations (2)'[Todays Date]
    )
VAR Result =
    COUNTROWS (
        FILTER (
            ADDCOLUMNS (
                CALENDAR ( MinDate , MaxDate  ),
                "Day Of Week", WEEKDAY ( [Date], 2 )
            ),
            NOT ( [Day Of Week] IN { 6, 7 } )
        )
    )
RETURN
    IF ( CheckCondition , Result, - Result )

 

 

 

Thank you , I will give this a try

Whitewater100
Solution Sage
Solution Sage

Hi:

Can you try:

Calc Col =
var venddate = TableName[End Date]
var vthisday = TODAY()
return
DATEDIFF(venddate, vthisday,DAY)
 
it will give pos & neg numbers. 

Thank you , I will give this a try

Hi @annalisekerr ,

 

I have a test tamerj1 's code and I find there is something wrong in it. It will show error : The start date in Calendar function can not be later than the end date.

1.png

Update Code:

Workdays between today and end date = 
VAR _CheckCondition = 'Iterations (2)'[Correct End Date] > 'Iterations (2)'[Todays Date]
VAR _MinDate =
    IF (
        _CheckCondition ,
        'Iterations (2)'[Todays Date],
        'Iterations (2)'[Correct End Date]
    )
VAR _MaxDate =
    IF (
        _CheckCondition ,
        'Iterations (2)'[Correct End Date],
        'Iterations (2)'[Todays Date]
    )
VAR _Result =
    COUNTROWS (
        FILTER (
            ADDCOLUMNS (
                CALENDAR ( _MinDate, _MaxDate ),
                "Day Of Week", WEEKDAY ( [Date], 2 )
            ),
            NOT ( [Day Of Week] IN { 6, 7 } )
        )
    )
RETURN
    IF (_CheckCondition,_Result,-_Result)

Result is as below.

1.png

 

Best Regards,
Rico Zhou

 

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

 

Perfect, yes that works brilliantly... thank you 

@annalisekerr 

Well, that was my solution.

Thanks anyway.
Good luck and have a great day!

Thank you for the correction. I've created the variables but missed to use them 🙂 I will update my reply accordingly

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.

Top Solution Authors