cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
iplaygod
Resolver I
Resolver I

DAX: Using variables inside iterators such as SUMX

Sorry if this has been answered before I could not find any info on it.

I would like to know if it is allowed / possible to use variables inside an iterator.

 

Basically I would like to grab some value from current row  inside the SUMX and store that in a variable, primarily because It would make the code readability much better imo.
Basically I want to reassign the value inside the iterator because it has row context.
Am I right in presuming that If I assign a variable outside the SUMX it will not update itself for each row in the SUMX following it?

 

So could I do something similar to this?

MyMeasure = SUMX(
                                    -- where would the variables? like this...?
                                           VAR currRowExchangeRate = 'myTable'[transactionCurrencyCode]
                                    -- and more code inside the SUMX here...
) -- sumx end

I cant find any way of doing it when I much around in Power BI.


If you guys know a way of storing the current row value in a variable in ANY way when using iterators, please let me know and give an example. If not at all possible please let me know that too?
Thanks in advance!                      

                                          

                 

1 ACCEPTED SOLUTION

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          
    )

View solution in original post

2 REPLIES 2
Stachu
Community Champion
Community Champion

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

 



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Proud to be a Super User!

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          
    )

Helpful resources

Announcements
August 2022 update 768x460.jpg

Check it Out!

Click here to learn more about the August 2022 updates!

August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors