Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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,

  • You could change your calculation like this :

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

     

8 Replies

  • Anonymous's avatar
    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,

    • v-kkf-msft's avatar
      v-kkf-msft
      Community Support

      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.

       

       

      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's avatar
        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:

         

         

        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).

         

         

        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,

  • m3tr01d's avatar
    m3tr01d
    Continued Contributor

    You could change your calculation like this :

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

     

    • Anonymous's avatar
      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's avatar
        m3tr01d
        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's avatar
      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,