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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
luckygirl
Helper I
Helper I

Sort a table before calculate based on date

I have a table like below.

CodeCompletedDateProductDetail
11/01/2018A
11/04/2019B
123/05/2021C
29/02/2018D
25/05/2021C
26/05/2022A
31/01/2018K
310/05/2019B
330/06/2021C
31/01/2024L
46/06/2017B
49/03/2018J
45/05/2021C
43/03/2024D

 

I need to create a Target column based on CompletedDate for Each Code group. So the expected solution would be like below.

CodeCompletedDateProductDetailTarget
11/01/2018AB
11/04/2019BC
123/05/2021C 
29/02/2018DC
25/05/2021CA
26/05/2022A 
31/01/2018KB
310/05/2019BC
330/06/2021CL
31/01/2024L 
46/06/2017BJ
49/03/2018JC
45/05/2021CD
43/03/2024D 

 

I tried the below DAX code. It generates correct results for some but not all. Can someone please help me. Thanks.

Target =
CALCULATE (
    MIN ( Tbl[ProductDetail] ),
    FILTER (
        'Tbl',
        Tbl[ClientCode] = EARLIER ( Tbl[ClientCode] )
         && Tbl[CompletedDate] > EARLIER ( Tbl[CompletedDate] )
    )
)

The results by the above code. Incorrect ones are highlighted in red.
CodeCompletedDateProductDetailTarget
11/01/2018AB
11/04/2019BC
123/05/2021C 
29/02/2018DA
25/05/2021CA
26/05/2022A 
31/01/2018KB
310/05/2019BC
330/06/2021CL
31/01/2024L 
46/06/2017BC
49/03/2018JC
45/05/2021CD
43/03/2024D 


1 ACCEPTED SOLUTION
Irwan
Super User
Super User

hello @luckygirl 

 

please check if this accomodate your need.

Irwan_0-1737083796882.png

create a new calculated column with following DAX

Target = 
var _Min =
MINX(
    FILTER(
        'Table',
        'Table'[CompletedDate]>EARLIER('Table'[CompletedDate])&&
        'Table'[Code]=EARLIER('Table'[Code])
    ),
    'Table'[CompletedDate]
)
Return
MINX(
    FILTER(
        'Table',
        'Table'[CompletedDate]=_Min
    ),
    'Table'[ProductDetail]
)
 
Hope this will help.
Thank you.

View solution in original post

3 REPLIES 3
luckygirl
Helper I
Helper I

@Irwan Thanks a lot. Works well 🙂

Hello @luckygirl 

 

Glad to be a help.

Thank you.

Irwan
Super User
Super User

hello @luckygirl 

 

please check if this accomodate your need.

Irwan_0-1737083796882.png

create a new calculated column with following DAX

Target = 
var _Min =
MINX(
    FILTER(
        'Table',
        'Table'[CompletedDate]>EARLIER('Table'[CompletedDate])&&
        'Table'[Code]=EARLIER('Table'[Code])
    ),
    'Table'[CompletedDate]
)
Return
MINX(
    FILTER(
        'Table',
        'Table'[CompletedDate]=_Min
    ),
    'Table'[ProductDetail]
)
 
Hope this will help.
Thank you.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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