Forum Discussion

Diogo_Dalla's avatar
Diogo_Dalla
Frequent Visitor
4 years ago
Solved

Join with Clauses

Hi,

I am looking for a way to merge two tables adding some clauses to the join.

 

For example:

Scrap Table:

 

OrderStepQuantity ScrappedCost
12343137

 

Order table:

Order Step Cost
1000110
1000212
1000315
1000410
1000510

 

The COST column from the Scrap table is the sum of all Cost column from Order table which step column is <= step column from scrap table.

 

Using SQL it would be more or less like below:

 

Select Scrap.order, Scrap.step , Scrap.QuantityScrapped , Sum(Order.Cost)

From Scrap Join Order

ON Scrap.order = Order.order

AND Order.step <= Scrap.Step

  • v-kkf-msft's avatar
    v-kkf-msft
    4 years ago

    Hi Diogo_Dalla ,

     

    Please try the following measure or custom column(PQ).

     

    Measure:

    SumCost = 
    SUMX (
        SUMMARIZE (
            Scrap,
            Scrap[Order],
            "_sum",
                CALCULATE (
                    SUM ( 'Order'[Cost] ),
                    FILTER (
                        'Order',
                        'Order'[Step] <= MAX ( Scrap[Step] )
                            && 'Order'[Order] = MAX ( 'Scrap'[Order] )
                    )
                )
        ),
        [_sum]
    )

     

    PQ Column:

    JoinOrderCost =
    let 
       myfunction = (CurrentOrder, CurrentStep) => 
         let 
           SelectRows = Table.SelectRows(Order, each [Order] = CurrentOrder and [Step] <= CurrentStep) 
         in 
           SelectRows,
       Data = List.Sum( myfunction([Order],[Step])[Cost])
    in 
       Data

     

    If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
    Best Regards,
    Winniz
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

7 Replies

  • v-kkf-msft's avatar
    v-kkf-msft
    Community Support

    Hi Diogo_Dalla ,

     

    Please try the following measure:

     

    Cost = 
    CALCULATE (
        SUM ( 'Order'[Cost] ),
        FILTER ( 'Order', 'Order'[Step] <= MAX ( Scrap[Step] ) )
    )

     

    If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
    Best Regards,
    Winniz
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

    • Diogo_Dalla's avatar
      Diogo_Dalla
      Frequent Visitor

      Hi,

      THank you for your reply.

       

      I checked the model that you sent, but I didn't find where you have added the DAX Code.

       

      Have you merged the two tables?

       

      Thank you,

      Diogo Dalla 

      • v-kkf-msft's avatar
        v-kkf-msft
        Community Support

        Hi Diogo_Dalla ,

         

        I did not merge the two tables, I simply created a measure as shown below.

         

         

        Or try to create a custom column in Power Query to get the data from the Order table and extend it.

         

        = Table.SelectRows(Order, each ([Step] <= #"Changed Type"{0}[Step]))

         

        If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
        Best Regards,
        Winniz
        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.