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
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])

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])

@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
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.