Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

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

homsimpsT2022-01-30
homsimpsT2022-02-28
homsimpsT2022-03-30
bartsimpsT2022-01-30
bartsimpsT2022-02-28
bartsimpsT2022-03-31
lisasimpsT2022-04-30
lisasimpsT2022-05-31
lisasimpsT2022-06-30
lisasimpsT2022-07-30
margesimpsT2022-02-28
margesimpsT2022-03-30

 

Expected Outcome:

Period | Count

2022-012
2022-021
2022-030
2022-041
  • 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
    minDate

    The 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

  • 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
    minDate

    The 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