Percent of Total Windowed in Power BI DAX

自古美人都是妖i 提交于 2020-04-30 02:35:21

问题


I have these two tables:

A data table...

ImaginaryData = 
DATATABLE (
    "Fruit", STRING,
    "Colour", STRING, 
    "Amount", INTEGER, 
    { 
        { "Apple", "Red", 10 }, 
        { "Apple", "Green", 5 },
        { "Pear", "Pink", 100 },
        { "Pear", "Blue", 65 },
        { "Orange", "Black", 12 },
        { "Orange", "White", 8 }
    } )

A lookup table...

ImaginaryLookup = 
DATATABLE (
    "Fruit", STRING, 
    { 
        { "Apple" }, 
        { "Pear" },
        { "Orange" }
    } )

Then I created this simple 1-to-many relationship:

Now I am trying to create a Percent of Total measure to add to a table - but I want the percent of total to be based on the total of the fruits that are selected in a slicer of ImaginaryLookup:

This is what I have tried:

 %Total = 
DIVIDE(
    sum( ImaginaryData[Amount] ),
    CALCULATE(
        sum( ImaginaryData[Amount] ),
        ALL(ImaginaryData)
    )
)

If all fruits are selected in the slicer then it works fine as I'd expect:

But if I select say "Orange" then the results are not what I want, as I'd like the 12 and the 8 to be a percentage of 20:

This also does not give me what I want:

%Total = 
DIVIDE(
    sum( ImaginaryData[Amount] ),
    CALCULATE(
        sum( ImaginaryData[Amount] ),
        ALLEXCEPT(ImaginaryData, ImaginaryData[Fruit])
    )
)

Because now if I select say Pears and Oranges then it is giving me the percent as a percent of each fruits total, rather than the percent of 185:


note to Alexis

If I try this:

%Total = 
DIVIDE(
    sum( ImaginaryData[Amount] ),
    CALCULATE(
        sum( ImaginaryData[Amount] ),
        ALLSELECTED( ImaginaryData[Fruit])
    )
)

I get this:


回答1:


The ALL function removes all filter context. Try it with ALLSELECTED instead. That will preserve your slicer selection while removing the table visual's filter context.

If you use this (note I didn't specify a column):

%Total = 
    DIVIDE(
        SUM( ImaginaryData[Amount] ),
        CALCULATE(
            SUM( ImaginaryData[Amount] ),
            ALLSELECTED( ImaginaryData )
        )
    )

Then you should get this result:

The reason it doesn't work if you do ALLSELECTED(ImaginaryData[Fruit]) is that the Colour filter context still exists, so you don't pick up the other fruits because those are all different colors than the row you are evaluating on.




回答2:


I used following measure and it works as expected:

%Total = 
CALCULATE(DIVIDE(sum(ImaginaryData[Amount]),
CALCULATE(SUM(ImaginaryData[Amount]),ALLSELECTED(ImaginaryData))))


来源:https://stackoverflow.com/questions/52393069/percent-of-total-windowed-in-power-bi-dax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!