Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Merging and Filtering related data with unique entries

I have 2 data tables with related data, and I'm having trouble relating the data between them.  I am trying to track the planning progress for an upcoming event.  The event scope is broken down on the worklist, which is broken down into jobcards, which are assigned to a primary contractor to plan (table 1).  The primary contractor breaks down a job card into packages, and then the packages are assigned to themselves as well as other contractors (sub contractors) (table2). 

Table 1:

WorklistJobcard...Primary Contractor
123ABC...A
456CDE...B
789FGH...C
101IJK...A
112LMN...C

Table 2:

WorklistJobcardPackage...Sub Contractor
123ABCABC.1...A
123ABCABC.2...B
456ABCABC.3...B
456CDECDE.1....B
101LMNLMN.1...C
112LMNLMN.2...C

 

These tables are used to track planning progress, so every line on each table is valuable information.    I want to relate the two tables by merging entries based on worklist# and job card number to identify progress in planning, but I also want to maintain the unique entries - which indicate a gap or lack of progress from the respective contractor.  I need to create visual tables that summarize data from both tables (see the visual table for dashboard)  

 

Visual table for dashboard:

ContractorJobcard Count by contractor
(Table 1)
Jobcard Count by Contractor
(table 2)
Package Count
(Table 2)
A211
B123
C212

 

I've tried establishing relationships (1:1, 1:*, *:*) between the two tables, but I lose the unique entries for one or both of the tables depending on how Power-BI cross filters.  I think I need to create a new calculated table, and establish 1:* relationships to each table.  I'm facing similar struggles when merging the fields into a calculated table (losing items that are unique to each table, and problems when "duplicate" items are in table 2.)  Ideally i'd like to see which scope items have been identified in table 1, but are missing from table 2 and vice versa.  I'm new to power bi so if anyone has another suggestion, let me know. 

 

Summary Table: 

WorklistJobcard
123ABC
456ABC
456CDE
101LMN
112LMN
789FGH
101IJK
  • Anonymous,

     

    This can be solved by creating a Contractor dimension table (star schema).

     

    1. Create a table with a distinct list of all primary contractors and subcontractors. This can be done in Power Query or DAX. Here's the DAX calculated table:

     

    Contractor = 
    DISTINCT (
        UNION (
            DISTINCT ( Table1[Primary Contractor] ),
            DISTINCT ( Table2[Sub Contractor] )
        )
    )

     

    2. Create relationships:

     

     

    3. Create measures:

     

    Jobcard Count by Primary Contractor (Table 1) = 
    VAR vCount =
        DISTINCTCOUNT ( Table1[Jobcard] )
    VAR vResult =
        IF ( ISBLANK ( vCount ), 0, vCount )
    RETURN
        vResult
    
    Jobcard Count by Primary Contractor (Table 2) = 
    VAR vCount =
        DISTINCTCOUNT ( Table2[Jobcard] )
    VAR vResult =
        IF ( ISBLANK ( vCount ), 0, vCount )
    RETURN
        vResult
    
    Package Count = 
    VAR vCount =
        COUNT ( Table2[Jobcard] )
    VAR vResult =
        IF ( ISBLANK ( vCount ), 0, vCount )
    RETURN
        vResult

     

    4. Create matrix using Contractor[Contractor] as rows (this is the Contractor dimension table):

     

     

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous 

    We usually create 1 to many relationship, and create measures for those fields that cannot provide expected output. Please provide a full short sample of the 2 tables and the exact expected output from that sample.

     

     

    Paul Zheng _ Community Support Team

    • Anonymous's avatar
      Anonymous
      Not applicable

      Samples of each table are below:

      Table 1:

      WorklistJobcard...Primary Contractor
      123ABC...A
      456CDE...B
      789FGH...C
      101IJK...A
      112LMN...C
      131OPQ...D

      Table 2:

      WorklistJobcardPackage...Sub Contractor
      123ABCABC.1...A
      123ABCABC.2...B
      456ABCABC.3...B
      456CDECDE.1....B
      101LMNLMN.1 C
      101LMNLMN.2...E
      112LMNLMN.3...C

       

      Desired Output table: 

      ContractorJobcard Count by Primary Contractor
      (Table 1)
      Jobcard Count by Primary Contractor
      (Counting from Table 2)
      Package Count
      (Table 2)
      A211
      B123
      C212
      D100
      E011

       

      The problem with the one to many relationship is that it will filter unique values from one or both of the sample tables: 

      * Contractor D was assigned 1 jobcard in table 1, but has not created any packages in table 2. 

      * Contractor E is not a primary contractor for any job card on table 1, but they are a subcontractor assigned Package LMN.2 for Jobcard 101-LMN (table 2). 

       

      I am also having trouble counting packages by contractor using measured values - I can only count packages when a specific contractor is the primary contractor and the subcontractor.  

      • DataInsights's avatar
        DataInsights
        Icon for Super User rankSuper User

        Anonymous,

         

        This can be solved by creating a Contractor dimension table (star schema).

         

        1. Create a table with a distinct list of all primary contractors and subcontractors. This can be done in Power Query or DAX. Here's the DAX calculated table:

         

        Contractor = 
        DISTINCT (
            UNION (
                DISTINCT ( Table1[Primary Contractor] ),
                DISTINCT ( Table2[Sub Contractor] )
            )
        )

         

        2. Create relationships:

         

         

        3. Create measures:

         

        Jobcard Count by Primary Contractor (Table 1) = 
        VAR vCount =
            DISTINCTCOUNT ( Table1[Jobcard] )
        VAR vResult =
            IF ( ISBLANK ( vCount ), 0, vCount )
        RETURN
            vResult
        
        Jobcard Count by Primary Contractor (Table 2) = 
        VAR vCount =
            DISTINCTCOUNT ( Table2[Jobcard] )
        VAR vResult =
            IF ( ISBLANK ( vCount ), 0, vCount )
        RETURN
            vResult
        
        Package Count = 
        VAR vCount =
            COUNT ( Table2[Jobcard] )
        VAR vResult =
            IF ( ISBLANK ( vCount ), 0, vCount )
        RETURN
            vResult

         

        4. Create matrix using Contractor[Contractor] as rows (this is the Contractor dimension table):