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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Anonymous
Not applicable

Need some help with a DAX measure that can calculate total shifts between two dates.

 

Below are two example of filtered data set, Total shift is what I am trying to calculate. 

All my data are in the descending order by DateShiftKey, so that on the same date, DS is always on top of NS.

My approch is to isolate the "first" and "last" shift, what falls in between is irrelevant. I am having trouble to isoloate the "first" and "last" shift information in each sample table. I have also pasted the DAX I wrote but it's not working at where I crossed out, I need soemthing to indentify the first and last shift.

 

Total Shifts Count =
VAR StartDate = MIN('Table'[ReportingDate])
VAR EndDate = MAX('Table'[ReportingDate])
VAR TotalDays = DATEDIFF(StartDate,EndDate,DAY)+1
VAR ShiftDatrt = IF(FIRSTNONBLANK(Table[Shift],0)= "DS",0,1)
VAR ShiftEnd = IF(LASTNONBLANK(Table[Shift],0)= "DS",1,0)
RETURN
    (TotalDays * 2) - ShiftDatrt - ShiftEnd
Filter one  
TaskDateShiftKeyReportingDateShift total shift=6
120240103NS1/3/2024NS  
220240104DS1/4/2024DS  
320240104NS1/4/2024NS  
420240105DS1/5/2024DS  
520240106DS1/6/2024DS  
620240106DS1/6/2024DS  
720240106DS1/6/2024DS  
      
      
      
      
Filter two  
TaskDateShiftKeyReportingDateShift total shift=13
120240109DS1/9/2024DS  
220240110DS1/10/2024DS  
320240110DS1/10/2024DS  
420240110DS1/10/2024DS  
520240112NS1/12/2024NS  
620240113DS1/13/2024DS  
720240113NS1/13/2024NS  
820240113NS1/13/2024NS  
920240113NS1/13/2024NS  
1020240114DS1/14/2024DS  
1120240114DS1/14/2024DS  
1220240114NS1/14/2024NS  
1320240115DS1/15/2024DS  
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi, @Anonymous 
Maybe you can try the following DAX expression, I've made some improvements to your Measure by judging the first and last shifts before calculating them.

Total Shifts Count = 
VAR StartDate = MIN('Table'[ReportingDate])
VAR EndDate = MAX('Table'[ReportingDate])
VAR TotalDays = DATEDIFF(StartDate,EndDate,DAY)+1
VAR ShiftStart = CALCULATE(MIN('Table'[Shift]),FILTER(ALL('Table'),'Table'[ReportingDate]=MIN('Table'[ReportingDate])))
VAR ShiftEnd = CALCULATE(MAX('Table'[Shift]), FILTER(ALL('Table'),'Table'[ReportingDate]=MAX('Table'[ReportingDate])))
VAR ShiftStartValue = IF(ShiftStart = "DS",0,1)
VAR ShiftEndValue = IF(ShiftEnd = "DS",1,0)
RETURN
    (TotalDays * 2) - ShiftStartValue - ShiftEndValue

 

Here is my preview:

vyohuamsft_0-1714098158678.png

 

How to Get Your Question Answered Quickly 

Best Regards

Yongkang Hua

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

2 REPLIES 2
Anonymous
Not applicable

Hi, @Anonymous 
Maybe you can try the following DAX expression, I've made some improvements to your Measure by judging the first and last shifts before calculating them.

Total Shifts Count = 
VAR StartDate = MIN('Table'[ReportingDate])
VAR EndDate = MAX('Table'[ReportingDate])
VAR TotalDays = DATEDIFF(StartDate,EndDate,DAY)+1
VAR ShiftStart = CALCULATE(MIN('Table'[Shift]),FILTER(ALL('Table'),'Table'[ReportingDate]=MIN('Table'[ReportingDate])))
VAR ShiftEnd = CALCULATE(MAX('Table'[Shift]), FILTER(ALL('Table'),'Table'[ReportingDate]=MAX('Table'[ReportingDate])))
VAR ShiftStartValue = IF(ShiftStart = "DS",0,1)
VAR ShiftEndValue = IF(ShiftEnd = "DS",1,0)
RETURN
    (TotalDays * 2) - ShiftStartValue - ShiftEndValue

 

Here is my preview:

vyohuamsft_0-1714098158678.png

 

How to Get Your Question Answered Quickly 

Best Regards

Yongkang Hua

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

@Anonymous Thanks!!! it worked

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.