How to calculate number of days in each month for a period which spans to several months

AksaS

New Member
Joined
Jan 12, 2016
Messages
2
Hi,

I need some help to calculate correctly number of sick leave days in each month.
The problem is following, the current PowerPivot based reporting calculates number of sick leave days for a month when sick leave was given even if the sick leave continues to next month.
i.e. if 10 days sick leave is given on 31st of December all 10 days are calculated to December based on the day when sick leave was given.
This should be fixed so that 1 day is calculated to December and 9 days are calculated to January.

In the PowerPivot table where all the events are saved there are columns "SickLeaveDate" which contains the date when sick leave was given, "SickLeaveEndDate" which contains the date when sick leave ends and "SickLeaveDuration" which contains number of sick leave days. In the this example e.g. "SickLeaveDate"=12/31/2015, "SickLeaveEndDate"=01/09/2016 and "SickLeaveDuration"=10.

Can anyone help with the correct DAX formula to fix this problem?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi AksaS,

I think the 'events in progress' pattern applies here.

I've just taken the code from page 27 of this paper and modified for your situation.
http://www.sqlbi.com/wp-content/uploads/DAX-Query-Plans.pdf

I've called your leave table Leave, and I'm assuming Leave[SickLeaveDate] is related to Calendar table column Calendar[Date].

Code:
=
SUMX (
    VALUES ( Calendar[Date] ),
    SUMX (
        FILTER (
            GENERATE (
                CALCULATETABLE (
                    SUMMARIZE (
                        Leave,
                        Leave[SickLeaveDate],
                        Leave[SickLeaveEndDate],
                        "Rows", COUNTROWS ( Leave )
                    ),
                    ALL ( Calendar )
                ),
                DATESBETWEEN ( Calendar[Date], Leave[SickLeaveDate], Leave[SickLeaveEndDate] )
            ),
            Calendar[Date] = EARLIER ( Calendar[Date] )
        ),
        [Rows]
    )
)
 
Upvote 0
Hi Ozeroth,

This works perfectly in my reports, now I get reports where sick leaves are correctly reported to right months.
Big thanks for your help and quick answer :)
 
Upvote 0

Forum statistics

Threads
1,224,146
Messages
6,176,666
Members
452,739
Latest member
SCEducator

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top