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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Measure needing different context

Dear community, 

 

I would appreciate some help in the following issue.

I have aggregated data about some stores. I have several more columns describing the stores but, for the sake of the example let's assume:

 

YearStoreCountryAmount($)Performance
2011AUS10030
2011AMX20020
2011BUS15025
2011BMX13010
2011BTZ19013
2012AUS...305

 

I compute the following measure:

 

measureA :=

DIVIDE (

  SUM ( Table[Performance] ),

  CALCULATE (

    SUM ( Table[Amount($)] ),

    ALL ( Table[Store] )

  )

)

Basically it computes a kind of average for each store to compare it with the others considering the filter context of the chart (I want a matrix in particular).

 

This will work AS LONG AS I include the Store in the matrix chart I am building. If not, I would get a value that does not make any sense. 

 

So, for example, if I create a matrix chart with Columns Year and Store I get:

 

YearStoremeasureA
2011A0.06
2011B0.04
2012A0.17

 

Which is fine. But now I have 2 questions (related to each other):

 

First question:

I want to create a measureB that counts how many rows satisfy measureA > 0.05 for the current context EXCEPT for the store. In the example above, I would like the table:

YearStoremeasureAmeasureB
2011A0.061
2011B0.041
2012A0.171

 

(In year 2011, only store A satisfies measureA > 0.05. Then I want a "1" in all rows that have Year = 2011. Same reasoning for 2012).

 

Second question:

I want to create a matrix chart taking for example Year (NOT Store) and want to count how many Stores satisfy for example measureA > 0.5

So I want the following table:

 

YearmeasureC
20111
20121

 

Any ideas?

 

Sincerely,

1 ACCEPTED SOLUTION
m3tr01d
Continued Contributor
Continued Contributor

You could change your calculation like this :

CALCULATE(
    DISTINCTCOUNT('Table'[Store]),
    FILTER(
        ALL( 'Table'[Store] ),
        [measureA] > 0.05
    )
)

 

View solution in original post

8 REPLIES 8
m3tr01d
Continued Contributor
Continued Contributor

You could change your calculation like this :

CALCULATE(
    DISTINCTCOUNT('Table'[Store]),
    FILTER(
        ALL( 'Table'[Store] ),
        [measureA] > 0.05
    )
)

 

Anonymous
Not applicable

Hello @m3tr01d , 

 

In fact, the measure above solve both questions, the result does not change if I add or delete the Store column to the table.

 

I will open another post because I do not want to add noise, but I really do not understand what is happening here behind the scenes and why does this work. If anybody would be so kind as to stop by I would appreciate it. 

 

Best regards,

Anonymous
Not applicable

Hello @m3tr01d ,

 

Thank you very much for your response.

This solved me the problem of measureB but still cannot figure out how to compute measureC, which unfortunately is my final table.

 

Any ideas would be much appreciated.

 

Best regards,

m3tr01d
Continued Contributor
Continued Contributor

Hmm,
maybe try 

COUNTROWS(
    FILTER(
        ALL( 'Table'[Store] ),
        [measureA] > 0.05
    )
)

 
At this point, I would need to do some more tests with data.

Anonymous
Not applicable

Hello @Greg_Deckler 

 

Thanks for your reply, I have spent a lot of time and I am sorry but I am not figuring this out. I cannot see how to combine HASONEFILTER() and maybe SUMMARIZE() to get both results I want. 

 

In fact, my attempts were working in the following direction:

For the first question:

measureB = 
CALCULATE(
    DISTINCTCOUNT('Table'[Store]),
    ALL('Table'[Store]),
    [measureA] < 0.05
)

But I get the error:

A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed 

 

For the second question no idea...

 

Regards,

Hi @Anonymous ,

 

For the first question, try the following formula:

 

MeasureB = IF( MAXX( ALLEXCEPT( 'Table', 'Table'[Year] ), [MeasureA] ) > 0.05, 1 )

 

And for the second question, try the formula provided by @m3tr01d.

 

vkkfmsft_0-1627285877418.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.


 

Anonymous
Not applicable

Dear @v-kkf-msft ,

 

Thank you for your answer. I am afraid the solution you propose appears to be correct but it is not. I have the impression the result is correct "by chance", and it is something I had observed before:

 

If I change slightly your measure to:

MeasureB-2 = MAXX( ALLEXCEPT( 'Table', 'Table'[Year] ), [MeasureA] )

just to check what Power BI is computing in this MAXX, I do not get what I expect:

 

capture1.PNG

 

The result for the last column should be (0.06, 0.06, 0.17). After doing some tests, I guess that what Power BI is computing under the hood is [measureA] NOT in the current context, but for each row of the original Table, and then computing the maximum of all the rows (in this case, the maximum IS INDEED computed given the current context).

 

Capture2.PNG

 

After reading a bit about it, I came to the conclusion that iterator functions like MAXX() "get rid" of all the context and compute all the expressions row by row.

Of course when you compute [measureA] not in the current context but just row by row, you get just a simple division like I show in the Excel capture in column Perf/Amount. But this is wrongly computed.

 

As a matter of fact by the way, I believe it should be:

ALL('Table'[Store])

instead of 

ALLEXCEPT( 'Table', 'Table'[Year] )

 

Don't you think so?

 

@m3tr01d formula solved the problem for question 1:

 

CALCULATE(
    DISTINCTCOUNT('Table'[Store]),
    FILTER(
        ALL( 'Table'[Store] ),
        [measureA] > 0.05
    )
)

 

But question 2 remains unsolved (which unfortunately is the final table in my report...).

 

Best wishes,

Greg_Deckler
Super User
Super User

@Anonymous I am thinking that this is essentially a measure totals problem. This looks like a measure totals problem. Very common. See my post about it here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

Also, this Quick Measure, Measure Totals, The Final Word should get you what you need:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.