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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
slanka
Helper I
Helper I

Column reference from related table with filter

Hello People,

 

I have two tables Project and Employee with structure as below,

 

Projects:

Employee NameProject NameStart DateEnd Date
Emp1Proj13/27/20194/2/2019
Emp1Proj24/3/20194/5/2019
Emp1Proj34/8/20194/9/2019
Emp2Proj14/3/20194/8/2019
Emp2Proj44/9/20194/10/2019

 

Employee:

Employee NameCalenderDate
Emp13/27/2017
Emp13/28/2017
Emp13/29/2017
Emp13/30/2017
Emp13/31/2017
Emp14/1/2017
Emp14/2/2017
Emp14/3/2017
Emp14/4/2017
Emp14/5/2017
Emp14/8/2017
Emp14/9/2017
Emp24/3/2019
Emp24/4/2019
Emp24/5/2019
Emp24/8/2019
Emp24/9/2019
Emp24/10/2019

 

I would need to populate 3rd column in Employee table, with filters as below:

1) IF Calender Date (which is generated from CALENDER function) falls between Start Date and End Date for any employee, populate that respective project (Group by employee, filter needs to be applied)

 

Final table should look like this:

Employee NameCalenderDateProject Name
Emp13/27/2017Proj1
Emp13/28/2017Proj1
Emp13/29/2017Proj1
Emp13/30/2017Proj1
Emp13/31/2017Proj1
Emp14/1/2017Proj1
Emp14/2/2017Proj1
Emp14/3/2017Proj2
Emp14/4/2017Proj2
Emp14/5/2017Proj2
Emp14/8/2017Proj3
Emp14/9/2017Proj3
Emp24/3/2019Proj1
Emp24/4/2019Proj1
Emp24/5/2019Proj1
Emp24/8/2019Proj1
Emp24/9/2019Proj4
Emp24/10/2019Proj4

 

On words, i would need a calculated column in Employee table like,

 

CALCULATE('Project'[ProjectName], If(CalenderDate >= StartDate && CalenderDate <= EndDate), GroupBy(EmployeeName))

 

Please advise

1 ACCEPTED SOLUTION

@slanka use following DAX to add new column in your employee table

 

Project Name = 
CALCULATE( 
    MAX( Proj[Project Name] ), 
    FILTER( 
        Proj, 
        Emp[CalenderDate] >= Proj[Start Date]  && 
        Emp[CalenderDate] <= Proj[End Date] && 
        Proj[Employee Name] = Emp[Employee Name]
    )
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

5 REPLIES 5
edhans
Super User
Super User

I would use Power Query for this. See my example PBIX file below. The merge is done there and has the date logic in it.

 

Power Query Example

 

When you open the file, select EDIT QUERIES to see your original tables, and the final merged table that is actually loaded into Power BI for reporting.

 

The formula that was used is:

=if ([CalenderDate] >= [Start Date] and [CalenderDate] <= [End Date]) then true else false

then I simply filtered for true for the final table.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Sorry, I forgot to mention that my Employee table is calculated table, hence i cannot use EDIT QUERIES for that

@slanka use following DAX to add new column in your employee table

 

Project Name = 
CALCULATE( 
    MAX( Proj[Project Name] ), 
    FILTER( 
        Proj, 
        Emp[CalenderDate] >= Proj[Start Date]  && 
        Emp[CalenderDate] <= Proj[End Date] && 
        Proj[Employee Name] = Emp[Employee Name]
    )
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k - do you have a link to an article or documentation as to why your solution works? I understand the logic of your CALCULATE() statement, but the Employee and Project tables cannot be related to each other, unless you use a MANY to MANY relationship.

 

But I left the tables unreleated and your solution works without any RELATED/RELATEDTABLE functions, so my real question is, why does this work, and what else works, without any relationships?

 

Is it as simple as CALCULATE() is itterating through each record of each table?



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

It Works! Thanks


@parry2k wrote:

@slankause following DAX to add new column in your employee table

 

Project Name = 
CALCULATE( 
    MAX( Proj[Project Name] ), 
    FILTER( 
        Proj, 
        Emp[CalenderDate] >= Proj[Start Date]  && 
        Emp[CalenderDate] <= Proj[End Date] && 
        Proj[Employee Name] = Emp[Employee Name]
    )
)

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.