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
Setname
Frequent Visitor

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
1 ACCEPTED SOLUTION

Hi @Setname ,

 

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:

7.PNG

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.

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

13 REPLIES 13
venal
Memorable Member
Memorable Member

@Setname 

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?

Setname
Frequent Visitor

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
v-jayw-msft
Community Support
Community Support

Hi @Setname ,

 

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

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

Hi @v-jayw-msft ,

 

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!

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:

1.PNG

2.PNG

3.PNG

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:

4.PNG

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.

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

Thanks @v-jayw-msft  it's a good idea.

 

However, Merge method only solve half of 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. 

 

After merging, the column Vessel and Destination port, Sailing date should be unique, and I just want to look up the earliest Job perform date after the Sailing date from Job table. Do you have any idea how to resolve this? Thanks.

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

Hi @Setname ,

That's exactly what I said about the duplicate data above.

If you just want to look up the earliest Job perform date, we need to do some Group By operations on Job table before merge these two tables:

5.PNG

And modify measure 1 as below:

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

Result would be shown as below:

6.PNG

 

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.

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

Thanks @v-jayw-msft  can you send me the updated pbix file?

Hi @Setname ,

 

Here's the Pbix, hopefully works for you.

BTW, if you just want the earliest date in Voyage table, just do the same Group By operation on it.

 

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.

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

Hi @v-jayw-msft ,

 

I understand your objective by Grouping before the merge but we can't simply pick the earliest date, the logic is wrong.

 

Firstly of all, like I said earlier, we have to use voyage table as the base, to map against job table, so basically once Vessel name and Port is matched, if there are mutiple job performed date, last step is to pick the earliest date right after the sailling date. If you group the job table by selecting earliest date before the mapping, you accidently move forward the last step into the first and this is not the right logic.

 

Do you have other method to achieve this?

 

 

 

 

 

 

 

 

Hi @Setname ,

 

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:

7.PNG

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.

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

Thanks Jay, exactly what I need!

Setname
Frequent Visitor

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

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.

Top Solution Authors