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
Anonymous
Not applicable

DAX help needed

I woud like to calculate previous month data . 

 

Example: I do have currency exchange rate table which  has data until 202008  whenver the current month exchange rates are empty. It has to pick up previous month exchange rates.

 

sample data.

 

Data.jpg

 

 

 

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@Anonymous 

If the current month Rate is zero or blank then it picks the previous available month's rate: Make sure your Month column is formatted as numbers 202008. Add the following code as a new column to the table.

Exchange Rate Complete = 
VAR SC = [Starting Currency]
VAR DC = [Destination Currency]
VAR M = [Month]
VAR PRMONTH = MAXX(FILTER( Table8, [Starting Currency]=SC && [Destination Currency] = DC && [Month] < M),[Month])
VAR PRRATE = MAXX(FILTER( Table8, [Starting Currency]=SC && [Destination Currency] = DC && [Month] = PRMONTH),[Exchange Rate])
RETURN
IF( [Exchange Rate]=0 || ISBLANK([Exchange Rate]), PRRATE,[Exchange Rate])

 

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

6 REPLIES 6
Fowmy
Super User
Super User

@Anonymous 

If the current month Rate is zero or blank then it picks the previous available month's rate: Make sure your Month column is formatted as numbers 202008. Add the following code as a new column to the table.

Exchange Rate Complete = 
VAR SC = [Starting Currency]
VAR DC = [Destination Currency]
VAR M = [Month]
VAR PRMONTH = MAXX(FILTER( Table8, [Starting Currency]=SC && [Destination Currency] = DC && [Month] < M),[Month])
VAR PRRATE = MAXX(FILTER( Table8, [Starting Currency]=SC && [Destination Currency] = DC && [Month] = PRMONTH),[Exchange Rate])
RETURN
IF( [Exchange Rate]=0 || ISBLANK([Exchange Rate]), PRRATE,[Exchange Rate])

 

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Anonymous
Not applicable

@Fowmy This working fine. Let me do further testing. Thanks a lot.

Anonymous
Not applicable

@Fowmy Thanks a lot . Its working fine.

Greg_Deckler
Super User
Super User

@Anonymous - You can do it like:

New Exchange Rate Column =
  IF([Exchange Rate]=0,MAXX(FILTER('Table',[Month] = EARLIER([Month] -1) && [Starting Currency] = EARLIER([Starting Currency]) && [Destination Currency]=EARLIER([Destination Currency])),[Exchange Rate]),[Exchange 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...
amitchandak
Super User
Super User

@Anonymous , you can convert this month to date and use time intelligence

 

Date =Date(left([Month],4),right([month],2),1)

Or use can use rank for time intelligence, refer my month and week blog for that. Please create a separate month year /date table

like

Month Rank = RANKX(all('Date'),'Date'[Month],,ASC,Dense)
This Month = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])))
Last Month = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])-1))
Last year Month= CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=(max('Date'[Month Rank]) -52)))

 

Time intelligence

MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))
last MTD (complete) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date],-1,MONTH))))
previous month value =  CALCULATE(sum('table'[total hours value]),previousmonth('Date'[Date]))

diff = [MTD Sales]-[last MTD Sales]
diff % = divide([MTD Sales]-[last MTD Sales],[last MTD Sales])

last year MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-12,MONTH)))
last year MTD (complete) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date],-12,MONTH))))
Month behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Month))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

 

Power BI — MTD
https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
Power BI — WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-Last-Week/ba-p/1051123

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184


Appreciate your Kudos.

 

 

aj1973
Community Champion
Community Champion

Hi @Anonymous 

 

you need to add a Calendar table in your model in order to use Time intelligence functions.

Do you have it in your model?

 

Regards
Amine Jerbi

If I answered your question, please mark this thread as accepted
and you can follow me on
My Website, LinkedIn and Facebook

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.