Forum Discussion
MinDate For Each Unique Record & Group
Hello,
I have a table in which the same employee (PRS_ID) can be included in many rows and I want to take the EARLIEST date (BGN_DATE) for each employee (PRS_ID). Next, I want to see how many employees I have each month. Does anyone know how to write a measure for such a calculation?
Sample Data:
PRS_ID | EMP_STS | BGN_DATE
| homsimps | T | 2022-01-30 |
| homsimps | T | 2022-02-28 |
| homsimps | T | 2022-03-30 |
| bartsimps | T | 2022-01-30 |
| bartsimps | T | 2022-02-28 |
| bartsimps | T | 2022-03-31 |
| lisasimps | T | 2022-04-30 |
| lisasimps | T | 2022-05-31 |
| lisasimps | T | 2022-06-30 |
| lisasimps | T | 2022-07-30 |
| margesimps | T | 2022-02-28 |
| margesimps | T | 2022-03-30 |
Expected Outcome:
Period | Count
| 2022-01 | 2 |
| 2022-02 | 1 |
| 2022-03 | 0 |
| 2022-04 | 1 |
Hey Anonymous ,
I created two calculated columns two answer your first question "earliest date".
The first calculated column repeats the the earlies date for each person:Earliest Date = var currentPerson = 'Table'[PRS_ID] var currntDate = 'Table'[BGN_DATE] var minDate = MINX( FILTER( 'Table' , 'Table'[PRS_ID] = currentPerson ) , [BGN_DATE] ) return minDateThe second column flags the earliest data for each person:
Is Earliest Date = var currentPerson = 'Table'[PRS_ID] var currntDate = 'Table'[BGN_DATE] var minDate = MINX( FILTER( 'Table' , 'Table'[PRS_ID] = currentPerson ) , [BGN_DATE] ) return IF( minDate = currntDate, "TRUE" , "FALSE" )The table will look like this:
The second question "number of persons per month" can be solved by
- creating a dedicated calendar table that also has a period colum
- create a relationship between the calendar table (one-side of the relationship) and the existing table
- create a table visual that uses the period column from the calendar table
- use the perion-id column to the table visual and change the aggregation function of the the column to count distinct
This article explains basically everything about date-related calculation: Time patterns – DAX Patterns
This article provides an introduction to data modeling in Power BI: Design a data model in Power BI - Learn | Microsoft Docs
Hopefully, this helps to tackle your challenge.
Regards,
Tom
1 Reply
- TomMartens
Super User
Hey Anonymous ,
I created two calculated columns two answer your first question "earliest date".
The first calculated column repeats the the earlies date for each person:Earliest Date = var currentPerson = 'Table'[PRS_ID] var currntDate = 'Table'[BGN_DATE] var minDate = MINX( FILTER( 'Table' , 'Table'[PRS_ID] = currentPerson ) , [BGN_DATE] ) return minDateThe second column flags the earliest data for each person:
Is Earliest Date = var currentPerson = 'Table'[PRS_ID] var currntDate = 'Table'[BGN_DATE] var minDate = MINX( FILTER( 'Table' , 'Table'[PRS_ID] = currentPerson ) , [BGN_DATE] ) return IF( minDate = currntDate, "TRUE" , "FALSE" )The table will look like this:
The second question "number of persons per month" can be solved by
- creating a dedicated calendar table that also has a period colum
- create a relationship between the calendar table (one-side of the relationship) and the existing table
- create a table visual that uses the period column from the calendar table
- use the perion-id column to the table visual and change the aggregation function of the the column to count distinct
This article explains basically everything about date-related calculation: Time patterns – DAX Patterns
This article provides an introduction to data modeling in Power BI: Design a data model in Power BI - Learn | Microsoft Docs
Hopefully, this helps to tackle your challenge.
Regards,
Tom