Forum Discussion
Count Lastest Records Before Selected Date
Hello community,
I have the follwoing data model: Date (1-*) Employees (1-*) Events.
Sample data from Employees:
| End of Month | Employee ID | Employee Status | Labour Type |
| 7/31/2025 | 97001779 | Active | Direct Labor |
| 7/31/2025 | 97001779 | Active | Indirect Labor |
| 3/31/2025 | 94062306 | Active | Indirect Labor |
| 5/31/2025 | 94062306 | Active | Indirect Labor |
| 8/31/2025 | 94062306 | Terminated | Indirect Labor |
| 12/31/2023 | 92999434 | Active | Indirect Labor |
| 12/31/2023 | 92999434 | Unpaid Leave | Indirect Labor |
| 1/31/2024 | 92999434 | Unpaid Leave | Indirect Labor |
| 6/30/2025 | 92999434 | Terminated | Indirect Labor |
| 12/31/2023 | 51115216 | Active | Indirect Labor |
| 1/31/2024 | 51115216 | Active | Indirect Labor |
| 4/30/2024 | 51115216 | Active | Indirect Labor |
| 3/31/2025 | 51115216 | Paid Leave | Indirect Labor |
| 4/30/2025 | 51115216 | Terminated | Indirect Labor |
In Power BI, I need to create measures such as Headcount, Terminations etc. Users must have the ability to select a Date in the slicer (and other slicers as well) and view these measures by them. The End of Month column from the below screenshot is pulled from the Date table dimension
I started with doing a general People Count (regardless of Employee Status), but figured that if I select a date in my slicer (2024-02) for which I got no records in Employees, it will return blanks.
People Count 1 = DISTINCTCOUNT( Employees[Employee ID] )
So to "bypass" this behaviour, I amended my formula. This helped me achieve my desired result of viewing the total people count by Month (from Date dim table):
People Count 2 =
CALCULATE (
DISTINCTCOUNT ( Employees[Employee ID] ),
FILTER (
ALL ( 'Date' ),
'Date'[Date] <= MAX ( 'Date'[Date] ) && 'Date'[Date] <> BLANK ()
),
Employees[Employee Status] = "Active"
)
But then, as I moved forward with my report, I wanted to calculate Headcount, meaning only ACTIVE employee statuses.
Headcount =
CALCULATE (
DISTINCTCOUNT ( Employees[Employee ID] ),
FILTER (
ALL ( 'Date' ),
'Date'[Date] <= MAX ( 'Date'[Date] ) && 'Date'[Date] <> BLANK ()
),
Employees[Employee Status] = "Active"
)But I got the same results like in the previous screenshot, and it`s normal. The code below is counting all the occurences of active Employee ID rows
I then figured that I need to make some adjustments. I need to be able to filter the rows of my table to display only the latest transactions up to my selected slicer date:. Here is an expected outcome:
So something that would allow me to select in my Slicer the Date (12/20/2024) and show People Count = 2. I then want to be able to refrence People Count in other measures for more advanced calculations (i.e. Headcount = CALCULATE( Peple Count, Employee Status="Active") and get the result 1 for 12/20/2024; i.e. Terminations = CALCULATE( Peple Count, 'Events'[Change Reason] = "Termination"); i.e. Levrage this further and calculate R12M Attrition).
The problem is getting this count correct 🙂 So, here`s what I did so far:
1. I started with creating a Snapshot Date, that`s basically the same value as my slicer selection:
Snapshot Date = CALCULATE( MAX('Date'[Date]), ALL('Date') )
2. Getting the Max Evemt Date
Originally I did it like below, but it returned blank when I selected my date in the slicer (because employees has no records for 12/20/2024)
Max Event Date =
CALCULATE (
MAX ( Employees[Effective Start Date] ),
FILTER( Employees , Employees[Employee ID] = MAX(Employees[Employee ID])))So I adjusted to this:
Max Effective Start Date =
CALCULATE (
MAX ( Employees[Effective Start Date] ) ,
ALLEXCEPT(Employees, Employees[Employee ID], Employees[Effective Start Date] )
)
3. I created a "Valid Record"field, which returns beautifully, like in my coloured screenshot above
Valid Record = IF([Max Effective Start Date]=[Latest Event Date In Scope],1,0)
4. Tried to do the People Count 3 (final) which... WENT WRONG, of course 🙂
People Count Final =
CALCULATE (
DISTINCTCOUNT ( Employees[Employee ID] ),
FILTER (
ALL ( 'Date' ),
'Date'[Date] <= MAX ( 'Date'[Date] ) && 'Date'[Date] <> BLANK () && [Valid Record] = 1
)
)
Morover, if I try to plot the End of Month (from Date) versus this measure, it`s returning 0!
Thank you for taking your time to review my case, any help is appreciated.
Hi,
See if my solution in this PBI file helps.
13 Replies
- Kedar_Pande
Super User
You're overcomplicing this. Use LASTDATE within the date context.
Create this measure:
Headcount =
VAR SelectedDate = MAX('Date'[Date])
RETURN
CALCULATE(
DISTINCTCOUNT(Employees[Employee ID]),
FILTER(
ALL(Employees),
Employees[End of Month] =
CALCULATE(
LASTDATE(Employees[End of Month]),
ALL('Date'),
Employees[End of Month] <= SelectedDate
)
),
Employees[Employee Status] = "Active"
) - DianaDM96Frequent Visitor
I figured that if I remove my Employee ID from the table, the whole logic falls 😓 I guess my logic was very "Excel" and less "Power BI". Looking forward to suggestions that would let me achieve my desired results.
- lbendlin
Super User
Well, you already have a calendar table, so you are moving in the right direction. You may have to break the relationship if you want to report on things that are not there.
How comfortable are you with WINDOW functions in DAX?
- DianaDM96Frequent Visitor
I have not used window functions so far, nor have I came across them.
I am confused about breaking the relationship, how will my slicer selection of Date work if I break the relationship? If I select 12/20/2024 in my slicer after breaking the realtionship, I get the same value over all months.