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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
cj_oat
Helper I
Helper I

To lookup text from another table with the closest value (smaller/larger)

Hi everyone,

 

I would like to seek your advice as I'm trying to create a column to lookup text from another table based on closest value. Here is the table that I have;

 

This is Table1 where there is the data for product status

 

cj_oat_0-1723223057568.png

 

and table2 which I want to lookup product status in each month from Table1 but if the Month in Table2 is not available in Table1 then go to get the status from closest month e.g. 202401 go to get status from 202404 in Table1 while 202409 go to get status from 202408 in Table1 

cj_oat_1-1723223448430.png

 

This is what I'm using but it did not give me the result I expected

 

Product Status =
VAR TimeMonth = 'Table2'[Month]
VAR PastMonth = maxx(FILTER(ALL('Table1'[Month]),'Table1'[Month] <= TimeMonth),'Table1'[Month])

Return if(LOOKUPVALUE('Table1'[Product Status],'Table1'[Month],PastMonth,'Table1'[SKU],'Table2'[SKU])=BLANK(),"Active",LOOKUPVALUE('Table1'[Status],'Table1'[Month],PastMonth,'Table1'[SKU],'Table2'[SKU]))

 

This is the result that I get while I'm expecting the result in yellow column

cj_oat_2-1723223665996.png

 

Any advices or guidance would be really appreciated. Thank you!

1 ACCEPTED SOLUTION

Hi,

Write this calculated column formula in Table2

Closest month = LOOKUPVALUE(Table1[Product status],Table1[Month],coalesce(CALCULATE(MAX(Table1[Month]),FILTER(Table1,Table1[SKU]=EARLIER(Table2[SKU])&&Table1[Month]<=EARLIER(Table2[Month]))),CALCULATE(min(Table1[Month]),FILTER(Table1,Table1[SKU]=EARLIER(Table2[SKU])&&Table1[Month]>=EARLIER(Table2[Month])))),Table1[SKU],Table2[SKU])

Hope this helps.

Ashish_Mathur_0-1723269137970.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

7 REPLIES 7
cj_oat
Helper I
Helper I

Hi @Greg_Deckler and @NaveenGandhi ,

 

Thanks for the guidance and support! However, the result is still different from what I expected.

 

I have also input column for the result after putting DAX from both of your advices in Table 2 below,

 

My apology that in the first post I did not mention that each SKU will have different active/expired status in different month and each SKU will also have different lenght of month in Table 1 as well (e.g. SKU A0001 only have status from month 202404 - 202408 while SKU A0002 have status from month 202401-202411) while in Table 2 I want to maintain the same lenght of month from 202401 - 202412 for all SKU. So not sure if this is the reason that make it did not work...

 

Please let me know if anything I should further adjust in DAX

 

Table 1

A0001202404Active
A0001202405Active
A0001202406Active
A0001202407Expired
A0001202408Expired
A0002202401Active
A0002202402Active
A0002202403Active
A0002202404Active
A0002202405Active
A0002202406Active
A0002202407Active
A0002202408Active
A0002202409Active
A0002202410Expired
A0002202411Expired
A0003202405Active
A0003202406Active
A0003202407Active
A0003202408Active
A0003202409Expired

 

Table 2

Table 2   Expected Result
SKUMonthGreg adviceNaveen adviceProduct Status
A0001202401 ActiveActive
A0001202402 ActiveActive
A0001202403 ActiveActive
A0001202404ActiveActiveActive
A0001202405ActiveActiveActive
A0001202406ActiveActiveActive
A0001202407ExpiredExpiredExpired
A0001202408ExpiredExpiredExpired
A0001202409 ActiveExpired
A0001202410 ActiveExpired
A0001202411 ActiveExpired
A0001202412 ActiveExpired
A0002202401ActiveActiveActive
A0002202402ActiveActiveActive
A0002202403ActiveActiveActive
A0002202404ActiveActiveActive
A0002202405ActiveActiveActive
A0002202406ActiveActiveActive
A0002202407ActiveActiveActive
A0002202408ActiveActiveActive
A0002202409ActiveActiveActive
A0002202410ExpiredExpiredExpired
A0002202411ExpiredExpiredExpired
A0002202412ExpiredExpiredExpired
A0003202401 ActiveActive
A0003202402 ActiveActive
A0003202403 ActiveActive
A0003202404 ActiveActive
A0003202405ActiveActiveActive
A0003202406ActiveActiveActive
A0003202407ActiveActiveActive
A0003202408ActiveActiveActive
A0003202409ExpiredExpiredExpired
A0003202410ExpiredActiveExpired
A0003202411ExpiredActiveExpired
A0003202412ExpiredActiveExpired

