Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to be able to calculate a measure in a table of many to many relationship. Here are my two tables:

Contracts Table

Serial#  ContractTyp  StartDate    EndDate
A        MP           1/1/2017    1/6/2018
B        ML           10/24/2017  6/30/2020
A        ML           1/6/2018    12/30/2019
C        MU           5/15/2018   1/1/2021

Performance Table

Serial#  Diff  Good  Bad  Date
A        15    1     0    1/30/2017
B        -24   1     0    12/17/2017
A        57    0     1    4/22/2017
A        18    1     0    2/1/2018
C        123   0     1    9/12/2018

So, my measure is simple. It just calculates the percentage of Good by Serial#.

NUM_GOOD = CALCULATE(COUNTA('Performance'[Good]),'Performance[Good] IN {"1"})
NUM_BAD = CALCULATE(COUNTA('Performance'[Bad]),'Performance[Bad] IN {"1"})
PERFORMANCE_METRIC = NUM_GOOD/(NUM_BAD + NUM_GOOD)

I need to be able to run that performance metric for every Serial# but only for when the machines were under a MP or MU ContractTyp. So I need it to be able to look at the Date in the Performance Table and make sure that date falls in the range between StartDate and EndDate on the Contracts Table where Serial# is MP or MU.

So I for example if I want to look at the Performance of all Serial#'s with a ContractTyp of MP or MU in the last 2 years I would want the result to look as follows:

Serial#  PERFORMANCE_METRIC
A        50%
B        100%
C        0%

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
184 views
Welcome To Ask or Share your Answers For Others

1 Answer

Below is a formula for a Calculated Column that you could add to the Performance table to return the True / False values (ref your comments).

To explain the logic:

  1. Store the Serial and Date values (from the current record) in variables.
  2. If we find at least 1 matching Contract with the required ContractTyp and Date range, return True, otherwise False.
Has Contract =
VAR vSerial = [Serial#]
VAR vDate = [Date]
RETURN
    IF (
        CALCULATE (
            COUNTROWS ( Contracts ),
            Contracts[Serial#] = vSerial,
            Contracts[ContractTyp] IN { "MP", "MU" },
            Contracts[StartDate] <= vDate,
            Contracts[EndDate] >= vDate
        ) > 0,
        TRUE (),
        FALSE ()
    )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...