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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Calculated column: Return last transaction date which is before a specific date for each client

Hi all,

 

I have two tables, 'transaction' and 'client'. The 'transaction' table have transaction date from 1 Jan 2016 to 31 Dec 2016.

 

In the 'client' table, I am trying to create a calculated colum to return the last transaction date which is before a specific date, say 30 June 2016, for each client.

 

Currently, I am using LOOKUPVALUE to return the last transaction date, =LOOKUPVALUE(transaction[date],transaction[date],LASTDATE(transaction[date]))

however I want to return the last transaction date which is before a specific date.

 

Thank you very much for your time in advance.

 

 

Best regards,

Eric

1 ACCEPTED SOLUTION
TomMartens
Super User
Super User

Hey @Anonymous,

 

here you'll find a little pbix file

 

Please be aware that both tables "Client" and "Transactions" have a relationship: 'Client'[Client] (one-side) -> 'Transactions'[Client] (many-side).

 

This DAX-statement is used to create a calculated column in the Client table

 

Latest Transaction = 
CALCULATE(
    MAX('Transactions'[TransactionDate])
    ,'Transactions'[TransactionDate] < DATE(2016,7,1)
)


This DAX-statement can be used if there is no relationship between both tables

 

Latest Transaction = 
var currentClient = 'Client'[Client]
return
CALCULATE(
    MAX('Transactions'[TransactionDate])
    ,'Transactions'[TransactionDate] < DATE(2016,7,1) 
    ,'Transactions'[Client] = currentClient
)

Depending on the size of your tables there will be a noticeable advantage on performance for the 1st statement because it leverages the relationship.

 

Hope this is what you are looking for

 

Regards

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

2 REPLIES 2
TomMartens
Super User
Super User

Hey @Anonymous,

 

here you'll find a little pbix file

 

Please be aware that both tables "Client" and "Transactions" have a relationship: 'Client'[Client] (one-side) -> 'Transactions'[Client] (many-side).

 

This DAX-statement is used to create a calculated column in the Client table

 

Latest Transaction = 
CALCULATE(
    MAX('Transactions'[TransactionDate])
    ,'Transactions'[TransactionDate] < DATE(2016,7,1)
)


This DAX-statement can be used if there is no relationship between both tables

 

Latest Transaction = 
var currentClient = 'Client'[Client]
return
CALCULATE(
    MAX('Transactions'[TransactionDate])
    ,'Transactions'[TransactionDate] < DATE(2016,7,1) 
    ,'Transactions'[Client] = currentClient
)

Depending on the size of your tables there will be a noticeable advantage on performance for the 1st statement because it leverages the relationship.

 

Hope this is what you are looking for

 

Regards

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

Dear @TomMartens,

 

Thank you very much for your guidance.

 

In addition to getting the solution from you, I learned so much extra.

 

1. Using "calculate" function which leverage on the relationship will have advantage in term of performance. (I definitely need this)

2. Now I have better understanding in how to use "var" and "return", this can be used as well if there is no relationship is being built between tables.

 

 

Thank you very much again,

Eric

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

April Fabric Community Update

Fabric Community Update - April 2024

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