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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

Return value if date is between 2 dates in another table

I have two tables, Calendar and Position, I'm trying to add a calculated column to Calendar that shows what department the NetID belonged to on that date.

 

Ex: It will find all the positions "a" had, then find the one where the "Date" is between "Start Date" and "End Date", and return the "Department".

 

Normally I would run 2 loops to get the answer, but not sure how to do this in Power BI as I'm quite new, please help thanks!

Calendar

DatesNetID
1/1/2020a
1/1/2020b
1/1/2020c
1/2/2020a
1/2/2020b
1/2/2020c
1/3/2020a
1/3/2020b
1/3/2020c

 

Position

NetIDStart DateEnd DateDepartment
a10/1/201912/1/2019Dining
a12/2/20193/1/2050Res life
b12/5/20193/1/2050Housing
c10/31/201811/5/2019Housing
c11/6/20193/1/2050Dining
2 ACCEPTED SOLUTIONS
VasTg
Memorable Member
Memorable Member

@Anonymous 

 

I second the approach by @JarroVGIT , but I would drop the fact and expand the table as he/she mentioned. Then define the relationship with Dates and the new fact.

 

Refer to these posts.

 

https://community.powerbi.com/t5/Desktop/Calculate-Sum-in-Period-Where-Periods-Differ-By-Project/m-p/886536

 

 

Column = CALCULATE(MAX(Position[Department]),FILTER(ALL(Position),Position[Start Date]>=Calendar[Dates] && Position[End Date]<=Calendar[Dates] && Calendar[NetID] = Position[NetID]))

 

 

If this helps, mark it as a solution

Kudos are nice too 

Connect on LinkedIn

View solution in original post

Hi @Anonymous,

Sorry for delay in response! You are right, you can't delete columns from a calculated table. (It does make sense but I didn't know that either untill you said so :P).

You can circumvent this by using this calculated table instead (is uses SELECTCOLUMNS to select only the columns that you want):

Calendar = 
VAR _tmpTable = GENERATE(Positions, 
    VAR _start = Positions[Start Date]
    VAR _end = Positions[End Date]
    RETURN
    GENERATESERIES(_start, _end))
RETURN
SELECTCOLUMNS(_tmpTable, "Date", [Value], "NetID", [NetID])

Don't forget to like posts that helped you out (as a motivator :)) and to mark posts as solution if it provided the solution for you 🙂

Good night!

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Keep those thumbs up coming! 🙂





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

Proud to be a Super User!




View solution in original post

6 REPLIES 6
btfergie
Helper I
Helper I

Perfectly what I needed.

VasTg
Memorable Member
Memorable Member

@Anonymous 

 

I second the approach by @JarroVGIT , but I would drop the fact and expand the table as he/she mentioned. Then define the relationship with Dates and the new fact.

 

Refer to these posts.

 

https://community.powerbi.com/t5/Desktop/Calculate-Sum-in-Period-Where-Periods-Differ-By-Project/m-p/886536

 

 

Column = CALCULATE(MAX(Position[Department]),FILTER(ALL(Position),Position[Start Date]>=Calendar[Dates] && Position[End Date]<=Calendar[Dates] && Calendar[NetID] = Position[NetID]))

 

 

If this helps, mark it as a solution

Kudos are nice too 

Connect on LinkedIn
Anonymous
Not applicable

@VasTg 

This seems to have worked too after changing it to 

"Position[Start Date]<=Calendar[Dates] && Position[End Date]>=Calendar[Dates]". I added it as a calculated column onto the Calendar table.
 
What do you mean by "drop the fact"? And could you explain the logic behind using MAX here? Thank you!

 

JarroVGIT
Resident Rockstar
Resident Rockstar

Hi @Anonymous ,

My advice would be to drop the Calendar table and create it dynamically from your Position data with the following DAX:

Calendar = GENERATE(Positions, 
    VAR _start = Positions[Start Date]
    VAR _end = Positions[End Date]
    RETURN
    GENERATESERIES(_start, _end))

That returns a row for every date between start date en end date for every row in the first table. It returns a couple more columns from the Positions table but you can delete those if you don't need those. Does this meet your requirements?

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Keep those thumbs up coming! 🙂

 





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

Proud to be a Super User!




Anonymous
Not applicable

@JarroVGIT 

This does help me get what I need! How would I remove the other columns in this new table though? The delete option is greyed out when I select a column and the table is not showing up in power query.

Hi @Anonymous,

Sorry for delay in response! You are right, you can't delete columns from a calculated table. (It does make sense but I didn't know that either untill you said so :P).

You can circumvent this by using this calculated table instead (is uses SELECTCOLUMNS to select only the columns that you want):

Calendar = 
VAR _tmpTable = GENERATE(Positions, 
    VAR _start = Positions[Start Date]
    VAR _end = Positions[End Date]
    RETURN
    GENERATESERIES(_start, _end))
RETURN
SELECTCOLUMNS(_tmpTable, "Date", [Value], "NetID", [NetID])

Don't forget to like posts that helped you out (as a motivator :)) and to mark posts as solution if it provided the solution for you 🙂

Good night!

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Keep those thumbs up coming! 🙂





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

Proud to be a Super User!




Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.