Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DAX SUMX: Storing a filtered table in a VAR and reference its columns later on in the expression

Tags:

powerbi

ssas

dax

In a measure (SUMX) I am filtering a table and storing it in a variable.

var currency = factFx[ALPHABETIC_CURRENCY_1]

var fxRates = FILTER(
    factMarketDataExchangeRates;
    factMarketDataExchangeRates[FX_CURRENCY] = currency
)

Then I need to do calcs that include further filtering fxRates

var exchangeRateOnTradeDate = CALCULATE(
    [Measure];
    FILTER( 
        fxRates;
        fxRates[CURVE_DATE] = tradeDate
    )
)

This throws an error in SSDT Cannot find table fxRates

Also It appears intellisense is not working. But each of the following does work. But is this expected behaviour?

Without table prefix:

var exchangeRateOnTradeDate = CALCULATE(
    [Measure];
    FILTER( 
        fxRates;
        [CURVE_DATE] = tradeDate
    )
)

With the underlying table's prefix:

var exchangeRateOnTradeDate = CALCULATE(
    [Measure];
    FILTER( 
        fxRates;
        factMarketDataExchangeRates[CURVE_DATE] = tradeDate
    )
)
like image 714
baouss Avatar asked Dec 20 '25 21:12

baouss


1 Answers

Yes, this is the expected behavior. You can only use the table[COLUMN] syntax for tables in your data model.

Both of your working versions are equivalent to substituting in the definition of fxRates.

var currency = factFx[ALPHABETIC_CURRENCY_1]

var exchangeRateOnTradeDate =
CALCULATE (
    [Measure];
    FILTER (
        FILTER (
            factMarketDataExchangeRates;
            factMarketDataExchangeRates[FX_CURRENCY] = currency
        );
        factMarketDataExchangeRates[CURVE_DATE] = tradeDate
    )
)

Since [CURVE_DATE] ultimately derives from factMarketDataExchangeRates, using that table prefix is really what's happening under the hood, but you are allowed to use the other version where that table is abstracted away and doesn't clutter your code.

The important thing to remember is that the fxRates variable isn't actually a table in this case, but rather a semantic trick to write code more legibly.

like image 86
Alexis Olson Avatar answered Dec 24 '25 09:12

Alexis Olson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!