Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

powerbi - Using CALCULATE to inject filter context to a calculated column - does it consist related table columns?

Calculated column does not have any filter context.

To inject a filter context we can use the CALCULATE function.

Assume I have a sales, location and date table. Sales is fact. The location and date are dimensions connected to the fact table.

Suppose I create a calculated column in the sales table with CALCULATE(SUM...)), does the injected filter context consist only rows from sales table or does the context also consist all related table columns as well?

A use case for this is for example suppose location table is linked to sales table via locationid and has state, country, then we could use ALLEXCEPT to keep only Country filter so that for each row, the SUM in CALCULATE is aggregated by Country rather than all fields in the row.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"Inject" isn't quite the right word. Using CALCULATE within a calculated column performs a context transition and then modifies that filter context using whatever arguments (if any) you supply.

You can perform the aggregate you suggest like this:

CALCULATE (
    SUM ( Sales[Amount] ),
    ALL ( Sales ),
    FILTER ( Location, Location[Country] = RELATED ( Location[Country] ) )
)

First, the context transition turns the row context into filter context. Then, the ALL removes any filter context on Sales and the FILTER adjusts the filtering to aggregate at the granularity specified.

ALLEXCEPT doesn't work because Country isn't a column of Sales.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...