Forum Discussion

tkavitha911's avatar
tkavitha911
Icon for Helper III rankHelper III
1 year ago
Solved

urgent help needed

Hi Team I need your support in creating a DAX formula to calculate Demurrage Cost based on two tables, with the logic applied market-wise. Here's the requirement: For each record, if Day_at_port ≤...
  • Poojara_D12's avatar
    1 year ago

    Hi tkavitha911 

    Demurrage_Cost = 
    SWITCH(
        TRUE(),
        'Table'[Day_at_port] <= 'Table'[FREE days], 0,
    
        'Table'[Markets] = "Australia", 
            ('Table'[Day_at_port] - 7) * 50 * 'Table'[Count of Container Number],
    
        'Table'[Markets] = "Korea", 
            ('Table'[Day_at_port] - 7) * 24 * 'Table'[Count of Container Number],
    
        'Table'[Markets] = "China",
            VAR ExtraDays = 'Table'[Day_at_port] - 14
            VAR Charge =
                SWITCH(
                    TRUE(),
                    ExtraDays <= 4, 0,
                    ExtraDays <= 12, (ExtraDays - 4) * 96,
                    (8 * 96) + (ExtraDays - 12) * 125
                )
            RETURN Charge * 'Table'[Count of Container Number],
    
        'Table'[Markets] = "Shenzhen",
            VAR ExtraDays = 'Table'[Day_at_port] - 14
            VAR Charge =
                SWITCH(
                    TRUE(),
                    ExtraDays <= 4, 0,
                    ExtraDays <= 12, (ExtraDays - 4) * 96,
                    (8 * 96) + (ExtraDays - 12) * 125
                )
            RETURN Charge * 'Table'[Count of Container Number],
    
        BLANK()
    )
    

    The SWITCH(TRUE(), ...) structure allows for flexible multi-condition evaluation.

     

    For each market, we calculate ExtraDays by subtracting the free threshold from Day_at_port.

     

    Based on tiered rate structures, we apply the appropriate cost calculations and multiply by container count.

     

    This formula assumes you're applying it in a table with fields like Markets, Day_at_port, FREE days, and Count of Container Number. Adjust field names if yours differ. You can convert this into a measure with additional aggregation (e.g., SUMX) if needed at the visual level.