Forum Discussion

EmJa's avatar
EmJa
Frequent Visitor
4 years ago
Solved

Lookup related record in same table

Product table

 

Sku

IDPrice

Prod1_a

Prod15
Prod1_bProd16
Prod1_cProd17
Prod2_aProd27
Prod2_bProd26

 

What dax measure can be used to return the price of the MAX(Sku) for each ID?

i.e All Prod1 would show 7 and all Prod2 would show 6.

 

Thank you

  • EmJa  you can use a measure like this

    Measure =
    VAR _id =
        MAX ( 'Table 1'[ID] )
    VAR _sku =
        CALCULATE (
            MAX ( 'Table 1'[Sku] ),
            FILTER (
                ALL ( 'Table 1' ),
                'Table 1'[ID] = _id
                    && RIGHT ( 'Table 1'[Sku], 1 )
                        = CALCULATE (
                            RIGHT ( MAX ( 'Table 1'[Sku] ), 1 ),
                            ALLEXCEPT ( 'Table 1', 'Table 1'[ID] )
                        )
            )
        )
    RETURN
        CALCULATE (
            MAX ( 'Table 1'[Price] ),
            FILTER ( ALL ( 'Table 1' ), 'Table 1'[ID] = _id && 'Table 1'[Sku] = _sku )
        )
    

     

     

     

2 Replies

  • smpa01's avatar
    smpa01
    Icon for Community Champion rankCommunity Champion

    EmJa  you can use a measure like this

    Measure =
    VAR _id =
        MAX ( 'Table 1'[ID] )
    VAR _sku =
        CALCULATE (
            MAX ( 'Table 1'[Sku] ),
            FILTER (
                ALL ( 'Table 1' ),
                'Table 1'[ID] = _id
                    && RIGHT ( 'Table 1'[Sku], 1 )
                        = CALCULATE (
                            RIGHT ( MAX ( 'Table 1'[Sku] ), 1 ),
                            ALLEXCEPT ( 'Table 1', 'Table 1'[ID] )
                        )
            )
        )
    RETURN
        CALCULATE (
            MAX ( 'Table 1'[Price] ),
            FILTER ( ALL ( 'Table 1' ), 'Table 1'[ID] = _id && 'Table 1'[Sku] = _sku )
        )
    

     

     

     

    • EmJa's avatar
      EmJa
      Frequent Visitor

      Thank you smpa01 , not just for the solution but for th concept. 

      (That was a really quick response!)