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
frankhofmans
Helper IV
Helper IV

Measure with multiple filters to count working days

hi PI experts,

 

I have a table with a start_date and a end_date of cases (sickness cases) and i would like to calculate the working days between the start_date and the end_date, but if the start date is earlier than the first date of the selected period, than i need the number of working days between the first date of the selected periode and de end_date. The same goes for the end_date, so if the end_date is later than the selected period, i need the number of working days between the start_date (or the first date of the selected period if the start_date is earlier than the first date of the selected period) and the end_date. So i have four possibles outcomes:

 

Start_DateEnd_DateStatusCalculation needed
30 March 202005 July 2020Started before Q2, ended after Q2# of working days between first day Q2 and last day Q2
02 April 202010 April 2020Started in Q2, ended in Q2# of working days between Start_Date and End_Date
05 April 202010 August 2020Started in Q2, ended after Q2# of working days between Start_Date and last day Q2
01 March 202001 May 2020Started before Q2, ended in Q2

# of working days between first day Q2 and End_Date

 

The result i want is:

 

Q# of cases# of working days totalAverage
Q1(al cases handled in Q1, so cases with Date_start in Q1 (ended in or after Q1) and cases with Date_start before Q1 and Date_end in or after Q1Sum of working days, given the "calculation needed" possible outcomes.# of working days total / # of cases
Q2   
Q3  

 

Q4   

 

I have no problem with the Excel formula, but i can't replicate it in Power BI. Can someone help me with this (for me) impossible calculation? 🙂 Thanks in advance!!

2 ACCEPTED SOLUTIONS

Hi @frankhofmans ,

 

Pleas change the " WEEKDAY ( [Date], 3 ) <= 5 " in measure “Calculation needed” to "WEEKDAY ( [Date], 3 ) <= 4":

 

Calculation needed = 
VAR StartDate =
    MAX ( MIN ( 'Calenda'[Date] ), MIN ( 'Outcomes'[Start_Date] ) )
VAR EndDate =
    MIN ( MAX ( 'Calenda'[Date] ), MAX ( 'Outcomes'[End_Date] ) )
RETURN
    IF (
        StartDate <= EndDate,
        COUNTROWS (
            FILTER ( CALENDAR ( StartDate, EndDate ), WEEKDAY ( [Date], 3 ) <= 4 )
        ),
        0
    )

 

7.jpg


Best regards,

 

Community Support Team _ Dong Li
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

Hi @frankhofmans ,

 

Firstly, we need to create a row table:

 

2.jpg

 

Then we create a calendar table as column

 

CalendarTable = ADDCOLUMNS(CALENDARAUTO(),"MonthName",FORMAT([Date],"MMM"),"MonthNo",MONTH([Date]),"Year",YEAR([Date]))

 

 Then we can create a measure to meet your requirement:

 

Count = 
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Case ID] ),
    FILTER (
        'Table',
        'Table'[Startdate Sickness Case] <= MAX ( 'CalendarTable'[Date] )
            && 'Table'[Enddate Sickness Case] >= MAX ( 'CalendarTable'[Date] )
            &&
            VAR workday =
                COUNTROWS (
                    FILTER (
                        CALENDAR ( 'Table'[Startdate Sickness Case], MAX ( 'CalendarTable'[Date] ) ),
                        WEEKDAY ( [Date], 2 ) <= 5
                    )
                )
            RETURN
                workday >= MIN ( 'Row Table'[MinDay] )
            && workday <= MAX ( 'Row Table'[MaxDay] )
    )
) + 0

 

3.jpg


By the way, PBIX file as attached.


Best regards,

 

Community Support Team _ Dong Li
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

13 REPLIES 13
v-lid-msft
Community Support
Community Support

Hi @frankhofmans ,

 

We can try to use following measures to meet your requirement:

 

Calculation needed = 
VAR StartDate =
    MAX ( MIN ( 'Calenda'[Date] ), MIN ( 'Outcomes'[Start_Date] ) )
VAR EndDate =
    MIN ( MAX ( 'Calenda'[Date] ), MAX ( 'Outcomes'[End_Date] ) )
RETURN
    IF (
        StartDate <= EndDate,
        COUNTROWS (
            FILTER ( CALENDAR ( StartDate, EndDate ), WEEKDAY ( [Date], 3 ) <= 5 )
        ),
        0
    )

 

