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
fahadarshad
Helper I
Helper I

Getting max date and then an accompanying column

Hello everyone

 

I have a table where I have captued the customer ID and the list of products they purchased and when (date/time).

From this I want to know which was the last product they purchased.

 

But I cannot seem to figure out the method to extract this result.

Here is what my data looks like and the expected result.

Kindly assist.

 

Untitled.png

1 ACCEPTED SOLUTION

@fahadarshad

 

If you need a Table

 

Go to Modelling Tab>>>NEW TABLE and then use this formula.

 

 

New Table =
SUMMARIZE (
    TableName,
    TableName[Customer ID],
    "Last Date", MAX ( TableName[Date] ),
    "Last Product", CALCULATE (
        FIRSTNONBLANK ( TableName[Product], 1 ),
        FILTER ( TableName, TableName[Date] = MAX ( TableName[Date] ) )
    )
)

Regards
Zubair

Please try my custom visuals

View solution in original post

11 REPLIES 11
Zubair_Muhammad
Community Champion
Community Champion

@fahadarshad

 

Try these MEASURES. Then in a Table Visual drag the Date Column and these 2 Measures

 

 

 

LastDate = Max(TableName[Date])
LastProduct =
CALCULATE (
    FIRSTNONBLANK ( TableName[Product], 1 ),
    FILTER ( TableName, TableName[Date] = [LastDate] )
)

Regards
Zubair

Please try my custom visuals

Hi Zubair @Zubair_Muhammad

 

What about the customer ID. how do I get that in as well.

@fahadarshad

 

My apologies.

 

I meant drag the Customer ID COlumn and these 2 measures


Regards
Zubair

Please try my custom visuals

Sorry bro; I am very new to Power BI. What does visual drag mean?

HI @fahadarshad

 

See the picture below

 

From the Visualization select table and select the Fields from right most section as shown in the pic

 

10090.png


Regards
Zubair

Please try my custom visuals

@fahadarshad

 

If you are using Excel

 

Put the Customer ID in row field and the 2 MEASURES in VALUE field in the Pivot Table


Regards
Zubair

Please try my custom visuals

@fahadarshad

 

If you need a Table

 

Go to Modelling Tab>>>NEW TABLE and then use this formula.

 

 

New Table =
SUMMARIZE (
    TableName,
    TableName[Customer ID],
    "Last Date", MAX ( TableName[Date] ),
    "Last Product", CALCULATE (
        FIRSTNONBLANK ( TableName[Product], 1 ),
        FILTER ( TableName, TableName[Date] = MAX ( TableName[Date] ) )
    )
)

Regards
Zubair

Please try my custom visuals

@fahadarshad

 

Revised Measure.

 

LastProduct =
CALCULATE (
    FIRSTNONBLANK ( TableName[Product], 1 ),
    FILTER ( TableName, TableName[Date] = MAX ( TableName[Date] ) )
)

Regards
Zubair

Please try my custom visuals

thanks bhai for the help! works superb. 

@Zubair_Muhammad

 

I am using Power BI desktop.

 

But more importantly, the meausre you have specified for the last product is giving me the wrong priduct. the max date seems to be working though.

 

any reason why the last product is incorrect?

Oh ok. 
But I would like to create a table of it rather than visualise it.

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.