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
Anmolgan
Post Prodigy
Post Prodigy

How to find 2 successive dates and calculate no of hours on those?

I need to write the DAX function using which I can find number of dates that are coming first 2 times for each of the employee and what are the hours worked, below is the data like:

 

EmpName   Date              hours

A                 23/10/2021    4

A                 30/10/2021    6

B                  01/11/2021    3

C                  05/11/2021     10

C                  14/11/2021     11

C                  20/11/2021     20

 

DAX should give me number of hours as A=10 and C= 22 (since we are talking about first 2 dates per employee), and B will not be counted in the above since there are no 2 consecutive dates.

 

Any ideas how to calculate the above?

1 ACCEPTED SOLUTION

@Anmolgan , you can try a flag like

 

Column = Var _count = countx(filter(Period, [EmpNam] =EARLIER([EmpNam]) && [Date] <= EARLIER([Date])), [EmpNam]) 
var _hour = sumx(filter(Period, [EmpNam] =EARLIER([EmpNam]) && [Date] <= EARLIER([Date])), [hours]) 
return if(_count =2 && _hour >20, 1,0)

 

See if attached file after signature can help

View solution in original post

5 REPLIES 5
amitchandak
Super User
Super User

@Anmolgan , Create these two columns

 

Cnt = countx(filter(Table, [EmpName] = earlier([EmpName]) && [Date] <= earlier([Date]) ), [EmpName])

total = countx(filter(Table, [EmpName] = earlier([EmpName]) && [Date] <= earlier([Date]) ), [hours])

 

create meausre

calculate(sum(Table[Total]), filter(Table, Table[cnt] =2))

@amitchandak  thanks for the response, can we tweek this as I have payperiods defined by the below DAX, so basically am looking to find number of employees worked less then 20 hours in there first two payperiods, now payperiods can be 3,5,10 etc. Below are the DAX have written to determine payperiod:

 

pay period number = FLOOR(DATEDIFF(DATE(2021,8,15),'Dates'[Date],DAY)/14,1)+1
pay period end date = CALCULATE(MAX('Dates'[Date]),ALLEXCEPT(Dates,Dates[pay period number]))

@Anmolgan , do you need based on selected range or only the first two periods?

 

You can first 2 period without slicer filter, columns will do

 

additional filter will do

calculate(sum(Table[Total]), filter(Table, Table[cnt] =2 && [Total] >20 ))

@amitchandak  I Require only the first two periods to check what were the hours worked does the DAX you posted previously will do this?

@Anmolgan , you can try a flag like

 

Column = Var _count = countx(filter(Period, [EmpNam] =EARLIER([EmpNam]) && [Date] <= EARLIER([Date])), [EmpNam]) 
var _hour = sumx(filter(Period, [EmpNam] =EARLIER([EmpNam]) && [Date] <= EARLIER([Date])), [hours]) 
return if(_count =2 && _hour >20, 1,0)

 

See if attached file after signature can help

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.