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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
MianAhsan
Helper I
Helper I

Compare two date columns from table 1 and table 2, then add a column value from table 1

Hi everyone,

I have 2 tables(Table1 and Table2) with similar column names.

Table 1 has three columns: ID, CP, and Date.

IDCPDates
1LP16/30/2024
1LP212/30/2024
1PP16/30/2025
1PP212/30/2025
2LP16/30/2025
2LP212/30/2025
2PP16/30/2026
2PP212/30/2026

Table 2 also has the same columns, but we have to find CP based on the Date column here.

IDDateCP
19/15/2024 
12/15/2025 
15/28/2024 
18/30/2025 
112/25/2025 
25/30/2025 
29/30/2025 
22/30/2026 
28/30/2026 
23/30/2026 

My requirement/target is: 

The logic behind "compare two date columns based on ID, when Table 1 [Date] < Table 2 [Date] then the corresponding CP value is copied into the Table 2 CP column".

My result will look like this:

IDDateCP
19/15/2024LP2
12/15/2025PP1
15/28/2024 
18/30/2025PP2
112/25/2025 
25/30/2025 
29/30/2025 
22/30/2026 
28/30/2026 
23/30/2026 

I have already tried logic and created a measure, but I'm not getting the correct result:

 

CP = 
VAR cur_date =
    SELECTEDVALUE ('Table2'[Date])
RETURN
    CALCULATE (
        MAX ('Table1'[CP]),
        FILTER (ALL('Table1'), 'Table1'[Dates] < cur_date)
    )

 

MianAhsan_0-1724058677708.png The result should be 2nd row -> PP1 and 3rd row -> PP2.

I think my logic is not correct. It should be Table 1[Date] < Table 2 [Date] value and also compare previous value too.

 

I hope my question is clear to everyone. 

Thank you for your support in advance.
@SamWiseOwl 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi, @MianAhsan 

 

You can try the following methods.

Measure:

Nearest Date = CALCULATE(MIN('Table 1'[Dates]),FILTER(ALL('Table 1'),[Dates]>SELECTEDVALUE('Table 2'[Date])&&[ID]=SELECTEDVALUE('Table 2'[ID])))
New CP = CALCULATE(MAX('Table 1'[CP]),FILTER(ALL('Table 1'),[ID]=SELECTEDVALUE('Table 2'[ID])&&[Dates]=[Nearest Date]))

vzhangtinmsft_0-1724310401407.png

If it doesn't match your desired output, you'll also need to be told why there is no output for 5/28/2024 and 12/25/2025.

 

Best Regards,

Community Support Team _Charlotte

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

View solution in original post

Anonymous
Not applicable

Hi, @MianAhsan 

 

The X-axis cannot be put in measure. Only existing column values.

Nearest Date Column = CALCULATE(MIN('Table 1'[Dates]),FILTER('Table 1',[Dates]>EARLIER('Table 2'[Date])&&[ID]=EARLIER('Table 2'[ID])))
New CP Column = CALCULATE(MAX('Table 1'[CP]),FILTER('Table 1',[ID]=EARLIER('Table 2'[ID])&&[Dates]=[Nearest Date Column]))

vzhangtinmsft_0-1724659538952.png

It can be put into the bar chart X-axis with this column.

 

Best Regards,

Community Support Team _Charlotte

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

View solution in original post

10 REPLIES 10
Anonymous
Not applicable

Hi, @MianAhsan 

 

You can try the following methods.

Measure:

Nearest Date = CALCULATE(MIN('Table 1'[Dates]),FILTER(ALL('Table 1'),[Dates]>SELECTEDVALUE('Table 2'[Date])&&[ID]=SELECTEDVALUE('Table 2'[ID])))
New CP = CALCULATE(MAX('Table 1'[CP]),FILTER(ALL('Table 1'),[ID]=SELECTEDVALUE('Table 2'[ID])&&[Dates]=[Nearest Date]))

vzhangtinmsft_0-1724310401407.png

If it doesn't match your desired output, you'll also need to be told why there is no output for 5/28/2024 and 12/25/2025.

 

Best Regards,

Community Support Team _Charlotte

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

Hi @Anonymous ,

 

I'm trying to use the "New CP" measure to create a bar chart, where for the x-axis, I plan to use the measure, and for the y-axis "increased cost" column. However, I cannot use this measure alone in any of the visuals. Any ideas or comments related to this issue? 

Anonymous
Not applicable

Hi, @MianAhsan 

 

The X-axis cannot be put in measure. Only existing column values.

Nearest Date Column = CALCULATE(MIN('Table 1'[Dates]),FILTER('Table 1',[Dates]>EARLIER('Table 2'[Date])&&[ID]=EARLIER('Table 2'[ID])))
New CP Column = CALCULATE(MAX('Table 1'[CP]),FILTER('Table 1',[ID]=EARLIER('Table 2'[ID])&&[Dates]=[Nearest Date Column]))

vzhangtinmsft_0-1724659538952.png

It can be put into the bar chart X-axis with this column.

 

Best Regards,

Community Support Team _Charlotte

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

Hi @Anonymous ,
I checked your methods with my actual data and confirmed it with the manually created Next CP columns, and both results show me the same result.
Bundle of thanks for your support. 

Regards,

Mian

lbendlin
Super User
Super User

My requirement/target is: 

The logic behind "compare two date columns based on ID, when Table 1 [Date] < Table 2 [Date] 

What should happen if multiple rows from Table 1 satisfy that condition?

That is why I want to check based on ID if Table 1[Date] < Table2[Date] (another precheck, also consider the previous value from Table 2). 
For example, ID 1 has a value in Table 2 [9/15/2024], then check all values in Table 1 [Date] -> Two values to be considered here,

1LP16/30/2024
1LP212/30/2024

Where Table 2 Date value comes under the LP2 category because it is less than that date but greater than LP1.
I hope this clarifies your question. 

You need two steps. First you need to find the max date in Table 1 and then you need to fetch the CP value for that date.   Max(CP) is not correct.

Thank you for your response.
Any guide on how to follow these two steps? 

2/30/2026	 

good one !

 

Here is one option for the calculated column in Table 2

 

lbendlin_0-1724091941287.png

 

Thank you for being so supportive.

Unfortunately, I cannot get the correct result with that approach. 

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.