Forum Discussion

Alex_Power_BI's avatar
Alex_Power_BI
Regular Visitor
2 years ago
Solved

Comparing if a value in one table can be found in another table through relationships

Hello,

 

I'm struggling to create a DAX measure solution for the following scenario:

 

We have employees that record hours in a virtual timecard system. This is a corporate dataset that I'm bringing into my Power BI data model. The managers in our department keep their own Excel sheets to assign their direct reports projects and to keep track of their assignments. I import their excel sheets into the data model and relate them to the dataset via Direct Query. I would like to create a measure that compares the projects for an employee on the manager assignement sheet to the projects employees record time against in the virtual time card system (and vice versa). I have example tables below simplifying the data.

 

The data tables in the data set include: 

Supervisor_Table

EmployeeSupervisorEmployee ID
AlexSarah101
BrianSarah102
ChrisSam103
DanSarah104
FrankSam

105

 

Resources

Employee IDCost CenterResource Code
101A12001
102A12002
103A22003
104A12004
105A22005

 

Hours

Resource CodeHoursActivity Code
200120W
200115X
20015Y
200218Z
200222W
200340X
20049Y
200431Z
200535Y

 

Activity

Activity CodeProject CodeProject Name
W31Project A
X31Project A
Y32Project B
Z33Project C

 

The manager assignment tracker table looks like:

Assignments

EmployeeEIDProject Name
Alex101Project A
Alex101Project B
Alex101Project C
Brian102Project A
Brian102Project C
Chris103Project A
Chris103Project C
Dan104Project C
Dan104Project A
Frank105Project B
Frank105Project A

 

The data model I built from these tables is depicted below:

 

 

The resulting tables I'd like to get are:
Result 1 (Comparing Manager Assignments to Timecard)

EmployeeEIDProject NameMatches Timecard?
Alex101Project ATRUE
Alex101Project BTRUE
Alex101Project CFALSE
Brian102Project ATRUE
Brian102Project BFALSE
Brian102Project CTRUE
Chris103Project ATRUE
Dan104Project CTRUE
Dan104Project AFALSE
Frank105Project BTRUE
Frank105Project AFALSE

 

Result 2 (Comparing Timecard projects to Manager Assignments)

EmployeeResource CodeHoursActivity CodeProject NameMatches Manager Assignment
Alex200120WProject ATRUE
Alex200115XProject ATRUE
Alex20015YProject BTRUE
Brian200218ZProject CTRUE
Brian200222WProject ATRUE
Chris200340XProject ATRUE
Dan20049YProject BFALSE
Dan200431ZProject CTRUE
Frank200535YProject BTRUE

 

Any help would be greatly appreciated. Thank you in advance!

3 Replies

    • Alex_Power_BI's avatar
      Alex_Power_BI
      Regular Visitor

      Thank you for the help! This is great! I did work on it a little more after I posted this and came up with the following:

      MatchesTimecard =
          VAR selectedProjectAssignments = SELECTEDVALUE('Assignments'[Project Name])
          VAR selectedEmployeeAssignments = SELECTEDVALUE(Assignments[Employee])
         
          //Variable for counting if line item exists in Timecard
          VAR MatchExist =
          COUNTX(
              CALCULATETABLE('Hours',
              filter('Supervisor_Table', 'Supervisor_Table'[Employee] = 'Assignments'[selectedEmployeeAssignments]),
              filter('Activity', 'Activity'[Project Name] = 'Assignments'[selectedProjectAssignments])
          ), 'Hours'[Hours]) > 0

          //Variable for counting if line item exists in the original Assignments sheet
          VAR AssignmentExist =
          CALCULATE(  
              COUNTROWS('Assignments'),
              filter('Assignments', 'Assignments'[Employee] = 'Assignments'[selectedEmployeeAssignments]),
              filter('Assignments', 'Assignments'[Project Name] = 'Assignments'[selectedProjectAssignments])
          ) > 0

          Return MatchExist + AssignmentExist - 1
       
      I couldn't quite get the T/F and was uing another measure to filter the table.
       
      Just curious, the measure filter is needed because of the Cartesian Product of the data tables right?
      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        Yes, without it too many combinations would show.