Many to Many Relationship BY DATE in PowerBI

一个人想着一个人 提交于 2020-01-26 03:59:25

问题


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!


回答1:


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



回答2:


One way to do it would be to create a new table in Power BI by converting the Contracts table which is at a Serial #, Start date and End date level to a Serial # and Date level. For example if the data has the following records:

ID Type Start      End

1  MP    1/1/2019  1/2/2019

You should convert it into something like this:

ID Type Date

1 MP 1/1/2019

1 MP 1/2/2019

The following link should help you out regarding this:

https://natechamberlain.com/2018/08/08/how-to-add-rows-for-dates-between-start-and-end-dates-in-power-bi-date-range-data/

Once the table is converted, you should be able to do a simple join based on Serial# and date and it will be a one to one relationship. Then you can apply whatever filter you are looking for.

One caveat here is that, the size of the data would matter a lot. If the Contracts table has a reasonable number of records, this would work, otherwise the data might blow up.




回答3:


Adding another possible answer using merge queries. Instead of creating a relationship you can directly merge the two tables. If you have not done this before, you can access this is in the edit query window. Once you merge both the tables using serial ID as the key, you can use a formula like below to use as a filter:

Filter Field = IF(Date2[Date]>=Date2[Date1.Start]&&Date2[Date]<=Date2[Date1.End],1,0)

Then you can simply set Filter Field =1 in the filter pane and you should be good to go. Hope this helps.

Here is a tutorial on merge queries. The link is specifically for excel, but it should work in power bi as well:

https://www.powerquery.training/merge-tables/



来源:https://stackoverflow.com/questions/59364497/many-to-many-relationship-by-date-in-powerbi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!