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
Anonymous
Not applicable

Last 6 Months DAX Measure - Based on Date Field

I am having a bit of difficulty trying to get my head around having data on the last six months based on a date field in another table

 

Scenario: 

I am looking at employees and the number of shifts that they do. I only want to count how many shifts they have completed in the last 6 months of their 'anniversary date'. I have tried using the DATESINPERIOD but it is not customised based on the anniversary date. I have the following tables:

  • Employee Data Table
Employee IDAnniversary Date
130 June 2021
223 June 2021
34 September 2021

 

  • Shift Information
Employee IDShift Date
129 June 2021
11 January 2021
220 June 2021
231 December 2020
320 May 2021
33 July 2021

 

  • Date Table

I have set up the following measure as well: 

Number of Shifts = COUNTROWS(Shift Information)

 

I would like to count the number of shifts that employees have done in the last 6 months from their anniversary date, so this is what I would like the expected output to be: 

Employee IDNumber of Shifts
11
21
32

 

As you can see any shifts that have fallen out of the six month period (of the employee's anniversary date) are not included.

 

What would you recommend the next steps be?

 

Thank you! 

1 ACCEPTED SOLUTION
v-yalanwu-msft
Community Support
Community Support

Hi, @Anonymous ;

As lbendlin Said , you offer data is all in last 6 months. if you want to according to every Employee ID's Anniversary Date to count . the measure is as follows ( the result is also different with you) :

countrow = CALCULATE(COUNTROWS('Shift'),FILTER('Shift',DATEDIFF([Shift Date],RELATED(Employee[Anniversary Date]),MONTH)<=6))

vyalanwumsft_0-1632113074257.png

The final output is shown below:

vyalanwumsft_1-1632113082473.png

However, if you want to show the result as yours. you could modify the measure like below:

countrow2 = CALCULATE(COUNTROWS('Shift'),FILTER('Shift',DATEDIFF([Shift Date],MAX(Employee[Anniversary Date]),MONTH)<=6))

The final output is shown below:

vyalanwumsft_2-1632113205496.png


Best Regards,
Community Support Team_ Yalan Wu
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-yalanwu-msft
Community Support
Community Support

Hi, @Anonymous ;

As lbendlin Said , you offer data is all in last 6 months. if you want to according to every Employee ID's Anniversary Date to count . the measure is as follows ( the result is also different with you) :

countrow = CALCULATE(COUNTROWS('Shift'),FILTER('Shift',DATEDIFF([Shift Date],RELATED(Employee[Anniversary Date]),MONTH)<=6))

vyalanwumsft_0-1632113074257.png

The final output is shown below:

vyalanwumsft_1-1632113082473.png

However, if you want to show the result as yours. you could modify the measure like below:

countrow2 = CALCULATE(COUNTROWS('Shift'),FILTER('Shift',DATEDIFF([Shift Date],MAX(Employee[Anniversary Date]),MONTH)<=6))

The final output is shown below:

vyalanwumsft_2-1632113205496.png


Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

lbendlin
Super User
Super User

@Anonymous maybe it is just me but I think all of the shifts are within the six months up to the anniversary date for each employee?

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.