Forum Discussion

cghanta's avatar
cghanta
Helper I
1 year ago
Solved

adding if statement in a Measure DAX

I am using the following DAX in the measure below: UPS %No Maint Bypass = VAR TotalMaintBypass = COUNTROWS(FILTER(ALL('UPS (2)'), 'UPS (2)'[UPS Maintenance Bypass] = "TRUE")) + COUNTROWS(FILTER(ALL('...
  • SolomonovAnton's avatar
    1 year ago

    To incorporate the condition 'UPS (2)'[Redundant UPS] = "TRUE" when referencing [Redundant UPS Maintenance Bypass], you can modify your measure by adding an additional filter inside those specific FILTER expressions.

    Here's the revised measure:

    UPS %No Maint Bypass = 
    VAR TotalMaintBypass = 
        COUNTROWS(
            FILTER(
                ALL('UPS (2)'), 
                'UPS (2)'[UPS Maintenance Bypass] = "TRUE"
            )
        ) +
        COUNTROWS(
            FILTER(
                ALL('UPS (2)'), 
                'UPS (2)'[UPS Maintenance Bypass] = "FALSE"
            )
        ) +
        COUNTROWS(
            FILTER(
                ALL('UPS (2)'), 
                'UPS (2)'[Redundant UPS] = "TRUE" &&
                'UPS (2)'[Redundant UPS Maintenance Bypass] = "TRUE"
            )
        ) +
        COUNTROWS(
            FILTER(
                ALL('UPS (2)'), 
                'UPS (2)'[Redundant UPS] = "TRUE" &&
                'UPS (2)'[Redundant UPS Maintenance Bypass] = "FALSE"
            )
        )
    
    VAR NoMaintBypass = 
        COUNTROWS(
            FILTER(
                ALL('UPS (2)'), 
                'UPS (2)'[UPS Maintenance Bypass] = "FALSE"
            )
        ) +
        COUNTROWS(
            FILTER(
                ALL('UPS (2)'), 
                'UPS (2)'[Redundant UPS] = "TRUE" &&
                'UPS (2)'[Redundant UPS Maintenance Bypass] = "FALSE"
            )
        )
    
    RETURN 
        DIVIDE(NoMaintBypass, TotalMaintBypass, 0) * 100

    This version ensures that the rows involving [Redundant UPS Maintenance Bypass] are only counted if [Redundant UPS] is also "TRUE".

    Try refreshing the visual to confirm the filters apply correctly.

    ✔️ If my message helped solve your issue, please mark it as Resolved!

    👍 If it was helpful, consider giving it a Kudos!

  • DataNinja777's avatar
    1 year ago

    Hi cghanta 

     

    To add an IF condition so that 'Redundant UPS Maintenance Bypass' is only counted when 'Redundant UPS' = "TRUE", you need to include that condition directly inside the FILTER for each of the three places where you're referencing 'Redundant UPS Maintenance Bypass'. Instead of checking the bypass column alone, you also check whether the system is redundant. Here's how your revised measure should look:

    UPS %No Maint Bypass =
    VAR TotalMaintBypass =
        COUNTROWS(
            FILTER(ALL('UPS (2)'), 'UPS (2)'[UPS Maintenance Bypass] IN {"TRUE", "FALSE"})
        ) +
        COUNTROWS(
            FILTER(
                ALL('UPS (2)'),
                'UPS (2)'[Redundant UPS] = "TRUE" &&
                'UPS (2)'[Redundant UPS Maintenance Bypass] IN {"TRUE", "FALSE"}
            )
        )
    
    VAR NoMaintBypass =
        COUNTROWS(
            FILTER(ALL('UPS (2)'), 'UPS (2)'[UPS Maintenance Bypass] = "FALSE")
        ) +
        COUNTROWS(
            FILTER(
                ALL('UPS (2)'),
                'UPS (2)'[Redundant UPS] = "TRUE" &&
                'UPS (2)'[Redundant UPS Maintenance Bypass] = "FALSE"
            )
        )
    
    RETURN
        DIVIDE(NoMaintBypass, TotalMaintBypass, 0) * 100
    

    This version ensures that Redundant UPS Maintenance Bypass rows are only counted if the unit is marked as redundant. The IN {"TRUE", "FALSE"} check simplifies the logic where both values are accepted.

     

    Best regards,