Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
jaygill
Frequent Visitor

DAX Measure to calculate number of lost days in different year from total number of days

Hello everyone, 

I'm fairly new to this community and DAX. and i have run into a problem at work. A help would be much appreciated.

 i'm trying to calculate number of days for particular year based on calender table that i have created.

 

For Example: I have 3 columns. 

Event, number of days and Date when this event started

 

Event       DaysLost

Injury       30              25/12/2016  

Injury       588            06/08/2012

 

Days in 2016 - 6

Days in 2017 - 24

 

For second case

Days in 2012 - 146

Days in 2013 - 365

Days in 2014 - 77

Now for above case there are only 6 days needs to be counted in 2016 and rest of the days should automatically be counted in 2017. But i can't figure out how to do it. 

 

I have a calender table and i want sum of days to populate for a particular year.

 

Thanks in Advance.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @jaygill

 

Try the following

 

1. Remove any relationship between calendar table and the fact table of injury.

2. If you do not have a Year value in calendar table , create a column called Year = Year(Calendar[Date]) in the calendar table.

3. Create a measure ( Using your exccel formula )

 

LostDays =
VAR MAXYEAR = Max('Calendar'[Year])
VAR MINDATE= Min(Injury[StartDate])
VAR MAXDATE = MAX(Injury[EndDate])
VAR CALYEAR = MAX('Calendar'[Year])
RETURN
IF (CALYEAR >= YEAR(MINDATE) && CALYEAR <= YEAR(MAXDATE),
                  IF(YEAR(MAXDATE) > CALYEAR,               
                    DATEDIFF(IF (CALYEAR <> YEAR(MINDATE),
                              DATE(CALYEAR,1,1) ,MINDATE ) ,ENDOFYEAR('Calendar'[Date] ) ,DAY) ,
        DATEDIFF(IF (CALYEAR <> YEAR(MINDATE),
                              DATE(CALYEAR,1,1) ,MINDATE ) ,MAXDATE,DAY ) ) , blank )

 

4. Now plot a matrix chart with

   Number from fact table on Rows

   Year from Calendar Table on Columns

   LostDays measure as values.

 

Capture.GIF

Sample out of data on the left hand side

 

If this works for you please accept this as a solution and also give KUDOS.

 

Cheers

 

CheenuSing

View solution in original post

3 REPLIES 3
jaygill
Frequent Visitor

I'm still looking for a DAX solution. However i figured it out using excel.

Please see a attached screen shot. 

If someone could provide a DAX solution it'd be much appreciated. 

 

Screen Shot 2017-09-04 at 8.03.39 PM.png

Anonymous
Not applicable

Hi @jaygill

 

Try the following

 

1. Remove any relationship between calendar table and the fact table of injury.

2. If you do not have a Year value in calendar table , create a column called Year = Year(Calendar[Date]) in the calendar table.

3. Create a measure ( Using your exccel formula )

 

LostDays =
VAR MAXYEAR = Max('Calendar'[Year])
VAR MINDATE= Min(Injury[StartDate])
VAR MAXDATE = MAX(Injury[EndDate])
VAR CALYEAR = MAX('Calendar'[Year])
RETURN
IF (CALYEAR >= YEAR(MINDATE) && CALYEAR <= YEAR(MAXDATE),
                  IF(YEAR(MAXDATE) > CALYEAR,               
                    DATEDIFF(IF (CALYEAR <> YEAR(MINDATE),
                              DATE(CALYEAR,1,1) ,MINDATE ) ,ENDOFYEAR('Calendar'[Date] ) ,DAY) ,
        DATEDIFF(IF (CALYEAR <> YEAR(MINDATE),
                              DATE(CALYEAR,1,1) ,MINDATE ) ,MAXDATE,DAY ) ) , blank )

 

4. Now plot a matrix chart with

   Number from fact table on Rows

   Year from Calendar Table on Columns

   LostDays measure as values.

 

Capture.GIF

Sample out of data on the left hand side

 

If this works for you please accept this as a solution and also give KUDOS.

 

Cheers

 

CheenuSing

jaygill
Frequent Visitor

I think i should Total number of days to First start and get a end date but dont know how to get number of days for different year if given days exceeds the total remaining days in that year and futher on.

For Example:  if days gives are 400 and Start date in 1/1/2015. i could get the end date by adding 400 to 1/1/2015 and get number of days for that particular year by subtrating nymber of days left in that year but how to automatically count the rest 35 days in next year.

 

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors