Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
PedroModa
Helper I
Helper I

Subtotal calculation with hierarchy in matrix chart

Guys, I have the following scenario today with this DAX measure:

 

Teste Realizado Condicional =
VAR EscopoTrimestre = ISINSCOPE(dCalendario[Trimestre_Ano])
VAR MaxTrimestre = MAX(dCalendario[Trimestre_Ano])
VAR MinTrimestre = MIN(dCalendario[Trimestre_Ano])
VAR RealizadoMax = CALCULATE([Saldo REALIZADO], dCalendario[Trimestre_Ano] = MaxTrimestre)
VAR RealizadoMin = CALCULATE([Saldo REALIZADO], dCalendario[Trimestre_Ano] = MinTrimestre)
VAR TrimSelecionados = CALCULATE(DISTINCTCOUNT(dCalendario[Trimestre_Ano]), ALLSELECTED(dCalendario[Trimestre_Ano], dCalendario[Índice_Trimestre_Ano]))
VAR Variacao = RealizadoMax - RealizadoMin

RETURN

SWITCH(
TRUE(),
EscopoTrimestre, [Saldo REALIZADO],
NOT EscopoTrimestre && TrimSelecionados = 2, Variacao,
BLANK()
)

This measure is in a matrix chart, which TODAY only has the quarter column. Its business rule is that when the user selects ONLY 2 quarters in the quarter slicer, the variation is displayed in the subtotal; otherwise, it remains blank.

 

Now, I have the following challenge: the business area has asked me for the variation at the year and month level, that is, the hierarchy would be Year > Quarter > Month.

 

I have tried several ways to find a DAX measure that solves my problem, but so far without success. The rule is as follows:

When I am in the YEAR SCOPE and select ONLY 2 years in the year slicer, the variation is displayed in the subtotal; the total (columns) is the actual total. When I go down to the quarter level, the variation should only be displayed in the subtotal when ONLY 2 quarters are selected, even if I select the year slicer, regardless of the quantity, the variation should NOT be calculated; it should appear blank. At the quarter level, the variation will only be calculated when 2 quarters are selected. When the user goes down to the month level, it gets a little more complex:

 

When the user is at the month level, the variation appears in these cases:

 

To compare two months of the same year, compare one month from two different years, or compare more than one month from different years (in which case you have to sum the total of each month of the year and compare it to the sum of the total months of the previous year) and bring up the variation.

I tried to do this, but I couldn't follow it because the variation always appeared in the context of the quarter when there were two years selected, for example. I couldn't identify if it's because it considers the quarter as part of the year and therefore the year context influenced it (by counting the quarter within the year), or if it's because the subtotal doesn't know which context it's in. That's why, even if I went to the quarter hierarchy, it ended up accepting the rule of two selected years from the year hierarchy... anyway, I would really appreciate your help.

1 ACCEPTED SOLUTION
v-nmadadi-msft
Community Support
Community Support

Hi @PedroModa ,

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information. Do not include anything that is unrelated to the issue or question.

Please show the expected outcome based on the sample data you provided.

 Thank you

View solution in original post

3 REPLIES 3
v-nmadadi-msft
Community Support
Community Support

Hi @PedroModa 

May I check if this issue has been resolved? If not, Please feel free to contact us if you have any further questions.


Thank you

v-nmadadi-msft
Community Support
Community Support

Hi @PedroModa 

I wanted to check if you had the opportunity to review the information requested. Please feel free to contact us if you have any further questions.


Thank you.

v-nmadadi-msft
Community Support
Community Support

Hi @PedroModa ,

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information. Do not include anything that is unrelated to the issue or question.

Please show the expected outcome based on the sample data you provided.

 Thank you

Helpful resources

Announcements
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.