Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago

return text based off calender date

Hello.

 

I am new to Power Bi and need some help.  I have a data pull from excel that shows a due date for each line item.  Based off that due date and looking at a calendar, I am able to identify the status of that item.  For example:  Assume today's date is 10-25-23.  Anything prior to today is considered Past Due.  Anything with a due date of today is considered Due Today.  Anything due in the next two days (making sure to account for weekends and holidays) is Due in Two Days.  Anything further out than that is On track.

 

My goal is to eventually have the data auto pull from the website and update the dashboard automatically.  Is there a way to create a column in Power BI to auto update the status based off the due date and keeping weekends / holidays in mind?  I came up with a nested formual in excel, but think I am making this much harder than it needs to be.  Also, my calculation would have to be updated constantly to account for weekends / holidays.  At that point it was easier to just manually make the updates as I was concened about it returning the correct response.

 

Can someone please help?

3 Replies

  • Anonymous, this is a fairly standard problem. The way to solve it would be to create a Date dimension in your Power BI report. The Date dimension can have a column (e.g. IsWorkingDay) that indicates is a day is a working day or not. How you populate that column will depend on the needs of your solution. Having a Date dimension in place will make it easier to do many other time-based calculations.

     

    Once you have created a Date dimension, you can then add a Calculated Column to your source data to work out what the DueStatus for each row is. The logic in this calculation will use today's date, the DueDate, and the Date dimension's IsWorkingDay flag to produce the Past Due, Due Today, Due in Two Days, On Track, or whichever other statuses are needed.

     

    A basic demonstration of the Calculated Column, without using a Date dimension, is shown below.

     

     

    DueStatus = 
        VAR daysdiff = DATEDIFF(TODAY(), YourTable[DueDate], DAY)
        RETURN
        SWITCH(TRUE(),
            daysdiff < 0, "Past Due",
            daysdiff = 0, "Due Today",
            daysdiff <= 2, "Due in Two Days",
            "On Track"
        )

     

     

    I am using  this test data for YourTable:

    DueDate
    23 Oct 2023
    24 Oct 2023
    25 Oct 2023
    26 Oct 2023
    27 Oct 2023
    28 Oct 2023
    29 Oct 2023
    30 Oct 2023

     

    And this is the output:

     

    Is this the sort of thing you are looking for?

  • Anonymous, further to my first reply, I have created a solution which takes working days into account.

     

    Create a Date dimension table which has a WorkingDays column as an integer. For working days set this to 1, for non-working days set it to 0. In my example I used this data for October:

     

    Date   WorkingDays

    01-Oct-23      0
    02-Oct-231
    03-Oct-231
    04-Oct-231
    05-Oct-231
    06-Oct-231
    07-Oct-230
    08-Oct-230
    09-Oct-231
    10-Oct-231
    11-Oct-231
    12-Oct-231
    13-Oct-231
    14-Oct-230
    15-Oct-230
    16-Oct-231
    17-Oct-231
    18-Oct-231
    19-Oct-231
    20-Oct-231
    21-Oct-230
    22-Oct-230
    23-Oct-231
    24-Oct-231
    25-Oct-231
    26-Oct-231
    27-Oct-231
    28-Oct-230
    29-Oct-230
    30-Oct-231
    31-Oct-231

     

    Then in your data table, create a Calculated Column called WorkingDaysTillDueDate with this DAX expression:

     

     

     

     

    WorkingDaysTillDueDate = 
        VAR duedate = YourTable[DueDate]
        VAR today = TODAY()
        RETURN
            SWITCH(TRUE(),
                duedate = today, 0,
                duedate < today,
                CALCULATE(
                    0 - SUM(dimDate[WorkingDays]),
                    dimDate[Date] >= duedate,
                    dimDate[Date] < today
                ),
                duedate > today,
                CALCULATE(
                    SUM(dimDate[WorkingDays]),
                    dimDate[Date] > today,
                    dimDate[Date] <= duedate
                )
            )

     

     

     

     

     

    Now create another Calculated Column called DueStatus with this DAX expression:

     

     

     

     

    DueStatus = 
        VAR days = YourTable[WorkingDaysTillDueDate]
        RETURN
            SWITCH(TRUE(),
                days < 0, "Past Due",
                days = 0, "Due Today",
                days <= 2, "Due in Two Days",
                "On Track"
            )

     

     

     

     

     

    When I run this today (25 Oct 2023) it gives me the following results for dates from 23 Oct to 30 Oct. And whenever you refresh the report, it will recalculate both Calculated Columns based on the current today's date.

     

    You will need to maintain the dimDate table by populating it with any future dates and what the WorkingDays field should be for each.

     

    Hopefully this helps.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you very much for your help!!  I have been able to create the calculation in excel to produce the correct Date Status.  I have a report in Power Bi that I have recently created that is linked to a SP site and updates automatically.  My next goal is to figure out how to add this new calculation / rows to the current report so that I can have the details linked.  I am viewing videos online to learn how to do this (very new to Power BI).  Fingers crossed I can update this soon.  🙂  Again, your help is very much appreciated.