0
votes

I have a requirement in Power Pivot where I need to show value based on the Dimension Column value.

If value is Selling Price then Amount Value of Selling Price from Table1 should display, if Cost Price then Cost Price Amount Should display, if it is Profit the ((SellingPrice-CostPrice)/SellingPrice) should display

My Table Structure is Table1:-

table 1

Table2:- table 2

Required Output:-

output

If tried the below option:-

1. Calculated Measure:=If(Table[Category]="CostPrice",[CostValue],If(Table1[category]="SellingPrice",[SalesValue],([SalesValue]-[CostValue]/[SalesValue])))
*[CostValue]:=Calculate(Sum(Table1[Amount]),Table1[Category]="CostPrice")
*[Sales Value]:=Calculate(Sum(Table1[Amount]),Table1[Category]="SellingPrice")

Tried this in both Calculated Column and Measure but not giving me required output.

2
You might want to take a look at how this blog post uses the 'hasonevalue' option. powerpivotpro.com/2012/06/dax-making-the-case-for-switchLukasz P.

2 Answers

1
votes
Cost:=
CALCULATE(
    SUM( Table1[Amount] )
    ,Table1[Category] = "CostPrice"
)

Selling:=
CALCULATE(
    SUM( Table1[Amount] )
    ,Table1[Category] = "SellingPrice"
)

Profit:=
DIVIDE(
    [Selling] - [Cost]
    ,[Selling]
)

ConditionalMeasure:=
IF(
    HASONEFILTER( Table2[Category] )
    ,SWITCH(
        VALUES( Table2[Category] )
        ,"CostPrice"
        ,[Cost]
        ,"SellingPrice"
        ,[Selling]
        ,"Profit"
        ,[Profit]
    )
    ,[Profit]
)

HASONEFILTER() checks that there is filter context on the named field and that the filter context includes only a single distinct value.

This is just a guard to allow our SWITCH() to refer to VALUES( Table2[Category] ). VALUES() returns a table of all distinct values in the named column or table. So, a 1x1 table can be implicitly converted to a scalar, which we need in SWITCH().

SWITCH() is a case statement.

Our else condition in the IF() is just returning [Profit]. You might want something else, but it's unclear what should happen at the grand total level. You can leave this off, and the measure will be blank in IF()'s else condition.

0
votes

I was thinking about this a little. I'm not sure why you have your categories on rows. Usually the data set would have columns like: item | CostPrice | SellingPrice | Profit. Then you can just use the columns to define your fields. The model becomes easier and more maintainable.