Forum Discussion

manoj_0911's avatar
manoj_0911
Icon for Kudo Commander rankKudo Commander
2 years ago
Solved

Dynamic Date Periods in Power BI Table

Hi everyone,

I'm working on a Power BI report that displays call center agent performance metrics (Date Period, Agent Name, accepted calls, not accepted calls, etc.) for different date periods (subhour, hour, day, month,quarter, year). I want the report to be dynamic based on a user selection of the date period. For example, if "Hour" is selected, the table should show the call date and time, and month is selected then the date period should show Month-Year, while other periods should display the date in a different format.

if Sub-Hour is selected then display in this format = May 1 2023 12:30AM
if Hour is selected then display in this format = May 2 2023 2:00AM
if Day is selected then display in this format = 5/2/2023
if Month is selected then display in this format = May-23

My data source has a date/time column (e.g., "IXN_SUBHOUR_DATE"). I'm unsure how to achieve the dynamic formatting for date and time.

Here are my specific questions:

1. How can I format the "Date & Time" column to display call date and time for the "Hour" selection and adjust formatting for other periods?

I've attached a simplified sample of my data for reference (without any sensitive information).

Thank you in advance for your help!

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi manoj_0911 ,

     

    I’d like to acknowledge the valuable input provided by danextian . His initial ideas were instrumental in guiding my approach. However, I noticed that further details were needed to fully understand the issue.

    In my investigation, I took the following steps:

     

    I regret to inform you that I did not see any example data, so I created the following example data myself:

    IXN_SUBHOUR_DATE

    1/1/2024 8:00:00 AM

    1/2/2024 11:00:00 PM

    1/3/2024 12:00:00 PM

    1/3/2024 6:00:00 PM

    1/4/2024 7:00:00 AM

    1/2/2024 6:00:00 AM

    1/4/2024 2:00:00 PM

    1/5/2024 6:00:00 AM

    1/6/2024 8:00:00 AM

    1/7/2024 3:00:00 PM

    1/8/2024 8:00:00 PM

    1/9/2024 1:00:00 PM

     

    I have two different ways to achieve your needs, this is the first one:

     

    Create different calculated columns according to the different date ranges you want to display, as follows:

    Hour = 'Table'[IXN_SUBHOUR_DATE].[Month] & " " & 'Table'[IXN_SUBHOUR_DATE].[Day] & " " & 'Table'[IXN_SUBHOUR_DATE].[Year] & " " & TIME(HOUR('Table'[IXN_SUBHOUR_DATE]),MINUTE('Table'[IXN_SUBHOUR_DATE]),SECOND('Table'[IXN_SUBHOUR_DATE]))
    
    Month = 'Table'[IXN_SUBHOUR_DATE].[Month] & "-" & YEAR('Table'[IXN_SUBHOUR_DATE])
    
    Sub-Hour = 'Table'[IXN_SUBHOUR_DATE].[Month] & " " & 'Table'[IXN_SUBHOUR_DATE].[Day] & " " & 'Table'[IXN_SUBHOUR_DATE].[Year] & " " & TIME(HOUR('Table'[IXN_SUBHOUR_DATE]),MINUTE('Table'[IXN_SUBHOUR_DATE]),SECOND('Table'[IXN_SUBHOUR_DATE]))
    
    Day = MONTH('Table'[IXN_SUBHOUR_DATE]) & "/" & DAY('Table'[IXN_SUBHOUR_DATE]) & "/" & YEAR('Table'[IXN_SUBHOUR_DATE])

     

    I don't see any difference between the display formats of Sub-Hour and Hour from your requirements.

     

    Create a field parameter and select the created calculated column. Remember to check the Slicer option.

     

    The effect of selecting pages from different periods is as follows:

     

    Another way is:

     

    First create a disconnected table containing all the time periods to be selected.

    Period

    Hour

    Sub-Hour

    Month

    Day

     

    Create a measure:

    Measure = 
    VAR SelectedPeriod = SELECTEDVALUE('DatePeriods'[Period], "Day")
    RETURN
    SWITCH(
        SelectedPeriod,
        "Sub-Hour", FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "mmm d yyyy hh:mmAM/PM"),
        "Hour", FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "mmm d yyyy hh:mmAM/PM"),
        "Day", FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "M/D/YYYY"),
        "Month", FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "mmm-yy"),
        FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "M/D/YYYY")
    )

     

    The final page effect is shown below:

     

    Overall, the second method performs better and is more convenient.

     

    The first and third lines are the time taken by the second method, and the other two lines are the time taken by the first line.

     

    Please choose your preferred method according to your needs.

     

    pbix file is attached.

     

    If you have any further questions please feel free to contact me.

     

    Best Regards,
    Yang
    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

