Forum Discussion
DAX: Using variables inside iterators such as SUMX
- 7 years ago
Thanks for pointing me in the right direction :)
It turns out you CAN put variables inside iterators, you just need to put them after the first argument.
This is great because it can greatly enhance readability
(if you are wondering what all the semicolons ; are about in the code, its because of language settings, its the same as a comma , in us versions)So like this:
SUMX( tableName;
-- put vars here:
VAR myVariable = tableName[somevalue]
VAR myOtherVariable = tableName[someothervalue]
-- after vars, put the RETURN:
RETURN
-- then use the variables in making the return calculations:
myVariable * myOtherVariable
)---------
Heres an example from what I was working on:
SUMXVARS =
VAR toCurrency = [Selected Currency] -- from slicer
VAR currencyRatePeriod = [Selected Currency Rate Period] -- from slicer
RETURN
SUMX( 'dummy sales data';
VAR currRowAmount = 'dummy sales data'[amount]
VAR currRowTransactionCurrency = 'dummy sales data'[transaction currency]
VAR currRowDate = 'dummy sales data'[date]
-- depending on user chosen rate period, we change the date to look for
VAR targetCurrencyDate = SWITCH(currencyRatePeriod;
"daily"; currRowDate;
"monthly"; DATE( YEAR(currRowDate); Month(currRowDate);1);
"yearly"; DATE( YEAR(currRowDate); 1;1))
VAR currencyRate = IF('dummy sales data'[transaction currency] = toCurrency;
-- from -> to currency is the same, therefore rate is ALWAYS 1
1;
-- from -> to currency is different, we need to convert
-- go find the right exchange rate value:
LOOKUPVALUE(currencyModel[rate]; currencyModel[from currency code]; currRowTransactionCurrency ;
currencyModel[to currency code]; toCurrency;
currencyModel[date]; targetCurrencyDate;
currencyModel[rate period]; currencyRatePeriod)
)
RETURN
currRowAmount * currencyRate
)
in this article one of the examples has variables defines within ADDCOLUMN, followed by RETURN
https://www.sqlbi.com/articles/variables-in-dax/
so your example would be like this (no clue if it works though)
MyMeasure =
SUMX(
-- where would the variables? like this...? VAR currRowExchangeRate = 'myTable'[transactionCurrencyCode] RETURN
currRowExchangeRate * 2 -- and more code inside the SUMX here... ) -- sumx end
other than that you can try using GROUPBY with CURRENTGROUP
https://msdn.microsoft.com/en-us/query-bi/dax/groupby-function-dax#currentgroup
Thanks for pointing me in the right direction :)
It turns out you CAN put variables inside iterators, you just need to put them after the first argument.
This is great because it can greatly enhance readability
(if you are wondering what all the semicolons ; are about in the code, its because of language settings, its the same as a comma , in us versions)
So like this:
SUMX( tableName;
-- put vars here:
VAR myVariable = tableName[somevalue]
VAR myOtherVariable = tableName[someothervalue]
-- after vars, put the RETURN:
RETURN
-- then use the variables in making the return calculations:
myVariable * myOtherVariable
)
---------
Heres an example from what I was working on:
SUMXVARS =
VAR toCurrency = [Selected Currency] -- from slicer
VAR currencyRatePeriod = [Selected Currency Rate Period] -- from slicer
RETURN
SUMX( 'dummy sales data';
VAR currRowAmount = 'dummy sales data'[amount]
VAR currRowTransactionCurrency = 'dummy sales data'[transaction currency]
VAR currRowDate = 'dummy sales data'[date]
-- depending on user chosen rate period, we change the date to look for
VAR targetCurrencyDate = SWITCH(currencyRatePeriod;
"daily"; currRowDate;
"monthly"; DATE( YEAR(currRowDate); Month(currRowDate);1);
"yearly"; DATE( YEAR(currRowDate); 1;1))
VAR currencyRate = IF('dummy sales data'[transaction currency] = toCurrency;
-- from -> to currency is the same, therefore rate is ALWAYS 1
1;
-- from -> to currency is different, we need to convert
-- go find the right exchange rate value:
LOOKUPVALUE(currencyModel[rate]; currencyModel[from currency code]; currRowTransactionCurrency ;
currencyModel[to currency code]; toCurrency;
currencyModel[date]; targetCurrencyDate;
currencyModel[rate period]; currencyRatePeriod)
)
RETURN
currRowAmount * currencyRate
)