Forum Discussion

kcantor's avatar
kcantor
Icon for Community Champion rankCommunity Champion
10 years ago
Solved

If has a value use A, else sum B -- DAX Question

I am working on a calculation within a single table for shipping charges. For each order number there is a tracking number field, and a couple of associated cost fields: Shipping Charge and Freight Quote. I need to create a calcuation (column or measure) that will return a value based upon the contents of those cells. The calculations would be (in plain English) If a freight quote exists, divide the freight quote by the distinct number of tracking numbers and apply that cost across all tracking numbers, if a freight quote does not exist, use the shipping charge that exists per package. I want to display the result by tracking number, not by order number.

Any help will be greatly appreciated.

Sample data:

Order NumberTracking NumberShipping ChargeFreight Quote Package numberDesired Result
121254HY6 $                     7.60 $                  6.35P16 $       6.35
161254HY3 $                     5.30 $                      -  P18 $       5.30
161254HY5 $                     4.60 $                      -  P22 $       4.60
181254HY2 $                     8.20 $                      -  P28 $       8.20
201254HY12 $                   12.00 $               19.65P29 $       6.55
201254HY7 $                     5.30 $               19.65P30 $       6.55
201254HY9 $                     4.60 $               19.65P41 $       6.55
251254HY1 $                     8.90 $                  6.35P15 $       6.35
281254HY10 $                     7.80 $                  5.60P52 $       5.60
291254HY8 $                     7.80 $                      -  P63 $       7.80
  • kcantor Try this...

    Calculated Column =
    IF (
        ISBLANK ( 'Table'[Freight Quote] ),
        'Table'[Shipping Charge],
        DIVIDE (
            'Table'[Freight Quote],
            CALCULATE (
                COUNTROWS ( 'Table' ),
                ALLEXCEPT ( 'Table', 'Table'[Order Number] )
            ),
            0
        )
    )

    Seems to work the way you want to show by Tracking Number (table) and also by Order and Tracking Number (matrix)

     

2 Replies

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

    kcantor Try this...

    Calculated Column =
    IF (
        ISBLANK ( 'Table'[Freight Quote] ),
        'Table'[Shipping Charge],
        DIVIDE (
            'Table'[Freight Quote],
            CALCULATE (
                COUNTROWS ( 'Table' ),
                ALLEXCEPT ( 'Table', 'Table'[Order Number] )
            ),
            0
        )
    )

    Seems to work the way you want to show by Tracking Number (table) and also by Order and Tracking Number (matrix)

     

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

      Beautiful. Thank you for helping me out on that. I did have to alter the formula to account for zero instead of blank but it worked perfectly.