CALCULATE with OR condition in two tables

非 Y 不嫁゛ 提交于 2019-12-22 11:29:32

问题


In order to Sum the sales amount of blue products OR products that belong to category shoes, I'm using the following DAX expression:

CALCULATE(

SUM(Table[SalesAmount]),

FILTER(

Table,

Table[Color] = "Blue" ||

Table[Category] = "Shoes")

)

However, this doesn't work with two different tables (Colors and Categories), like:

CALCULATE(

SUM(Table[SalesAmount]),

FILTER(

Table,

Colors[Color] = "Blue" ||

Categories[Category] = "Shoes")

)

Can anyone help?

Thanks!


回答1:


Searching the web led me to this forum topic. Borrowing from OwenAuger's post, I propose the following formula:

CALCULATE(SUM(Table[SalesAmount]),
          FILTER(SUMMARIZE(Table, Colors[Color], Categories[Category]),
                 Colors[Color] = "Blue" ||
                 Categories[Category] = "Shoes"))

We get around the single table restriction by using SUMMARIZE to create a single table that has all of the pieces we need.




回答2:


This might not be the ideal answer, but one way to do this is to use the inclusion-exclusion principle:

CALCULATE(SUM(Table[SalesAmount]), Colors[Color] = "Blue") + 
CALCULATE(SUM(Table[SalesAmount]), Categories[Category] = "Shoes") - 
CALCULATE(SUM(Table[SalesAmount]), Colors[Color] = "Blue", Categories[Category] = "Shoes") 


来源:https://stackoverflow.com/questions/48430572/calculate-with-or-condition-in-two-tables

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