Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

dax - Many to Many Relationship BY DATE in PowerBI

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

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

1 Reply

0 votes
by (71.8m points)

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 ()
    )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...