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
N-PivotMeasure
Regular Visitor

DAX calculated column to find related ID but only once

I have a table for Opportunities and a table for Payments (example data below).  They both share a ProductID but no direct relationship currently exists in the schema.  I'd like to create a calculated column in the Opportunities table with PaymentID that most closely matches the Opportunity's ProductID and Date.  The Payment just after the Opportunity is preferred but matching an earlier Payment if not.  However, there are likely multiple more Opportunities than payments and I'd like to not duplicate PaymentIDs in the Opportunity table.  In other words, I'd like to create this relationship between the two where one doesn't really exist but only one-to-one.

 

Payments Table

ProductIDPaymentIDDate
RedX1/3/2022
RedZ1/12/2022
RedY1/8/2022
GreenX12/1/2022

 

Opportunities Table

ProductIDOppID Date Correct Result
RedA1/1/2022 
RedB1/2/2022X
RedC1/5/2022 
RedD1/6/2022Y
RedE1/9/2022Z
GreenA12/4/2022X1
GreenB12/5/2022 

 

I've got the following DAX in a column but am getting duplicated IDs as expected.  Is there a way to change this formula, or one like it, to get only unique results?

 

 

=
IF (
    ISBLANK (
        CALCULATE (
            MIN ( Payments[PaymentID] ),
            FILTER (
                Payments,
                [ProductID] = Opportunities[ProductID]
                    && [Date] >= Opportunities[Date]
            )
        )
    ),
    CALCULATE (
        MAX ( Payments[PaymentID] ),
        FILTER (
            Payments,
            [ProductID] = Opportunities[ProductID]
                && [Date] < Opportunities[Date]
        )
    ),
    CALCULATE (
        MIN ( Payments[PaymentID] ),
        FILTER (
            Payments,
            [ProductID] = Opportunities[ProductID]
                && [Date] >= Opportunities[Date]
        )
    )
)

 

 

 

My Results (Incorrect)

ProductIDOppIDDateCorrect ResultMy DAX Result
RedA1/1/2022  X
RedB1/2/2022 XX
RedC1/5/2022  Y
RedD1/6/2022 YY
RedE1/9/2022 ZZ
GreenA12/4/2022 X1X1
GreenB12/5/2022  X1

 

Is DAX the right solution for what I'm trying to achieve?

1 ACCEPTED SOLUTION
v-yinliw-msft
Community Support
Community Support

Hi @N-PivotMeasure ,

 

You can try the following methods.

Columns:

 

Payment date = 
Var N1=CALCULATE (
            MIN ( Payments[Date]),
            FILTER (
                Payments,
                [ProductID] = Opportunities[ProductID]
                    && [Date]>=Opportunities[Date]
            )
        )

Var N2=CALCULATE (
        MAX ( Payments[Date] ),
        FILTER (
            Payments,
            [ProductID] = Opportunities[ProductID]
                && [Date] <Opportunities[Date]
        )
    )
Return
IF(ISBLANK(N1),N2,N1)
Days = ABS(DATEDIFF([Date],[Payment date],DAY))
Result = 
VAR minday =
    CALCULATE (
        MIN ( Opportunities[Days] ),
        FILTER (
            Opportunities,
            [Payment date] = EARLIER ( Opportunities[Payment date] )
        )
    )
RETURN
    CALCULATE (
        MAX ( Payments[PaymentID] ),
        FILTER (
            Payments,
            [Date] = EARLIER ( Opportunities[Payment date] )
                && [Days] = minday
        )
    )

 

vyinliwmsft_0-1662544272637.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Yinliw

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

 

View solution in original post

3 REPLIES 3
v-yinliw-msft
Community Support
Community Support

Hi @N-PivotMeasure ,

 

You can try the following methods.

Columns:

 

Payment date = 
Var N1=CALCULATE (
            MIN ( Payments[Date]),
            FILTER (
                Payments,
                [ProductID] = Opportunities[ProductID]
                    && [Date]>=Opportunities[Date]
            )
        )

Var N2=CALCULATE (
        MAX ( Payments[Date] ),
        FILTER (
            Payments,
            [ProductID] = Opportunities[ProductID]
                && [Date] <Opportunities[Date]
        )
    )
Return
IF(ISBLANK(N1),N2,N1)
Days = ABS(DATEDIFF([Date],[Payment date],DAY))
Result = 
VAR minday =
    CALCULATE (
        MIN ( Opportunities[Days] ),
        FILTER (
            Opportunities,
            [Payment date] = EARLIER ( Opportunities[Payment date] )
        )
    )
RETURN
    CALCULATE (
        MAX ( Payments[PaymentID] ),
        FILTER (
            Payments,
            [Date] = EARLIER ( Opportunities[Payment date] )
                && [Days] = minday
        )
    )

 

vyinliwmsft_0-1662544272637.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Yinliw

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

 

tamerj1
Super User
Super User

Hi @N-PivotMeasure 
Actually the filtering criteria is not well bounded. For example for PaymentID "Y", the date 1st June is not the nearest date to 1st August unless we consider only the dates before 1st August. However, if we do that then there would be no match for PaymentID "X1" - Payment Date "2nd January" as all the related dates in the Opportunity table are after 2nd January. So need to double check your requirement. 
For the provided sample data, following is my proposed solution. Please refer to attached sample file.

1.png

Result = 
VAR CurrentDate = Opportunities[ Date ]
VAR T1 =
    FILTER ( 
        Payments,
        VAR ProdID = Payments[ProductID]
        VAR PayDate = Payments[Date]
        VAR T2 = 
            FILTER ( 
                Opportunities, 
                Opportunities[ProductID]= ProdID 
            )
        VAR T3 = 
            FILTER ( 
                T2, 
                Opportunities[ Date ] <= PayDate
            )
        VAR T4 = ADDCOLUMNS ( T2, "@Difference", ABS ( [ Date ] - PayDate ) )
        VAR T5 = ADDCOLUMNS ( T3, "@Difference", ABS ( [ Date ] - PayDate ) )
        VAR T6 = TOPN ( 1, T4, [@Difference], ASC )
        VAR T7 = TOPN ( 1, T5, [@Difference], ASC )
        VAR NearestDate = COALESCE ( MAXX ( T7, [ Date ] ), MAXX ( T6, [ Date ] ) )
        RETURN
            CurrentDate = NearestDate 
    )
RETURN
    MAXX ( T1, [PaymentID] )
amitchandak
Super User
Super User

@N-PivotMeasure , Try like

new column in Opportunities 

Col 1=

var _max = maxx(filter(Payment, [ProductID] = Opportunities[ProductID] && Payment[Date] >= Opportunities[Date]) , Payment[Date])

return 

maxx(filter(Payment, [ProductID] = Opportunities[ProductID] && Payment[Date] =_max) , Payment[Date])

 

 

Final column =

var _cnt = countx(filter(Opportunities, Opportunities[ProductID] = earlier(Opportunities[ProductID]) && [Col 1] = earlier([Col 1]) && [Date]  < earlier([Date]) ) , [Col 1])

return

if(not(isblank(_cnt), Blank(), [Col1])

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