Hi,

Write this calculated column formula in Table2

Closest month = LOOKUPVALUE(Table1[Product status],Table1[Month],coalesce(CALCULATE(MAX(Table1[Month]),FILTER(Table1,Table1[SKU]=EARLIER(Table2[SKU])&&Table1[Month]<=EARLIER(Table2[Month]))),CALCULATE(min(Table1[Month]),FILTER(Table1,Table1[SKU]=EARLIER(Table2[SKU])&&Table1[Month]>=EARLIER(Table2[Month])))),Table1[SKU],Table2[SKU])

Hope this helps.

Ashish_Mathur_0-1723269137970.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks @Ashish_Mathur ! This just works well for me and I get the result as expcted, appreciated your support!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Greg_Deckler
Community Champion
Community Champion

@cj_oat Without sample data as text that I can copy and paste, I can't test this fully but something like this should work:

Column in T2 = 
  VAR __SKU = [SKU]
  VAR __Month = [Month]
  VAR __MinMonth = MIN( 'Table 1'[Month] )
  VAR __MaxMonth = MAX( 'Table 1'[Month] )
  VAR __Match = MAXX ( FILTER( 'Table 1', [SKU] = __SKU && [Month] = __Month ), [Product Status] )
  VAR __Result = 
    SWITCH( TRUE(),
      __Match <> BLANK(), __Match,
      __Month < __MinMonth, MAXX ( FILTER( 'Table 1', [SKU] = __SKU && [Month] = __MinMonth ), [Product Status] ),
      MAXX ( FILTER( 'Table 1', [SKU] = __SKU && [Month] = __MaxMonth ), [Product Status] )
    )
RETURN
  __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
NaveenGandhi
Super User
Super User

HI @cj_oat 

You have almost done it right, The below DAX works.

Product Status =
VAR TimeMonth = 'Table 2'[Month]
VAR PastMonth =
MAXX (
FILTER ( ALL ( 'Table 1'[Month] ), 'Table 1'[Month] <= TimeMonth ),
'Table 1'[Month]
)
VAR AlteredMonths =
IF (
ISBLANK ( PastMonth ),
MINX (
FILTER ( ALL ( 'Table 1'[Month] ), 'Table 1'[Month] >= TimeMonth ),
'Table 1'[Month]
),
PastMonth
)
RETURN
IF (
LOOKUPVALUE (
'Table 1'[Product Status],
'Table 1'[Month], AlteredMonths,
'Table 1'[SKU], 'Table 2'[SKU]
)
= BLANK (),
"Active",
LOOKUPVALUE (
'Table 1'[Product Status],
'Table 1'[Month], AlteredMonths,
'Table 1'[SKU], 'Table 2'[SKU]
)
)

NaveenGandhi_0-1723225585917.png



All i have done is add a new variable altered months, as the pastmonth variable only consideres months lesser than current month. 1st SS is for past month and 2nd is for alteredmonths.

NaveenGandhi_1-1723225674689.png

 

NaveenGandhi_2-1723225688957.png

 

 




Let me know if this helps.

If this post helps, then please consider Accept it as the solution to help the others find it more quickly. Appreciate you kudos!!

Follow me on LinkedIn!!!




SamWiseOwl
Super User
Super User

This will pull through the highest date on or before the current date.

Return Status =
var sku = [SKU]
var rowmonth = [Month]
var filttable = Filter('Table 1', [SKU] = sku && [Month] <= rowmonth)
RETURN

     MAXx( TOPN( 1,filttable,[Month],DESC),[Product Status])

If you are happy with this answer please mark as a solution for others to find !

Kudos are always appreciated! Check out our free Power BI video courses.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors