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
Anonymous
Not applicable

Creating a measure between column of separate linked tables that can be filtered

I have two tables, DEFECTS and SAMPLE that I've created simple examples of below.

 

The DEFECTS table has a record for each defect recorded along with info about the defect and a key to connect it to the SAMPLE table.  The key is a combination of the defect descriptors.

DateShiftModelKey
2/24/2020

1

A202002241A
2/24/20202A202002242A
2/24/20202B202002242B
2/25/20201A202002251A
2/25/20201B202002251B
2/25/20201A202002251A
2/25/20202A202002252A
2/25/20202A202002252A
2/25/20202B202002252B
2/25/20202B202002252B

 

The SAMPLE table has a record for each date/shift/model and the volume (Sample) that was produced.

DateShiftModelSampleKey

2/24/2020

1A20202002241A
2/24/20201B15202002241B
2/24/20202A21202002242A
2/24/20202B13202002242B
2/25/2020

1

A19202002251A
2/25/20201B18202002251B
2/25/20202A22202002252A
2/25/20202B11202002252B

 

I'm trying to create a measure that is DPU (defects per unit produced) so that I can plot it by date.  I want to be able to slice/filter the plot by shift and model.  The DPU is calculated by dividing the number of defects by the total units produced (Sample).  As I filter for shift and model, I need the defect count and the sample size to change accordingly.

 

I think I need something like what I've shown below, but I can't get the FILTER function to allow me to use a boolean expression that uses columns from two different tables.

 

DPUMeasure = CALCULATE(COUNT(DEFECTS[Date] / SUM(SAMPLE[Sample]), FILTER(DEFECTS[Key] = SAMPLE[Key]))

9 REPLIES 9
amitchandak
Super User
Super User

Try that you create a common key table . Or can sample act as master 

 

New column in sample 
//add DEFECTS[shift] = SAMPLE[shift] optional
Total defect in sample table = countx(filter(DEFECTS[Key] = SAMPLE[Key]),DEFECTS[Date])

New measure when key is master
averagex(summarize(Key[Key],"_cnt",COUNT(DEFECTS[Date]),"_sum",SUM(SAMPLE[Sample])),divide([_cnt],[_sum]))

 

New measure when sample is master
averagex(summarize(SAMPLE[Key],"_cnt",COUNT(DEFECTS[Date]),"_sum",SUM(SAMPLE[Sample])),divide([_cnt],[_sum]))

Anonymous
Not applicable

Amit,

 

I'm having trouble following the first option you gave where I create a common key table and use key as master.  I don't know what you mean by creating a common key table.

 

I tried the second option where sample is master.  It gives the same result as Greg_Deckler's solution.  I replied to him with the results (shown below).

 

"I tried creating your measure, but it doesn't seem to be calculating correctly.

 

For example, DPU for 2/24/2020 should be 0.04 (3 defects / 69 units) and for 2/25/2020 should be 0.10 (7 defects / 70 units).  However, the measure gives me 0.02 for 2/24/2020 and 0.05 for 2/25/2020.

 

Then when I filter/slice by shift 1, DPU for 2/24/2020 should be 0.03 (1 defects / 35 units) and for 2/25/2020 should be 0.08 (3 defects / 37 units).  However, the measure gives me 0.01 for 2/24/2020 and 0.02 for 2/25/2020.

 

I think the problem is that the final DPU calculation is still dividing by the total sample for both days (139) regardless of how I plot it (say by date) or filter/slice it."

Anonymous
Not applicable

I think I may be close.  I tried:

 

DPU = CALCULATE(COUNTROWS(DEFECTS) / SUM('SAMPLE'[Sample]), FILTER(DEFECTS, RELATED('SAMPLE'[Key]) = DEFECTS[Key]))
 
It gives me the correct answer for most scenarios but not all.
Anonymous
Not applicable

I figured out why this formula won't work in all situations, but I don't know how to solve the issue.

 

DPU = CALCULATE(COUNTROWS(DEFECTS) / SUM('SAMPLE'[Sample]), FILTER(DEFECTS, RELATED('SAMPLE'[Key]) = DEFECTS[Key]))

 

This formula is summing the sample size based on the key from defects.  So if there is no defect for a particular date/shift/model, it won't include that sample in the sum even though it is needed for the calculation.

 

Any ideas?  I'm thinking I may have to create some kind of user inputs that become variables to sum up the correct sample size.

Greg_Deckler
Super User
Super User

I did it like this but this currently depends on a 1:many relationship between the tables which if that is not the case I think is still fixable.

 

DPU = 
    VAR __Table = 
        ADDCOLUMNS(
            GROUPBY(
                'SAMPLE',
                [Key],
                "__Sum",SUMX(CURRENTGROUP(),[Sample])
            ),
            "__Defects",COUNTROWS(RELATEDTABLE(DEFECTS))
        )
RETURN
    DIVIDE(
        SUMX(__Table,[__Defects]),
        SUMX(__Table,[__Sum]),
        0
    )

 PBIX attached. 


@ 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...
Anonymous
Not applicable

Greg,

 

I tried creating your measure, but it doesn't seem to be calculating correctly.

 

For example, DPU for 2/24/2020 should be 0.04 (3 defects / 69 units) and for 2/25/2020 should be 0.10 (7 defects / 70 units).  However, the measure gives me 0.02 for 2/24/2020 and 0.05 for 2/25/2020.

 

Then when I filter/slice by shift 1, DPU for 2/24/2020 should be 0.03 (1 defects / 35 units) and for 2/25/2020 should be 0.08 (3 defects / 37 units).  However, the measure gives me 0.01 for 2/24/2020 and 0.02 for 2/25/2020.

 

I think the problem is that the final DPU calculation is still dividing by the total sample for both days (139) regardless of how I plot it (say by date) or filter/slice it. 

Greg_Deckler
Super User
Super User

So these tables are not related?

 

Can you create a bridge table of unique values and relate them?


@ 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...
Anonymous
Not applicable

I have the tables related by the Key column in each table.  Defects[Key]:Sample[Key] as a Many:1 connection.

@Anonymous - OK, then we should be good then?


@ 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...

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.