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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

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

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

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))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

@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 ))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

@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

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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