Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Match two tables with multiple criteria

Hi,

 

I'm facing a problem to map values between two tables.

 

Basically there are two tables, one is "Voyage" and the other one is "Job", and my objective is to see how many jobs we didn't manage to get per port.

 

For example, in the "Voyage" table, Vessel "A", Destination Port "ASD" and sailling date is on Jan 6th 2019, and in the "Job" table, it needs to look up the same Vessel which is "A", then look up in Loading Port "ASD", and if the two condition matched, the third criteria is the Job Perform date in "Job" table must not be more than two months (eg 60 days) than the "Sailling date" in 'Voyage" table.

 

If any of the 3 criteria is not matched, it counts as "one" opportunity.

 

In the end, I want to have an analysis on how many opportunities per Destination Port in the "Voyage" Table.

 

Can anyone please help me with this? I have listed below the two tables for your reference.

 

Thanks!

 

Voyage Table:

VesselDestination PortSailing Date (mm/dd/yyyy)
AASD1/6/2019
BFSD3/5/2019
CKLD5/6/2019
DDFR3/4/2019
AACF2/5/2019
BASD4/5/2019
CFSD3/17/2019
DKLD3/29/2019
EDFR5/1/2019
FACF3/9/2019
BDFR4/12/2019
CACF5/4/2019
DASD3/21/2019
EFSD3/23/2019
FKLD3/14/2019
CFSD1/9/2019
DKLD2/16/2019
ADFR2/9/2019
BACF5/3/2019
CDFR4/14/2019
DACF4/4/2019
EASD4/21/2019
FFSD3/4/2019
BKLD2/22/2019
CFSD4/4/2019
FKLD2/28/2019
BDFR3/6/2019
CACF4/13/2019
DDFR4/3/2019
EACF4/20/2019
FASD3/14/2019
CFSD2/26/2019
DKLD2/18/2019

 

 

Job Table:

VesselLoading PortJob perform date (mm/dd/yyyy)
AASD2/3/2019
BFSD4/6/2019
CKLD8/4/2019
DDFR7/4/2019
EDFR6/1/2019
FACF5/9/2019
BACF6/9/2019
CFSD7/1/2019
DASD4/1/2019
EFSD5/4/2019
FKLD3/23/2019
CFSD3/4/2019
DKLD3/1/2019
AACF3/4/2019
BFSD4/4/2019
CASD8/4/2019
DFSD7/4/2019
EKLD6/1/2019
  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi Anonymous ,

     

    Sorry for the delay.

    Please modify the measures as below:

    Measure =
    VAR a =
        CALCULATE (
            MIN ( Merge1[Job perform date] ),
            FILTER (
                ALL ( Merge1 ),
                Merge1[Vessel] = MAX ( Merge1[Vessel] )
                    && Merge1[Destination Port] = MAX ( Merge1[Destination Port] )
            )
        )
    VAR b =
        IF (
            ISBLANK ( MAX ( Merge1[Job perform date] ) ),
            BLANK (),
            IF (
                MAX ( Merge1[Sailing Date] ) > MAX ( Merge1[Job perform date] ),
                "NA",
                IF (
                    MAX ( Merge1[Job perform date] ) <> a
                        && MAX ( Merge1[Sailing Date] ) < a,
                    "NA",
                    a
                )
            )
        )
    RETURN
        IF (
            ISBLANK ( b ),
            1,
            IF (
                b = "NA",
                0,
                IF (
                    DATEDIFF (
                        MAX ( Merge1[Sailing Date] ),
                        MAX ( Merge1[Job perform date] ),
                        MONTH
                    ) > 2,
                    1,
                    0
                )
            )
        )
    Measure 2 = SUMX(Merge1,[Measure])

    Result would be shown as below:

    BTW, Pbix as attached, hopefully works for you.

     

    Best Regards,

    Jay

    Community Support Team _ Jay Wang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

13 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi, is there anyone can help with this pls? Thanks.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    I'm working on your data and I want to know how we deal with the duplicate data in Voyage table and Job table?

    For Example:

    Voyage Table:

    VesselDestination PortSailing Date (mm/dd/yyyy)
    CFSD3/17/2019
    CFSD1/9/2019
    CFSD4/4/2019

    Job Table:

    VesselLoading PortJob perform date (mm/dd/yyyy)
    CFSD7/1/2019
    CFSD3/4/2019

     

    Best Regards,

    Jay

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

       

      Thanks for helping.

       

      It's not really duplicated rows because the Sailing date in Voyage table and Job performed date in Job table is different.

      For example, in the Voyage table, Vessel C could depart to Destination Port FSD a few times across the year.

      Similarly, we could do the loading job for Vessel C a few times in a year.

       

      I understand that there could be a problem like below, it's just an illustration and not in my file:

       

      Voyage Table:

      VesselDestination PortSailing Date (mm/dd/yyyy)
      CFSD3/17/2019
      CFSD2/9/2019
      CFSD5/1/2019

       

      Job Table:

      VesselLoading PortJob perform date (mm/dd/yyyy)
      CFSD4/1/2019

       

      In the example above, based ont he criteria, the Job performed on April 1st 2019 which mapped both Sailing date on March 17th and Feburary 9th 2019 (less than 60 days). In such a case, they are not considered an Opportunity. However, the Sailing date is May 1st 2019 but there is no job performed within 60 days on the same vessel same port, so this is considered as an Opportunity.

      Result (by creating a new column):

      VesselDestination PortSailing Date (mm/dd/yyyy)Opportunity
      CFSD3/17/2019No
      CFSD2/9/2019No
      CFSD5/1/2019Yes

       

      Thanks again!

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi @Setname ,

         

        In you scenario, we can use Merge Queries feature in Query Editor, please check following steps as below:

        1. Merge these two tables as a new table and expand table:

        2. Click Close&Apply and create measures:

            Measure = IF(ISBLANK(MAX(Merge1[Job.Job perform date (mm/dd/yyyy)])),1,IF(DATEDIFF(MAX(Merge1[Sailing Date (mm/dd/yyyy)]),MAX(Merge1[Job.Job perform date (mm/dd/yyyy)]),MONTH)>2,1,0))

            Measure 2 = SUMX(Merge1,[Measure])

        3. Result would be shown as below:

        BTW, Pbix as attached, hopefully works for you.

         

        Best Regards,

        Jay

        Community Support Team _ Jay Wang

        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • venal's avatar
    venal
    Icon for Memorable Member rankMemorable Member

    Anonymous 

    I tried to re-pro the issue from my end and uploaded the file.

    3rd creteria, i am not clear. so tried to do it upto days wise data.

    If you have any concerns, please let us know.

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more
    If this post was helpful may I ask you to mark it as solution and click on thumb symbol?

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks venal , it's a good idea but the base we should use is Voyage table not Job table to do the merge.

       

      However, Merge method doesnt really solve the issue because it will create duplicate rows after merging.

       

      For example as below, it will create duplicate two rows (Vessel B, Port FSD and same Sailing Date), however my objective is to only showing the earliest Job perform date which is April 4th 2019, not together with April 6th 2019. Do you have any idea how to resolve this? Thansks.

      VesselDestination PortSailing Date (mm/dd/yyyy)Job.VesselJob.Loading PortJob.Job perform date (mm/dd/yyyy)
      BFSD3/5/2019BFSD4/6/2019
      BFSD3/5/2019BFSD4/4/2019