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
Thiagops
Helper II
Helper II

Column with date of last purchase

Good morning,
I have 02 tables (One with sales and another with customers).

I need to include a column in the customer table with the date of the last purchase made by him
- The column I have in the 2 tables is CLI_CODIGO
- The column that has the date of sale is NS_DATA_EMISSAO

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Thiagops 
Please try

Last Purchase Date =
MAXX ( RELATEDTABLE ( Sales ), Sales[NS_DATA_EMISSAO] )

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

Hi @Thiagops 
Please try

Last Purchase Date =
MAXX ( RELATEDTABLE ( Sales ), Sales[NS_DATA_EMISSAO] )
jaweher899
Impactful Individual
Impactful Individual

You can achieve this by using a calculated column in the customer table that uses the MAX function to retrieve the latest date of purchase for each customer. Here's the DAX code for the calculated column:

Last Purchase Date = CALCULATE(MAX(Sales[NS_DATA_EMISSAO]), FILTER(Sales, Sales[CLI_CODIGO] = Customers[CLI_CODIGO]))

In this code, "Sales" is the table with sales data, "Customers" is the table with customer data, "NS_DATA_EMISSAO" is the date of sale column in the Sales table, and "CLI_CODIGO" is the column that links the two tables. The CALCULATE function evaluates an expression in a modified filter context, and the FILTER function returns a table with only the rows that meet the specified criteria. In this case, the FILTER function is used to filter the Sales table to only include the sales for the customer in the current row of the customer table. The MAX function is then used to find the maximum value of the NS_DATA_EMISSAO column in the filtered Sales table, which will be the latest date of purchase for the customer.

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.