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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
v-shex-msft
Community Support
Community Support

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

4 REPLIES 4
ImkeF
Super User
Super User

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

v-shex-msft
Community Support
Community Support

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

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.

 

Hi @dstramilov,

 

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

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.