Search code examples
relationshiplookuppowerpivot

How to use CALCULATE with LOOKUPVALUE and USERELATIONSHIP


I have three tables, one dim table called "ISO_ccy" only showing the ISO acronyms of currencies, one dim table showing the "home currency" of an entity ("entities") and another (fact) table ("trades") showing foreign exchange (FX) trades. The thing with FX trades is, they always have two currencies (ccy) involved, hence the latter table has two columns with currency ISO codes (and corresponding amounts). The two dim tables both only have one column with ISO ccy codes (the table "ISO_ccy" having distinct values only).

I now have one (active) relationship for currency 1 (ccy1) and one inactive for currency 2 (ccy2) between the "ISO_ccy" and the "trades" table. There is also an active relationship between "ISO_ccy" and "entities" tables.

I need to calculate the sum for each currency and each entity where the currency is not equal to the "home currency" of that entity.

Seems to be pretty straight forward for the ccy with an active relationship (ccy1):

    Sum_Hedges_activeRelation:=
    CALCULATE(
    SUM([Amount_ccy1]);
    FILTER(trades;trades[ccy1]>LOOKUPVALUE(entities[ccy];entities[name];trades[name]))
    )

The filter expression ensures that only amounts are shown where the ccy of a trade is not equal to the "home" ccy of the entity.

Here, I'm getting the desired result.

Now I need to do the same with the inactive relation (ccy2).

This is what I tried:

Sum_Hedges_in-activeRelation:=
CALCULATE(
SUM([Amount_ccy2]);
USERELATIONSHIP(trades[ccy2];ISO_ccy[ccy]);
FILTER(trades;trades[ccy2]<>LOOKUPVALUE(entities[ccy];entities[name];trades[name]))
) 

However, I only get an "empty" result.

I also tried to add "ALL(trades)" to the CALCULATE function. No results there as well.

So, I am a bit at a loss now, how I can make this work. Can you please help?


Solution


  • UPDATE 08 April 2019 with a solution:

    I found a solution to my problem here:

    sqlbi: USERELATIONSHIP in a Measure

    Now my forumlar looks like this:

    Sum_Hedges_in-activeRelation:=
    CALCULATE(
     CALCULATE(
     SUM([Amount_ccy2]);
     FILTER(trades;trades[ccy2]
     <>LOOKUPVALUE(entities[ccy];entities[name];trades[name]))
     );
    USERELATIONSHIP(trades[ccy2];ISO_ccy[ccy])
    )
    

    This is slightly different from the solution provided (for a column related context) in the referenced article, as I omitted the ALL() instruction in the outer CALCULATE(). I could not explain, though...