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
dstramilov
Helper II
Helper II

Connect 2 Tables Using Time Between

Hello,

 

Got 2 tables:

Table 1

 

Health Plan Name | Health Plan Start Date | Health Plan End Date | Health Plan Cost Per Month

Plan A                    | 1/1/2014                      | 12/31/2014                | 400

Plan A                    | 1/1/2015                      | 12/31/2015                | 500

Plan A                    | 1/1/2016                      | 12/31/2016                | 600

 

Table 2

 

Employee ID | Health Plan Name  | Health Plan Select Start Date | Health Plan Select End Date

12345           |  Plan A                    | 5/1/2014                                 | 2/31/2015

12346           |  Plan A                    | 4/1/2016                                 |12/31/2078

12347           |  Plan A                    | 7/1/2015                                 |12/31/2078

 

I need to connect these tables so I can run report & find out how much each employee should pay for any give period.

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @dstramilov,

 

Based on your description, you want to get the summary pay of the employee, right?

 

If as I said, you can refer to below formula:

 

Total = 
var currStart=MAX(Sheet2[Health Plan Select Start Date])
var currEnd=MAX(Sheet2[Health Plan Select End Date])
var startRange=12-MONTH(currStart)+1
var endRange=MONTH(currEnd)
var startPerMonth= LOOKUPVALUE(Sheet1[Health Plan Cost Per Month],Sheet1[Health Paln Start Date].[Year],YEAR(currStart))
var EndPerMonth= LOOKUPVALUE(Sheet1[Health Plan Cost Per Month],Sheet1[Health Plan End Date].[Year],YEAR(currEnd))
return 
if(AND([Middle Date Start]<>BLANK(),[Middle Date End]<>BLANK()),SUMX(FILTER(ALL(Sheet1),AND(Sheet1[Health Paln Start Date]>=[Middle Date Start],Sheet1[Health Plan End Date]<=[Middle Date End])),Sheet1[Health Plan Cost Per Month]*12),0)+

startRange*startPerMonth+endRange*EndPerMonth

 

 

Tables:
 Capture.PNGCapture2.PNG

 

Measure to get the middle start date and end date:

 

Middle Date Start =
var currStart=MAX([Health Plan Select Start Date])
var currEnd= MAX([Health Plan Select End Date])
return
if(DATEDIFF(currStart,currEnd,DAY)>=365, DATE(YEAR( currStart)+1,1,1),BLANK())

 

Middle Date End =
var currStart = MAX([Health Plan Select Start Date])
var currEnd = MAX([Health Plan Select End Date])
return
if(DATEDIFF(currStart,currEnd,DAY)>=365, DATE(YEAR( currEnd)-1,12,31),BLANK())

 

Create visual to display the result:


Capture3.PNG 


Notice: I spilt the date range based on three points: start part , middle part, end part.(start date to 12.31, the full years range, 1.1 to end date)


Regards,
Xiaoxin Sheng

View solution in original post

4 REPLIES 4
ImkeF
Community Champion
Community Champion

Another option would be to add a column in the query editor, that would create one line for each month in both tables. You could then connect these tables by the date (either directly or via a calendar-table):

 

This formula in an added-custom columnn would create a list on you first table with all months-end-dates within the range:

 

List.Distinct(List.Transform({Number.From([Health Plan Start Date])..Number.From([Health Plan End Date])}, each Date.EndOfMonth(Date.From(_))))

 

 

Simply replace the bolded expressions by the column names of your 2nd table.

 

You then just have to expand this column and all the rows will be created automatically. Just load to the data model then.

 

It will drag on the performance, but seems a bit quicker to implement.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hi @dstramilov,

 

Based on your description, you want to get the summary pay of the employee, right?

 

If as I said, you can refer to below formula:

 

Total = 
var currStart=MAX(Sheet2[Health Plan Select Start Date])
var currEnd=MAX(Sheet2[Health Plan Select End Date])
var startRange=12-MONTH(currStart)+1
var endRange=MONTH(currEnd)
var startPerMonth= LOOKUPVALUE(Sheet1[Health Plan Cost Per Month],Sheet1[Health Paln Start Date].[Year],YEAR(currStart))
var EndPerMonth= LOOKUPVALUE(Sheet1[Health Plan Cost Per Month],Sheet1[Health Plan End Date].[Year],YEAR(currEnd))
return 
if(AND([Middle Date Start]<>BLANK(),[Middle Date End]<>BLANK()),SUMX(FILTER(ALL(Sheet1),AND(Sheet1[Health Paln Start Date]>=[Middle Date Start],Sheet1[Health Plan End Date]<=[Middle Date End])),Sheet1[Health Plan Cost Per Month]*12),0)+

startRange*startPerMonth+endRange*EndPerMonth

 

 

Tables:
 Capture.PNGCapture2.PNG

 

Measure to get the middle start date and end date:

 

Middle Date Start =
var currStart=MAX([Health Plan Select Start Date])
var currEnd= MAX([Health Plan Select End Date])
return
if(DATEDIFF(currStart,currEnd,DAY)>=365, DATE(YEAR( currStart)+1,1,1),BLANK())

 

Middle Date End =
var currStart = MAX([Health Plan Select Start Date])
var currEnd = MAX([Health Plan Select End Date])
return
if(DATEDIFF(currStart,currEnd,DAY)>=365, DATE(YEAR( currEnd)-1,12,31),BLANK())

 

Create visual to display the result:


Capture3.PNG 


Notice: I spilt the date range based on three points: start part , middle part, end part.(start date to 12.31, the full years range, 1.1 to end date)


Regards,
Xiaoxin Sheng

I think you are getting very close, I am getting this error:

 

"A table of multiple values was supplied where a single value was expected."

 

Could you send me actual .pbix file please.

 

Anonymous
Not applicable

Hi @dstramilov,

 

If below file is not help, plese feel free to let me know.

 

Regards,

Xiaoxin Sheng

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.

Top Kudoed Authors