# of cases = 
CALCULATE (
    COUNTROWS ( 'Outcomes' ),
    FILTER (
        'Outcomes',
        OR (
            AND (
                'Outcomes'[Start_Date] <= MAX ( 'Calenda'[Date] ),
                'Outcomes'[Start_Date] >= MIN ( 'Calenda'[Date] )
            ),
            AND (
                'Outcomes'[Start_Date] < MIN ( 'Calenda'[Date] ),
                'Outcomes'[End_Date] >= MIN ( 'Calenda'[Date] )
            )
        )
    )
)

 

# of working days total = 
SUMX (
    FILTER (
        'Outcomes',
        OR (
            AND (
                'Outcomes'[Start_Date] <= MAX ( 'Calenda'[Date] ),
                'Outcomes'[Start_Date] >= MIN ( 'Calenda'[Date] )
            ),
            AND (
                'Outcomes'[Start_Date] < MIN ( 'Calenda'[Date] ),
                'Outcomes'[End_Date] >= MIN ( 'Calenda'[Date] )
            )
        )
    ),
    [Calculation needed]
)

 

Average = DIVIDE([# of working days total],[# of cases],0)

 

4.jpg


If it doesn't meet your requirement, Could you please show the exact expected result based on the Tables that you have shared?


By the way, PBIX file as attached.


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Dear Dong Li,

 

a few month ago you helpend me with a questions about the working days. I want to replicate your measure, but this time for calendar days instead of working days. How do i change the following measure to create the calendar days?

 

Calculation needed =
VAR StartDate =
    MAX ( MIN ( 'Calenda'[Date] ), MIN ( 'Outcomes'[Start_Date] ) )
VAR EndDate =
    MIN ( MAX ( 'Calenda'[Date] ), MAX ( 'Outcomes'[End_Date] ) )
RETURN
    IF (
        StartDate <= EndDate,
        COUNTROWS (
            FILTER ( CALENDAR ( StartDate, EndDate ), WEEKDAY ( [Date], 3 ) <= 5 )
        ),
        0

 

thanks in advance,

 

Regards,

 

Frank
    )

Dear Dong Li,

 

thanks for your response. I'm getting really close. I created the following test table:

 

Start_dateEnd_dateStart date Q1 neededEnd date Q1 neededStart Date Q2 neededEnd date Q2 needed# Working days (working days between Start_Date and End_Date)# Working days Q1 needed (working days between start date Q1 and end date Q1)# Working days Q1 needed (working days between start date Q2 and end date Q2)
5-1-201928-2-20201-1-202028-2-2020  300430
31-12-20199-1-20201-1-20209-1-2020  870
1-12-20191-4-20201-1-202031-3-20201-4-20201-4-202088651
7-1-202020-1-20207-1-202020-1-2020  10100
7-1-20201-7-20207-1-202031-3-20201-4-202030-6-20201276165
20-12-201930-4-20201-1-202031-3-20201-4-202030-4-2020956522
1-5-20203-5-2020  1-5-20203-5-2020101
1-5-202010-8-2020  1-5-202030-6-202072043
1-3-20201-6-20201-3-202031-3-20201-4-20201-6-2020662244

 

The outcome has to be:

 

Q# CasesSum working days 
Q17273sum working days Q1 needed
Q26176sum working days Q2 needed

 

Based on your suggestions, which i copied exactly, i got the following results:

 

Q# casesSum working days
Q17326
Q26212

 

The # of cases is correct, but i got a difference in the sum of working days (and the difference is not explained by calendar days vs working days). Do you any other suggestions?

 

thanks in advance!

 

Regards, Frank

Hi @frankhofmans ,

 

Pleas change the " WEEKDAY ( [Date], 3 ) <= 5 " in measure “Calculation needed” to "WEEKDAY ( [Date], 3 ) <= 4":

 

Calculation needed = 
VAR StartDate =
    MAX ( MIN ( 'Calenda'[Date] ), MIN ( 'Outcomes'[Start_Date] ) )
VAR EndDate =
    MIN ( MAX ( 'Calenda'[Date] ), MAX ( 'Outcomes'[End_Date] ) )
RETURN
    IF (
        StartDate <= EndDate,
        COUNTROWS (
            FILTER ( CALENDAR ( StartDate, EndDate ), WEEKDAY ( [Date], 3 ) <= 4 )
        ),
        0
    )

 

7.jpg


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Dear Dong Li,

 

A month ago you helped me with a problem. Maybe you can help me one more time. i want to present the number of open sickness (started before the last day of a month or week, closed after the last day of the month or week) and the number of days those sickess are open (the number of working days between the startdate of the sickness en the last day of the month or week).

 

Startdate Sickness CaseEnddate Sickness CaseOpen on 31th january 2020Open on 29th february 2020Open on 31th march 2020# Working days on 31th january 2020# Working days on 29th february 2020# Working days on 30th march 2020
1-1-20203-2-2020YNN23 (working days between start date and 31th of january)00
10-1-202020-1-2020NNN000
11-1-20207-4-2020YYY153556
3-2-202010-2-2020NNN000
7-2-20203-3-2020NYN0160
5-3-202020-3-2020NNN000
5-3-20204-4-2020NNY0018

 

I solved the first part of my wish (the number of open cases in a certain period), but i can't find a way to present the number of working days between the start date and the last date of the period. The way i want to present it is:

 

 JanFebMarch
# of open sickness cases222
# of open sickness cases 0-2 days000
# of open sickness cases 3-10 days000
# of open sickness cases 11-20 days111
# of open sickness cases > 20 days111

 

thanks in advance for your help!

 

Regards,

 

Frank

Hi @frankhofmans ,

 

Firstly, we need to create a row table:

 

2.jpg

 

Then we create a calendar table as column

 

CalendarTable = ADDCOLUMNS(CALENDARAUTO(),"MonthName",FORMAT([Date],"MMM"),"MonthNo",MONTH([Date]),"Year",YEAR([Date]))

 

 Then we can create a measure to meet your requirement:

 

Count = 
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Case ID] ),
    FILTER (
        'Table',
        'Table'[Startdate Sickness Case] <= MAX ( 'CalendarTable'[Date] )
            && 'Table'[Enddate Sickness Case] >= MAX ( 'CalendarTable'[Date] )
            &&
            VAR workday =
                COUNTROWS (
                    FILTER (
                        CALENDAR ( 'Table'[Startdate Sickness Case], MAX ( 'CalendarTable'[Date] ) ),
                        WEEKDAY ( [Date], 2 ) <= 5
                    )
                )
            RETURN
                workday >= MIN ( 'Row Table'[MinDay] )
            && workday <= MAX ( 'Row Table'[MaxDay] )
    )
) + 0

 

