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
Anonymous
Not applicable

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.

 

johnbasha33_0-1663863144858.png

@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
3 ACCEPTED SOLUTIONS
PaulDBrown
Community Champion
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]
    )

 

table.png

 

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] )
)

cal.png

 

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

 

emp.png

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

model.png

Next create the following measure:

 

WFH Dates = 
COUNT(Expanded[_Dates])

 

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

exp 1.png

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

exp 2.png

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

matrix.png

 

 

Attached is the sample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

tamerj1
Super User
Super User

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

3.png1.png2.png

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 } ) ) )
    )

View solution in original post

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.

hols table.png

 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

hols result.png

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

cal table with hols.png

 And use this measure to exclude holidays:

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

Cal result.png

 

New file attached

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

7 REPLIES 7
tamerj1
Super User
Super User

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

3.png1.png2.png

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
Not applicable

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

PaulDBrown
Community Champion
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]
    )

 

table.png

 

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] )
)

cal.png

 

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

 

emp.png

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

model.png

Next create the following measure:

 

WFH Dates = 
COUNT(Expanded[_Dates])

 

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

exp 1.png

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

exp 2.png

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

matrix.png

 

 

Attached is the sample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






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?

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.

hols table.png

 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

hols result.png

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

cal table with hols.png

 And use this measure to exclude holidays:

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

Cal result.png

 

New file attached

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

@PaulDBrown you are truley awesome. thank you.

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.

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
Top Kudoed Authors