Forum Discussion
Lookup value in same table, matching to previous date
Hello,
I have a problem that I thought was going to be an easy fix. Unfortunately, I can't seem to do it. I think I'm making it more complicated than it needs to be.
I have a simple table like below, but it contains thousands of lines with multiple PHARMACY_IDs. What I would like to do is create a calculated column that returns the Value for the previous date, matching all other columns.
| 340B_ID | PHARMACY_ID | Date | Manufacturer | Attribute | Value |
| DSH190064 | 1885 | 6/18/2024 | abbvie | RETAIL_STATUS | INELIGIBLE |
| DSH190064 | 1885 | 6/18/2024 | abbvie | DUOPA_STATUS | INELIGIBLE |
| DSH190064 | 1885 | 6/18/2024 | abbvie | IMBRUVICA_STATUS | INELIGIBLE |
| DSH190064 | 1885 | 6/18/2024 | abbvie | VENCLEXTA_STATUS | INELIGIBLE |
| DSH190064 | 1885 | 7/1/2024 | abbvie | RETAIL_STATUS | ELIGIBLE |
| DSH190064 | 1885 | 7/1/2024 | abbvie | DUOPA_STATUS | ELIGIBLE |
| DSH190064 | 1885 | 7/1/2024 | abbvie | IMBRUVICA_STATUS | ELIGIBLE |
| DSH190064 | 1885 | 7/1/2024 | abbvie | VENCLEXTA_STATUS | ELIGIBLE |
Wanted results:
| 340B_ID | PHARMACY_ID | Date | Manufacturer | Attribute | Value | Previous Value |
| DSH190064 | 1885 | 6/18/2024 | abbvie | RETAIL_STATUS | INELIGIBLE | |
| DSH190064 | 1885 | 6/18/2024 | abbvie | DUOPA_STATUS | INELIGIBLE | |
| DSH190064 | 1885 | 6/18/2024 | abbvie | IMBRUVICA_STATUS | INELIGIBLE | |
| DSH190064 | 1885 | 6/18/2024 | abbvie | VENCLEXTA_STATUS | INELIGIBLE | |
| DSH190064 | 1885 | 7/1/2024 | abbvie | RETAIL_STATUS | ELIGIBLE | INELIGIBLE |
| DSH190064 | 1885 | 7/1/2024 | abbvie | DUOPA_STATUS | ELIGIBLE | INELIGIBLE |
| DSH190064 | 1885 | 7/1/2024 | abbvie | IMBRUVICA_STATUS | ELIGIBLE | INELIGIBLE |
| DSH190064 | 1885 | 7/1/2024 | abbvie | VENCLEXTA_STATUS | ELIGIBLE | INELIGIBLE |
Below is the dax code I am using, and the results are all blank. There is a relationship between the date column and a date table.
Hi jhollingworth
If you wanna do it as a measure, Try this.Previous Value =VAR CurrentDate = MAX('Table'[Date])RETURNCALCULATE(MAX('Table'[Value]),FILTER(ALL('Table'),'Table'[Date] < CurrentDate))
If this post helps, then please consider Accept it as the solution to help the others find it more quickly. Appreciate you kudos!!
4 Replies
- jhollingworthFrequent Visitor
Thank you everyone for the help. The measure work posted by NaveenGandhi. The two calculated columns both resulted in blank columns. I will play around with those.
Thanks again.
- rajendraongole1
Super User
Hi jhollingworth - Create a calculated column as below to that returns the Value for the previous date.
Snap
Create a new calculated column FYR: Change the table name as per your dataset references.
Previous Value =VAR Current340B_ID = 'Looku'[340B_ID]VAR CurrentPHARMACY_ID = 'Looku'[PHARMACY_ID]VAR CurrentDate = 'Looku'[Date]VAR CurrentManufacturer = 'Looku'[Manufacturer]VAR CurrentAttribute = 'Looku'[Attribute]VAR PreviousDate =CALCULATE(MAX('Looku'[Date]),FILTER('Looku','Looku'[340B_ID] = Current340B_ID &&'Looku'[PHARMACY_ID] = CurrentPHARMACY_ID &&'Looku'[Manufacturer] = CurrentManufacturer &&'Looku'[Attribute] = CurrentAttribute &&'Looku'[Date] < CurrentDate))RETURNCALCULATE(MAX('Looku'[Value]),FILTER('Looku','Looku'[340B_ID] = Current340B_ID &&'Looku'[PHARMACY_ID] = CurrentPHARMACY_ID &&'Looku'[Manufacturer] = CurrentManufacturer &&'Looku'[Attribute] = CurrentAttribute &&'Looku'[Date] = PreviousDate))Hope it worksDid I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!! - NaveenGandhi
Memorable Member
Hi jhollingworth
If you wanna do it as a measure, Try this.Previous Value =VAR CurrentDate = MAX('Table'[Date])RETURNCALCULATE(MAX('Table'[Value]),FILTER(ALL('Table'),'Table'[Date] < CurrentDate))
If this post helps, then please consider Accept it as the solution to help the others find it more quickly. Appreciate you kudos!! - Ashish_Mathur
Super User
Hi,
This calculated column formula works
Column = LOOKUPVALUE(Data[Value],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(Data,Data[340B_ID]=EARLIER(Data[340B_ID])&&Data[PHARMACY_ID]=EARLIER(Data[PHARMACY_ID])&&Data[Date]<EARLIER(Data[Date])&&Data[Manufacturer]=EARLIER(Data[Manufacturer])&&Data[Attribute]=EARLIER(Data[Attribute]))),Data[340B_ID],Data[340B_ID],Data[PHARMACY_ID],Data[PHARMACY_ID],Data[Manufacturer],Data[Manufacturer],Data[Attribute],Data[Attribute])Hope this helps.