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
Anonymous
Not applicable

Create a calculated column that selects the most recent text from a different table

Hello! I am going in circles. I have a table of donors and a table of gifts in a one to many relationship. In the Donor table, I would like to create a calculated column that looks at all the gifts for each donor and then writes the most recent Solicit Code (based on Gift Date) to the calculated column. My main problem is that I don't have anything to aggregrate so most of the examples don't apply.

 

Donor IDGift DateSolicit Code
11/1/2016Banquet
11/1/2017Fun Run
11/1/2018Appeal
11/1/2019Online

 

In this case, the most recent gift is 1/1/2019 so the Most Recent Solicit Code would be "Online" and that's what I would like written into the calculated column field for Donor 1.

 

Once I figure out how to do this, it is going to be very helpful in our data analysis, so thank you in advance for your assistance!

1 ACCEPTED SOLUTION

You can use similar basic logic, if you can guarentee that you don't have multple gifts from the one Donor on a given day you could use the relatedtable and lookup functions

Latest Solicit Code = 
VAR _lastDate = MAXX(RELATEDTABLE(Gift), Gift[Gift Date] ) 
VAR _result = LOOKUPVALUE(Gift[Solicit Code], Gift[Donor ID], Donor[Donor ID], Gift[Gift Date], _lastDate)
RETURN _result

If you can't guarentee that there won't be more than one gift from an given donor on a given day you could use the following, but note that it will just grab the "max" Solicit Code (so probably the last one when sorted alphabetically)

Latest Solicit Code 2 = 
VAR _lastDate = MAXX(RELATEDTABLE(Gift), Gift[Gift Date] ) 
VAR _currentDonor = Donor[Donor ID]
VAR _result = CALCULATE(MAXX(FILTER(Gift, Gift[Donor ID] = _currentDonor && Gift[Gift Date] = _lastDate) , Gift[Solicit Code]))
RETURN _result

View solution in original post

5 REPLIES 5
d_gosbell
Super User
Super User

If you want to do this as a calculated column on the example table you provided above you could use the following code:

 

Latest Solicit Code Column =
VAR _lastDate = CALCULATE(MAX(Table1[Gift Date]), ALLEXCEPT(Table1,Table1[Donor ID]))
VAR _result = CALCULATE(MAX(table1[Solicit Code]), FILTER(Table1, table1[Gift Date] = _lastDate ))
RETURN _result
Anonymous
Not applicable

Ideally, I need the calculated column on the Donor table, not the Gift table. How do I move a string of text from a column in one table to a column in another?

Hi @Anonymous , if those tables are correctly related, you can create a matrix table on the canvas and use the columns you want.

 

or if you prefer, send me the pbix to give it a try.

 

 

You can use similar basic logic, if you can guarentee that you don't have multple gifts from the one Donor on a given day you could use the relatedtable and lookup functions

Latest Solicit Code = 
VAR _lastDate = MAXX(RELATEDTABLE(Gift), Gift[Gift Date] ) 
VAR _result = LOOKUPVALUE(Gift[Solicit Code], Gift[Donor ID], Donor[Donor ID], Gift[Gift Date], _lastDate)
RETURN _result

If you can't guarentee that there won't be more than one gift from an given donor on a given day you could use the following, but note that it will just grab the "max" Solicit Code (so probably the last one when sorted alphabetically)

Latest Solicit Code 2 = 
VAR _lastDate = MAXX(RELATEDTABLE(Gift), Gift[Gift Date] ) 
VAR _currentDonor = Donor[Donor ID]
VAR _result = CALCULATE(MAXX(FILTER(Gift, Gift[Donor ID] = _currentDonor && Gift[Gift Date] = _lastDate) , Gift[Solicit Code]))
RETURN _result
Anonymous
Not applicable

Thank you! I used the second formula because we have split gifts that have the same gift date.  Generally, they have the same solicit code but if they don't it's fine to use whatever the formula decides is MAX. 

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.