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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
POSPOS
Post Partisan
Post Partisan

How to compare two dates from different tables and populate the output

Hi All,

I have a requirement as below.

I have a table called as  "Final Table". In this table I need to add a new derived column called as "Total Hours"

Sample file is attached here

 

Final Table:

POSPOS_0-1760459772642.png

Logic for "Total Hours" :  I need to check the "lookup table" (shown below).

For each ID from the final table, take each "Pay Period End Data"  and check the "Current Date" column in the "Lookup Table".

eg: for ID 2000,

 1) first data is 6/16/2024. Check this in the lookup table "current date" column and aggregate all the hours <=6/16/2024.. In this case, there is no date less than 6/16/2024 then leave it blank.

2) next date is 6/30/2024. Again, check this in the current date column i.e., check for all dates greater than the previous date and less than or equal to the pay period end date, that is >6/17/2024 and <=6/30/2024 and then aggregate the Number of hours, so it will be 6/21+6/24+6/25+6/26+6/28=>8.25+9.25+9.25+9.00+8.25..

3) next date us 7/14/2024. Here, the dates will be >6/30/2024 and <=7/14/2024

 

Repeat this for every ID.

 

Lookup Table:

Lookup table will gave full history data

 

POSPOS_3-1760460722168.png

 

Can someone please advise on how to acheive this?

 

Thank You.

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@POSPOS,

 

Try this calculated column in table 'data-1':

 

Total Hours = 
VAR vID = 'data-1'[ ID]
VAR vPayPeriodEndDate = 'data-1'[Pay Period End Date]
VAR vPayPeriodEndDatePrevious =
    MAXX (
        FILTER (
            'data-1',
            'data-1'[ ID] = vID
                && 'data-1'[Pay Period End Date] < vPayPeriodEndDate
        ),
        'data-1'[Pay Period End Date]
    )
VAR vResult =
    SUMX (
        FILTER (
            'data-2',
            'data-2'[ID] = vID
                && 'data-2'[Current Date] > vPayPeriodEndDatePrevious
                && 'data-2'[Current Date] <= vPayPeriodEndDate
        ),
        'data-2'[Sum of Number of hours]
    )
RETURN
    vResult

 

DataInsights_0-1760472538905.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
Praful_Potphode
Resolver II
Resolver II

Hi @POSPOS 
Please try the solution suggested by @DataInsights  


I have taken cue from the same solution and created measure which also give similar results.

Hours = 
var max_date=MAX('data-1'[Pay Period End Date])
var prev_date=MAXX(FILTER(ALL('data-1'),'data-1'[Pay Period End Date]<max_date),'data-1'[Pay Period End Date])
RETURN 
CALCULATE(SUM('data-2'[Sum of Number of hours]),
'data-2'[Current Date]>prev_date
,'data-2'[Current Date]<=max_date
)

for this to work i created a relationship between two tables using ID column(this is a many to many relationship  with cross filter direction set to both)

 

Thanks and Regards,

Praful

DataInsights
Super User
Super User

@POSPOS,

 

Try this calculated column in table 'data-1':

 

Total Hours = 
VAR vID = 'data-1'[ ID]
VAR vPayPeriodEndDate = 'data-1'[Pay Period End Date]
VAR vPayPeriodEndDatePrevious =
    MAXX (
        FILTER (
            'data-1',
            'data-1'[ ID] = vID
                && 'data-1'[Pay Period End Date] < vPayPeriodEndDate
        ),
        'data-1'[Pay Period End Date]
    )
VAR vResult =
    SUMX (
        FILTER (
            'data-2',
            'data-2'[ID] = vID
                && 'data-2'[Current Date] > vPayPeriodEndDatePrevious
                && 'data-2'[Current Date] <= vPayPeriodEndDate
        ),
        'data-2'[Sum of Number of hours]
    )
RETURN
    vResult

 

DataInsights_0-1760472538905.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@DataInsights  - Thank you for providing this solution. It worked!!

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.