Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Dynamic Day calculation based on weekly

Hi Guys, i have a complex scenario as explained below.

 

i want to pull a weekly report for employees who applied for no of days of WFH in a week.

i want to find this based on WFH applied dates.

 

can you please tell me what is the best approach to capture the no of days for the selected week?

 

from the below example is Out put1 or Out put2 is possibile? how to achieve both the outputs?

 

But we need to calculate the no of days only for the given week, an employee can apply wfh for multiple days but we need to count the days only for the given week based on the from and to date.

 

tamerj1 amitchandak Jihwan_Kim daXtreme Greg_Deckler Pragati11 mwegener 

parry2k lbendlin PaulDBrown 

Employee NumberApplied on From date To Date Expected Output 2WFH Date
1234520-08-202225-08-202226-08-2022 1234525-08-2022
3456725-08-202226-08-202231-08-2022 1234526-08-2022
8796922-08-202223-08-202206-09-2022 3456726-08-2022
     3456727-08-2022
Reporting week Aug 22nd to 26th    3456728-08-2022
Expected output 1    3456729-08-2022
     3456730-08-2022
Employee NumberNo of days of WFH applied for the week 3456731-08-2022
123452 Days   8796923-08-2022
345671 Day   8796924-08-2022
879694 Days   8796925-08-2022
     8796926-08-2022
     8796927-08-2022
     8796928-08-2022
     8796929-08-2022
     8796930-08-2022
     8796931-08-2022
     8796901-09-2022
     8796902-09-2022
     8796903-09-2022
     8796904-09-2022
     8796905-09-2022
  • Here is one way, which basically involves expanding the from / to dates.

    Create a new table using:

     

    Expanded =
    VAR _EXP =
        GENERATE (
            'Original Table',
            CALENDAR ( 'Original Table'[From date ], 'Original Table'[To Date] )
        )
    RETURN
        SELECTCOLUMNS (
            _EXP,
            "_Employee Num", 'Original Table'[Employee Number],
            "_App on", 'Original Table'[Applied on ],
            "_Dates", [Date]
        )
    

     

     

    Create a Calendar Table and a dimension table for employees using:

     

    Calendar Table =
    ADDCOLUMNS (
        CALENDAR ( MIN ( Expanded[_Dates] ), MAX ( Expanded[_Dates] ) ),
        "MonthNum", MONTH ( [Date] ),
        "Month", FORMAT ( [Date], "MMM" ),
        "Year", YEAR ( [Date] ),
        "WeekNum", WEEKNUM ( [Date] ),
        "W #", "W" & WEEKNUM ( [Date] )
    )
    

     

    Employee Table = 
    DISTINCT('Original Table'[Employee Number])

     

     Create single direction one-to-many relationships between these new tables and the corresponding fields in the "Expanded" table. The model looks like this:

    Next create the following measure:

     

    WFH Dates = 
    COUNT(Expanded[_Dates])

     

    Create a table visual with 'Employee Table'[Employee] and the measure to get:

     Create a new table visual with the 'Employee Table'[Employee] and 'Calendar Table'[Date] (rename the latter to whatever you wish) to get

     Create a matrix visual with 'Employee Table'[Employee] as rows, 'Calendar Table'[W#) as columns and the [WFH Dates] measure to get:

     

     

    Attached is the sample PBIX file

  • Hi Anonymous 
    Please refer to attached sample file with the solution

    Mumber of Days = 
    VAR MinDate =
        MIN ( 'Date'[Date] )
    VAR MaxDate =
        MAX ( 'Date'[Date] )
    VAR Dates1 =
        CALENDAR ( MinDate, MaxDate )
    RETURN
        SUMX (
            'Table',
            VAR Dates2 =
                CALENDAR ( 'Table'[From Date], 'Table'[To Date] )
            RETURN
                COUNTROWS ( FILTER ( INTERSECT ( Dates1, Dates2 ), NOT ( WEEKDAY ( [Date], 2 ) IN { 6, 7 } ) ) )
        )
  • To manage holidays, you will need to import a table listing public holidays. In this example I imported public holidays for the UK from the Uk-gov website.

     You can then use a measure to subtract WFU days which occur on a holiday as follows:

    Non Hols WFH = 
    VAR _NWD = CALCULATE(COUNT(Expanded[_Dates]), FILTER('Calendar Table', 'Calendar Table'[Date] IN VALUES('Holiday Table'[Date])))
    RETURN
    COUNT(Expanded[_Dates]) - _NWD

     Or you can include the holidays in a new column in the calendar table

     And use this measure to exclude holidays:

    WFH non hol Days (Calendar) =
    CALCULATE (
        COUNT ( Expanded[_Dates] ),
        FILTER ( 'Calendar Table', ISBLANK ( 'Calendar Table'[Holiday] ) )
    )
    

     

    New file attached

     

7 Replies

  • PaulDBrown's avatar
    PaulDBrown
    Community Champion

    Here is one way, which basically involves expanding the from / to dates.

    Create a new table using:

     

    Expanded =
    VAR _EXP =
        GENERATE (
            'Original Table',
            CALENDAR ( 'Original Table'[From date ], 'Original Table'[To Date] )
        )
    RETURN
        SELECTCOLUMNS (
            _EXP,
            "_Employee Num", 'Original Table'[Employee Number],
            "_App on", 'Original Table'[Applied on ],
            "_Dates", [Date]
        )
    

     

     

    Create a Calendar Table and a dimension table for employees using:

     

    Calendar Table =
    ADDCOLUMNS (
        CALENDAR ( MIN ( Expanded[_Dates] ), MAX ( Expanded[_Dates] ) ),
        "MonthNum", MONTH ( [Date] ),
        "Month", FORMAT ( [Date], "MMM" ),
        "Year", YEAR ( [Date] ),
        "WeekNum", WEEKNUM ( [Date] ),
        "W #", "W" & WEEKNUM ( [Date] )
    )
    

     

    Employee Table = 
    DISTINCT('Original Table'[Employee Number])

     

     Create single direction one-to-many relationships between these new tables and the corresponding fields in the "Expanded" table. The model looks like this:

    Next create the following measure:

     

    WFH Dates = 
    COUNT(Expanded[_Dates])

     

    Create a table visual with 'Employee Table'[Employee] and the measure to get:

     Create a new table visual with the 'Employee Table'[Employee] and 'Calendar Table'[Date] (rename the latter to whatever you wish) to get

     Create a matrix visual with 'Employee Table'[Employee] as rows, 'Calendar Table'[W#) as columns and the [WFH Dates] measure to get:

     

     

    Attached is the sample PBIX file

    • Anonymous's avatar
      Anonymous
      Not applicable

      tamerj1 PaulDBrown  i have to say you guys are brilliant, allow me with some time to review these provided solutions and will confirm the same.

    • Anonymous's avatar
      Anonymous
      Not applicable

      PaulDBrown , one more check i want to do, how to know any given day is holiday or not? how can we exclude Holidays from these dates?

      • PaulDBrown's avatar
        PaulDBrown
        Community Champion

        To manage holidays, you will need to import a table listing public holidays. In this example I imported public holidays for the UK from the Uk-gov website.

         You can then use a measure to subtract WFU days which occur on a holiday as follows:

        Non Hols WFH = 
        VAR _NWD = CALCULATE(COUNT(Expanded[_Dates]), FILTER('Calendar Table', 'Calendar Table'[Date] IN VALUES('Holiday Table'[Date])))
        RETURN
        COUNT(Expanded[_Dates]) - _NWD

         Or you can include the holidays in a new column in the calendar table

         And use this measure to exclude holidays:

        WFH non hol Days (Calendar) =
        CALCULATE (
            COUNT ( Expanded[_Dates] ),
            FILTER ( 'Calendar Table', ISBLANK ( 'Calendar Table'[Holiday] ) )
        )
        

         

        New file attached

         

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi Anonymous 
    Please refer to attached sample file with the solution

    Mumber of Days = 
    VAR MinDate =
        MIN ( 'Date'[Date] )
    VAR MaxDate =
        MAX ( 'Date'[Date] )
    VAR Dates1 =
        CALENDAR ( MinDate, MaxDate )
    RETURN
        SUMX (
            'Table',
            VAR Dates2 =
                CALENDAR ( 'Table'[From Date], 'Table'[To Date] )
            RETURN
                COUNTROWS ( FILTER ( INTERSECT ( Dates1, Dates2 ), NOT ( WEEKDAY ( [Date], 2 ) IN { 6, 7 } ) ) )
        )
    • Anonymous's avatar
      Anonymous
      Not applicable

      tamerj1 thank for your brilliant walkthrough, allow me with some time to review this analysis and will confirm the same.