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
Adham
Helper III
Helper III

Filter created table based on multiple conditions

Hello All,

 

I hope you are all well. I have got the following two tables:

 

Tasks (Table 1):

 

Employee ID | Project ID | Task Start Date | Task Due Date

 

different employees can work on different projects and different projects will have multiple people working on them.

 

Employee Weeks (Table 2):

 

Date | Employee ID | Project ID | Work Hours

 

Table 2 is created using Table 1. The code i used is:

 

 

 

Employee Weeks = 
    SELECTCOLUMNS(
        ADDCOLUMNS(
            GENERATE(
                FILTER(
                    ADDCOLUMNS(
                        GENERATE(
                            DISTINCT(Tasks[Task Assignee Id]),
                            CALENDAR(MIN(Tasks[Task Start Date]),MAX(Tasks[Task Due Date]))
                        ),
                        "__IsWeekDay",IF(WEEKDAY([Date],3) < 5,TRUE(),FALSE())
                    ),
                    [__IsWeekDay] = TRUE()
                ),
                DISTINCT(Tasks[Project Id])
            ),
            "__Work Hours",8
        ),
        "Employee Id",[Task Assignee Id],
        "Project Id", [Project Id],
        "Date",[Date],
        "Work Hours",[__Work Hours]
    )

 

 

 

I want to filter table 2 to only have values where:

 

if a row in table 1 has equal values of employee id and project id to a row in table 2 and also for the date of the row of table 2 to be between the start and due date of the row of table 1.

 

I would really appreciate if someone can help me on this!

1 ACCEPTED SOLUTION

Hello @Greg_Deckler  and @v-frfei-msft ,

 

Thank you very much for your help. I actually solved it by running a python script since i am more proficient in python than DAX. Its really cool how power BI enables the use of python!!

View solution in original post

8 REPLIES 8
v-frfei-msft
Community Support
Community Support

Hi @Adham ,

 

Please try to get the filtered table by the following formula. If it doesn't meet your requirement,  kindly share your sample data and excepted result to me if you don't have any Confidential Information. Please upload your files to One Drive and share the link here.

 

Employee Weeks =
VAR k =
    SELECTCOLUMNS (
        ADDCOLUMNS (
            GENERATE (
                FILTER (
                    ADDCOLUMNS (
                        GENERATE (
                            DISTINCT ( Tasks[Task Assignee Id] ),
                            CALENDAR ( MIN ( Tasks[Task Start Date] ), MAX ( Tasks[Task Due Date] ) )
                        ),
                        "__IsWeekDay", IF ( WEEKDAY ( [Date], 3 ) < 5, TRUE (), FALSE () )
                    ),
                    [__IsWeekDay] = TRUE ()
                ),
                DISTINCT ( Tasks[Project Id] )
            ),
            "__Work Hours", 8
        ),
        "Employee Id", [Task Assignee Id],
        "Project Id", [Project Id],
        "Date", [Date],
        "Work Hours", [__Work Hours]
    )
VAR std =
    CALCULATE (
        MAX ( Tasks[Task Start Date] ),
        FILTER ( Tasks, [Employee ID] = [Employee Id] && [Project ID] = [Project Id] )
    )
VAR endd =
    CALCULATE (
        MAX ( Tasks[Task Due Date] ),
        FILTER ( Tasks, [Employee ID] = [Employee Id] && [Project ID] = [Project Id] )
    )
RETURN
    FILTER ( k, [Date] >= std && [Date] <= endd )

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

@Adham do you still need help with this, @v-frfei-msft 's solution looked pretty solid. 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hello @Greg_Deckler  and @v-frfei-msft ,

 

Thank you very much for your help. I actually solved it by running a python script since i am more proficient in python than DAX. Its really cool how power BI enables the use of python!!

amitchandak
Super User
Super User

Refer to my HR blog https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

There us current employee calculation. Take the filter part of it and move it inside the calculate table.

On top of that put a summarize and get the data

Hello @amitchandak ,

 

I am sorry i dont quite get you. Could you please provide an example?

 

Kind regards,

 

Adham

Try something like this. Not tested

summarize(
 CALCULATETABLE(Employee,filter(Employee,Employee[Start Date]<=max('Date'[Date]) && (ISBLANK(Employee[End Date]) || Employee[End Date]>max('Date'[Date]))),CROSSFILTER(Employee[Start Date],'Date'[Date],None)),Employee[ID],Employee[Project Id],'Date'[Date],"__Work Hours",8)

 

You might have to remove crosstable and date table join.

 

I am actually more confused now

JirkaZ
Solution Specialist
Solution Specialist

@Adham You'll have to create a measure to calculate whatever you need to calculate from table 2. This cannot be done using relationships and modelling. 

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.