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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Calculated column with column = measure yields different result than column = hardcoded value

Hello,

 

I would like to add calculated columns indicating if a row contains the latest or second latest value in the file_date column.

This works well with the latest value, but does not with the second latest value - the result is always zero.

The second latest value calculation works when the "dynamic" second latest value is replaced with the hardcoded value of itself. The value calculated by the measure seems to be right, as the table in the second picture shows. The data types are the same for updates_most_recent, updates_second_most_recent.

 

Updates_second_most_recent is calculated the way it is to avoid a circular reference as described in this forum.

 

Thanks for your help and enjoy your weekend 😀

 

 

brst_0-1646991875476.png

brst_1-1646992022672.png

 

 

# measures, in measures table
updates_most_recent = max(updates_engagement_bei_updates[file_date])

updates_second_most_recent = var thisdate= calculate(max(updates_engagement_bei_updates[file_date]), ALLEXCEPT(updates_engagement_bei_updates,updates_engagement_bei_updates[file_date]))
var seconddate = CALCULATE(max(updates_engagement_bei_updates[file_date]),updates_engagement_bei_updates[file_date]<thisdate, ALLEXCEPT(updates_engagement_bei_updates,updates_engagement_bei_updates[file_date]))
return  seconddate

# calculated columns
is_latest = if(max(updates_engagement_bei_updates[file_date])=[updates_most_recent],1,0)
is_latest_hard = if(updates_engagement_bei_updates[file_date]=date(2022,3,9),1,0)
is_second_latest = if(max(updates_engagement_bei_updates[file_date])=('measure'[updates_second_most_recent]),1,0)
is_second_latest_hard = if((updates_engagement_bei_updates[file_date])=date(2022,2,9),1,0)

 

 

6 REPLIES 6
v-chenwuz-msft
Community Support
Community Support

Hi @Anonymous ,

 

Measure result is depend on the filter context, by for calculate column, you can consider it has filter context also, but its context is the whole table and won't be changed.

 

And you can test these columns in one table.

column1 = 'measure'[updates_second_most_recent]

column2 = date(2022,2,9)

 

Then try this code:

is_second_last_column =
VAR _topn =
    TOPN ( 2, VALUES ( 'table'[file_date] ), [file_date], DESC )
RETURN
    MINX ( _topn, [file_date] )

 

 

Best Regards

Community Support Team _ chenwu zhu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

One more question:

brst_0-1646996904754.png

rankx result is wrong - even though it's not a tie

 

Threads on rankx with datetime columns like Solved: Rank based on date for each individual user - Microsoft Power BI Community unfortunatelly did not work

mmm try RANKX just with table and date, It works for me. 

rank = RANKX(calc_table_updates, file_date)

Anonymous
Not applicable

Hmm thats how I did it - Re: Calculated column with column = measure yields... - Microsoft Power BI Community

rank = RANKX(calc_table_updates, [file_date],,,Skip)
Anonymous
Not applicable

Thank you, that works

 

Just for future reference, here are some details for the suggested solution by @AntonioHR 

brst_0-1646995493065.png

 

 

# calculated table
calc_table_updates = distinct(SELECTCOLUMNS(updates_engagement_bei_updates,"file_date", [file_date]))

# rank column of calculated table
rank = RANKX(calc_table_updates, [file_date],,,Skip)

 

brst_1-1646995561203.png

 

 

AntonioHR
Helper I
Helper I

Hello, 

I'm not sure if I have understand it correctly, but you could try creating a table (DISTINCT dates) and using RANKX on it. 1 and 2 will be the lastest and second lastest, so after you link the two tables using date you can add the calculated column.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Top Solution Authors