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
bwang2
Helper I
Helper I

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
v-yohua-msft
Community Support
Community Support

Hi, @bwang2 
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
v-yohua-msft
Community Support
Community Support

Hi, @bwang2 
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.

@v-yohua-msft Thanks!!! it worked

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.