How to apply CONTAINS clause on hierarchical categories to create scenario analysis in Power BI

你说的曾经没有我的故事 提交于 2019-12-24 10:56:30

问题


Building upon the solution given by @olly (Power BI: How to scenario analysis, where the selector "looks up" the select value from slicer and gets values from that row) & file: https://pwrbi.com/so_55281950-2/

In the sample file a "what if" or scenario analysis is created. Two slicers are used, one which selects the scenario, and another one which selects the objects to apply the scenario on. @Olly provided a clever solution to deal with the situation:

Value + Trend = 
SUMX ( 
    'Demo Fact Table';
    'Demo Fact Table'[Value] * 
        ( 1 +
            IF ( 
                ISFILTERED ( 'Item Chooser'[Item] ) &&
                CONTAINS ( 
                    'Item Chooser';
                    'Item Chooser'[Item];
                    'Demo Fact Table'[Item]
                ) && 
                HASONEVALUE ( 'Scenario - Trend'[Category] );
                VALUES ( 'Scenario - Trend'[Trend Rise] );
                'Demo Fact Table'[trend_default]
            )
        )
    )

Building upon this solution I've been trying to apply the same logic, but to my specific problem. In my problem I don't only have a single layer of "categories", but instead hierarchy of 3 levels. How would I go on about applying the same solution if my categories had hierarchies? So from my slicer I would select three things: Category1, Year and Category2, which would yield the trend_rise for the selection. and would apply this apply this trend_rise on the selected (4th slicer) rows, i.e. (item A,B or/and C)

category1      -   Year - Category2 - trend rise
POSITIVE-trends    2018   low         5%
POSITIVE-trends    2018   high        5%
POSITIVE-trends    2017   low         5%
NEGATIVE-trends    2017   very high   -5%
NEUTRAL-trends     2018   low         0%
POSITIVE-trends    2018   high        5%
NEUTRAL-trends     2017   low         5%
NEUTRAL-trends     2016   very high   15%

回答1:


You only need a small tweak to the measure, to check whether the trend_rise field has one value, and if so then use that, otherwise use the default:

Value + Trend = 
SUMX ( 
    'Demo Fact Table',
    'Demo Fact Table'[Value] * 
        ( 1 +
            IF ( 
                ISFILTERED ( 'Item Chooser'[Item] ) &&
                CONTAINS ( 
                    'Item Chooser',
                    'Item Chooser'[Item],
                    'Demo Fact Table'[Item]
                ) && 
                HASONEVALUE ( 'Scenario - Trend'[Trend Rise] ),
                VALUES ( 'Scenario - Trend'[Trend Rise] ),
                'Demo Fact Table'[trend_default]
            )
        )
    )

Now you can use slicers on all columns of your Scenario table.

See https://pwrbi.com/so_55332313/ for worked example file.



来源:https://stackoverflow.com/questions/55332313/how-to-apply-contains-clause-on-hierarchical-categories-to-create-scenario-analy

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