Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
a68tbird
Resolver II
Resolver II

AverageIfs in DAX / LOOKUPVALUE

Hello All -

  I'd like to add a calculated column to my Sales table (AllSales) that will return the relevant exchange rate.  The ExchangeRate table has daily rates, in four currencies.  What I would like to do is lookup the currency and date of transaction and return the monthly average.  I have been able to do this in Excel as such: 

 

AVERAGEIFS(ExchangeRates[Rate],ExchangeRates[Currency],cell_with_Currency,ExchangeRates[Year],YEAR(salesDate),ExchangeRates[ClosingMonth],MONTH(salesDate))

 

So if the sale was on November 15, 2021, in USD, the formula would return the average USD closing rate for November.  

 

Trying to replicate this in DAX has me stumped.  I've tried different combinations of AVERAGEX with FILTER and LOOKUPVALUE, but usually get a "too many values" returned error.  

 

Any suggestions?

Thanks!

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@a68tbird Try something like:

Column = 
  VAR __Date = [salesDate]
  VAR __Year = YEAR(__Date)
  VAR __Month = MONTH(__Date)
RETURN
  AVERAGEX(FILTER(ALL('ExchangeRates'),'ExchangeRates'[Year] = __Year && 'ExchangeRates'[ClosingMonth] = __Month),'ExchangeRates'[Rate])

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
a68tbird
Resolver II
Resolver II

Thank you both, @Greg_Deckler  and @Ashish_Mathur .  I used your solution @Greg_Deckler , and included currency in the filter too.  Works perfectly!

Ashish_Mathur
Super User
Super User

Hi,

Write these calculated column formulas in the Data Table

Year = year(data[sales date])

Month number = month(data[sales date])

Exchange rate = calculate(average(rate[exchange rate]),filter(rate,rate[year]=earlier(data[year])&&rate[month number]=earlier(data[month number])&&rate[currency]=earlier(data[currency])))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Greg_Deckler
Super User
Super User

@a68tbird Try something like:

Column = 
  VAR __Date = [salesDate]
  VAR __Year = YEAR(__Date)
  VAR __Month = MONTH(__Date)
RETURN
  AVERAGEX(FILTER(ALL('ExchangeRates'),'ExchangeRates'[Year] = __Year && 'ExchangeRates'[ClosingMonth] = __Month),'ExchangeRates'[Rate])

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.