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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Veblengood
Helper I
Helper I

Creating calculated column with max date from different table

I have a table with instore transactional data - table A, with data for each day, each product, each  sale and a table with detailed product conditions, e.g. cogs and profit for each product, which varyies by date - table B. The tables are indirectly connected with master data, one for date and for products with corresponding columns.

 

Table A

ProductQuantityMonth-year
A101.04.2021
B201.04.2022

 

Table B

ProductNSVMonth-year
A1001.05.2021
B3001.04.2022

 

I am trying to create a calculated column in table A, retreiving NSV-conditions from Table B with the powerbi lookup function. The issue is that not all months per year match with Table B, some will those retrieve blank values. Table B may lack some months, however, Table A will have all months included in the data set.  My idea is that if the lookup function retrieves blank values, then it will retrieve the max month-year value from table B under the condition that this max date is smaller than month-year from Table A in the given row.

 

I know this code is horrible, however, this is what I have tried:

 

NSV=
var mont_year=TableA[month-year]
return
if(isblank(LOOKUPVALUE(tableB[NSV] ,tableB[Product],TableA[Product], TableB[Month-year],'TableA[Month-year])),
LOOKUPVALUE(tableB[NSV] ,tableB[Product],TableA[Product], TableB[Month-year],calculate(max(TableB[Month-year]), allexcept(tableB[Product]), tableB[month-year]<manedar), LOOKUPVALUE(tableB[NSV] ,tableB[Product],TableA[Product], TableB[Month-year],'TableA[Month-year]))
 
The main issue with the formula is that calculate(max(TableB[Month-year]), allexcept(tableB[Product]), tableB[month-year]<manedar), does not retrieve the max date, but returns blank values. 
 
Do you have any thoughts?
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Veblengood , Try a new column like

 

new column =
var _max = maxx(filter(Table, TableB[Month-year] <= TableA[Month-year] && TableB[Product] = TableA[Product]),TableB[Month-year])
return
maxx(filter(Table, TableB[Month-year] =_max && TableB[Product] = TableA[Product] ),TableB[NSV])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

4 REPLIES 4
v-easonf-msft
Community Support
Community Support

Hi, @Veblengood 

The solution provided by @Veblengood  seems to work.

15.png

If your problem has been solved, please accept the reply as solution to close this thread. Other community members will easily find the solution when they get the same issue.

 

Best Regards,
Community Support Team _ Eason

 

johnt75
Super User
Super User

You could use TOPN, e.g.

var monthYear = TableA[Month-year]
var prod = TableA[Product]
RETURN SELECTCOLUMNS(
    TOPN( 1,
        CALCULATETABLE( 
            TableB, 
            TableB[Month-year] <= monthYear,
            TREATAS( { prod }, TableB[Product] )
        ),
        TableB[Month-year]
    ),
    "NSV", TableB[NSV]
)

amitchandak
Super User
Super User

@Veblengood , Try a new column like

 

new column =
var _max = maxx(filter(Table, TableB[Month-year] <= TableA[Month-year] && TableB[Product] = TableA[Product]),TableB[Month-year])
return
maxx(filter(Table, TableB[Month-year] =_max && TableB[Product] = TableA[Product] ),TableB[NSV])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak

Thanks a lot, this did it for me! Just curious, do you use the maxx function in the last part of the syntax (maxx(filter(Table, TableB[Month-year] =_max && TableB[Product] = TableA[Product] ),TableB[NSV])) instead of lookup(), because maxx is faster and more efficient? Both should be able to retrieve the same result in this case.

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors