Forum Discussion

Maria_Maya's avatar
Maria_Maya
Regular Visitor
1 year ago
Solved

Help with changing specific row values in a matrix

Hello, I'm Maria, and I need help with a DAX formula. I have a measure called Resultado%. I use that measure in a matrix to visualize the result. What do I need? I need to modify the measure only f...
  • DataNinja777's avatar
    1 year ago

    Hi Maria_Maya ,

     

    To apply a different calculation for just those two specific rows, you can modify your measure to check the current row's name and apply the correct logic accordingly.

    You can replace your existing Resultado% measure with the updated DAX formula below. It incorporates your specific requirements while keeping the rest of your logic intact.

    Resultado% =
    VAR CurrentJerarquia1 = SELECTEDVALUE('OrdenPyG'[Nombre Grupo])
    VAR CurrentJerarquia2 = SELECTEDVALUE('Maximo-Balance'[JERARQUIA 2])
    
    -- This is the main logic that will check which level of the hierarchy is active
    RETURN
    IF(
        ISINSCOPE('Maximo-Balance'[JERARQUIA 2]),
        
        // -- Calculations for Level 2 (JERARQUIA 2) --
        VAR CurrentValue = SUM('Maximo-Balance'[Saldo])
    
        // -- Calculate the specific revenues needed for the new logic.
        // -- We use ALLSELECTED to remove the filter from the current row and find the 'Ventas' rows.
        VAR VentasBar =
            CALCULATE(
                SUM('Maximo-Balance'[Saldo]),
                FILTER(
                    ALLSELECTED('Maximo-Balance'),
                    'Maximo-Balance'[JERARQUIA 2] = "Ventas Serv Bar"
                )
            )
    
        VAR VentasRestaurante =
            CALCULATE(
                SUM('Maximo-Balance'[Saldo]),
                FILTER(
                    ALLSELECTED('Maximo-Balance'),
                    'Maximo-Balance'[JERARQUIA 2] = "Ventas Serv Restaurante"
                )
            )
            
        // -- This is your original calculation for the total, used as the default.
        VAR TotalIngresosOperacionales =
            CALCULATE(
                SUM('Maximo-Balance'[Saldo]),
                FILTER(
                    ALL('Maximo-Balance'),
                    'Maximo-Balance'[JERARQUIA 1] = "1 Ingresos Operacionales"
                ),
                KEEPFILTERS(VALUES(Calendario[Date]))
            )
    
        // -- Use SWITCH to apply the correct formula for each row.
        RETURN
            SWITCH(
                TRUE(),
                CurrentJerarquia2 = "Costo Serv Bar", DIVIDE(CurrentValue, ABS(VentasBar)),
                CurrentJerarquia2 = "Costo Serv Restaurante", DIVIDE(CurrentValue, ABS(VentasRestaurante)),
                
                // -- Default calculation for all other rows in JERARQUIA 2
                DIVIDE(CurrentValue, TotalIngresosOperacionales)
            ),
            
        // -- Calculations for Level 1 (JERARQUIA 1) - Your original SWITCH logic --
        IF(
            HASONEVALUE(OrdenPyG[Nombre Grupo]),
            SWITCH(
                CurrentJerarquia1,
                "(+) Ingresos Operacionales", 1,
                "(-) Costos de Materias Primas", [12_Costo de Materia Prima],
                "⩧ Utilidad Bruta por MP", [13_Utilidad Bruta por MP],
                "(-) Costo de Produccion", [14_Costo de Producción],
                "⩧ Utilidad Bruta", [15_Utilidad Bruta],
                "(-) Gastos de Administración", [16_Gastos de Administracion],
                "(-) Gastos de Ventas", [17_Gastos de Ventas],
                "⩧ Utilidad Operacional", [18_Utilidad Operacional],
                "(+) Ingresos no Operacionales", [19_Ingresos No Operacionales],
                "(-) Gastos no Operacionales", [20_Gastos No Operacionales],
                "⩧ Utilidad Neta ", [21_Utilidad Neta]
            )
        )
    )

    This updated formula first checks if your matrix is expanded to the JERARQUIA 2 level using ISINSCOPE. If it is, it calculates the specific revenue values for the bar and restaurant using variables. The key part of these calculations is FILTER(ALLSELECTED('Maximo-Balance'), ...) which allows DAX to find the 'Ventas' (Sales) value that corresponds to the 'Costo' (Cost) row within the same period. A SWITCH(TRUE(), ...) statement then directs the calculation: if the row is "Costo Serv Bar" or "Costo Serv Restaurante", it performs your custom division. For all other rows at this level, it uses your original default calculation. I've also wrapped the revenue variables in the ABS function because your revenue figures are negative; this ensures the resulting cost percentage is a positive value, as is standard. The rest of the formula remains your original logic for the higher levels of your report.

     

    Best regards,