Forum Discussion
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 for two very specific rows.
When the matrix shows in JERARQUIA 2:
- "Costo Serv Bar" I need Resultado% to be calculated like this: Costo Serv Bar / Ventas Serv Bar (in the image: 97,775 / 292,836)
- "Costo Serv Restaurante" I need Resultado% to be calculated like this: Costo Serv Restaurante / Ventas Serv Restaurante (in the image: 347,691 / 519,232)
Here is my Measure:
I'll send you an image and highlight in yellow what I need to modify.
Thanks in advance!
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,
5 Replies
- DataNinja777Super User
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,
- AnonymousNot applicable
Hi Maria_Maya,
Thanks for reaching out to the Microsoft fabric community forum.
The custom logic you need for "Costo Serv Bar" and "Costo Serv Restaurante" can definitely be handled within your DAX measure. Kindly go throufh the response provided by DataNinja777, where he uses ISINSCOPE to check when you're at the JERARQUIA 2 level and then applies a SWITCH(TRUE(), ...) block to override the calculation only for those two rows.
I would also take a moment to thank DataNinja777, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Best Regards,
Hammad.- AnonymousNot applicable
Hi Maria_Maya,
As we haven’t heard back from you, so just following up to our previous message. I'd like to confirm if you've successfully resolved this issue or if you need further help.
If yes, you are welcome to share your workaround so that other users can benefit as well. And if you're still looking for guidance, feel free to give us an update, we’re here for you.
Best Regards,
Hammad.
- AnonymousNot applicable
Hi Maria_Maya,
Hope everything’s going smoothly on your end. As we haven’t heard back from you, so I wanted to check if the issue got sorted.
Still stuck? No worries just drop us a message and we can jump back in on the issue.Best Regards,
Hammad.