3.jpg


By the way, PBIX file as attached.


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

It works perfect! thanks again for the quick response!

 

Regards,

 

Frank

Can i ask you one more question? what is the dax formule if i want the cumulated count of days. so every quarter start with 1st january of the selected year. 

 

Start dateEnd DateOutcome needed 
01-11-201910-1-2020Working days between 1-1-2020 and 10-1-2020 
02-01-202026-02-2020Working days between 02-01-2020 and 26-02-2020 
10-02-202005-04-2020Working days between 10-02-2020 and 31-3-2020 

 

Thanks in advance!

Hi @frankhofmans ,

 

Of couse you can ask question here, we will try to solve it. Do you mean set filter Year=2020, then the every record which start date earlier before 2020 will calculated from 01-01-2020? It seems the previous formula can output the desire result.

 

16.jpg


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

hi,

 

i added a extra column in my date table (column name "first date of year" with 1st januar for every year). i changed the min(date) in min(first date of year) and now its working. i simple solution, i had to think longer before asking you the question. My apologies...

Wow, a perfect outcome! you are my hero 🙂 thanks for the help!

Greg_Deckler
Super User
Super User

Net Work Days Quick Measure: https://community.powerbi.com/t5/Quick-Measures-Gallery/Net-Work-Days/td-p/367362


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

hi Greg,

 

thanks for your reply. For me the difficulty is to combine the networking days function (as you described in your reply) with a multipe arguments calculation (more than 2 arguments):

 

if(and(start_date >= first day Q1;start_date<=last day Q1;end_date<=last day);working days between start_date and end_date

if(and(start_date < first day Q1; end_date>=first day Q1; end_date<=last day Q1);working days between first day Q1 and end_date

etc.

 

Do you have a solutions for me to calculate this in 1 Dax formula?

 

Thanks!

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.