3 Replies

  • You can use field parameters to switch between fields. But honestly speaking, datetime will occupy a lot of space in your model  as compared with separate date and time columns. So if you don't need the date and time to be together, just keep them separate.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi manoj_0911 ,

     

    I’d like to acknowledge the valuable input provided by danextian . His initial ideas were instrumental in guiding my approach. However, I noticed that further details were needed to fully understand the issue.

    In my investigation, I took the following steps:

     

    I regret to inform you that I did not see any example data, so I created the following example data myself:

    IXN_SUBHOUR_DATE

    1/1/2024 8:00:00 AM

    1/2/2024 11:00:00 PM

    1/3/2024 12:00:00 PM

    1/3/2024 6:00:00 PM

    1/4/2024 7:00:00 AM

    1/2/2024 6:00:00 AM

    1/4/2024 2:00:00 PM

    1/5/2024 6:00:00 AM

    1/6/2024 8:00:00 AM

    1/7/2024 3:00:00 PM

    1/8/2024 8:00:00 PM

    1/9/2024 1:00:00 PM

     

    I have two different ways to achieve your needs, this is the first one:

     

    Create different calculated columns according to the different date ranges you want to display, as follows:

    Hour = 'Table'[IXN_SUBHOUR_DATE].[Month] & " " & 'Table'[IXN_SUBHOUR_DATE].[Day] & " " & 'Table'[IXN_SUBHOUR_DATE].[Year] & " " & TIME(HOUR('Table'[IXN_SUBHOUR_DATE]),MINUTE('Table'[IXN_SUBHOUR_DATE]),SECOND('Table'[IXN_SUBHOUR_DATE]))
    
    Month = 'Table'[IXN_SUBHOUR_DATE].[Month] & "-" & YEAR('Table'[IXN_SUBHOUR_DATE])
    
    Sub-Hour = 'Table'[IXN_SUBHOUR_DATE].[Month] & " " & 'Table'[IXN_SUBHOUR_DATE].[Day] & " " & 'Table'[IXN_SUBHOUR_DATE].[Year] & " " & TIME(HOUR('Table'[IXN_SUBHOUR_DATE]),MINUTE('Table'[IXN_SUBHOUR_DATE]),SECOND('Table'[IXN_SUBHOUR_DATE]))
    
    Day = MONTH('Table'[IXN_SUBHOUR_DATE]) & "/" & DAY('Table'[IXN_SUBHOUR_DATE]) & "/" & YEAR('Table'[IXN_SUBHOUR_DATE])

     

    I don't see any difference between the display formats of Sub-Hour and Hour from your requirements.

     

    Create a field parameter and select the created calculated column. Remember to check the Slicer option.

     

    The effect of selecting pages from different periods is as follows:

     

    Another way is:

     

    First create a disconnected table containing all the time periods to be selected.

    Period

    Hour

    Sub-Hour

    Month

    Day

     

    Create a measure:

    Measure = 
    VAR SelectedPeriod = SELECTEDVALUE('DatePeriods'[Period], "Day")
    RETURN
    SWITCH(
        SelectedPeriod,
        "Sub-Hour", FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "mmm d yyyy hh:mmAM/PM"),
        "Hour", FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "mmm d yyyy hh:mmAM/PM"),
        "Day", FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "M/D/YYYY"),
        "Month", FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "mmm-yy"),
        FORMAT(MAX('Table'[IXN_SUBHOUR_DATE]), "M/D/YYYY")
    )

     

    The final page effect is shown below:

     

    Overall, the second method performs better and is more convenient.

     

    The first and third lines are the time taken by the second method, and the other two lines are the time taken by the first line.

     

    Please choose your preferred method according to your needs.

     

    pbix file is attached.

     

    If you have any further questions please feel free to contact me.

     

    Best Regards,
    Yang
    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    • manoj_0911's avatar
      manoj_0911
      Icon for Kudo Commander rankKudo Commander

      Hey Anonymous,

      I just wanted to express my sincere gratitude for taking the time to answer my question. Your explanation was incredibly clear and helpful. Thanks to your help, I was able to resolve my query.

      Thanks again!