The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.
Hello,
I'm wrting here because I spent a lot hours to solve my problem and I give up... It's 2AM, and I'm so tired 🙂
I'm looking to compare sales between the current month and the previous one. But it's not easy at it looks...
I have a first table "Sales and Warranty" with all my sales. The table include the fields :
- Ref.Article (String), it's the ID of the product
- Sales Last 24 Mths (Integer) it's a calculated value from SAP BW wich represent the cumulated sales of the last 24 months at the moment
- Date Month Year (String)... Yes it's not a date field, but a String. The format is "JAN 2021"
So this table look something like that :
On the other hand, I have a Date table with this fields :
- Date (DateTime generate in DAX)
- MonthDateSAP (String with the exact same format of the "Date Month Year")
So, I want to add a new column to the Sales and Warranty table wich represent the Sales Last 24 Mths for the previous period.
This is the DAX code I wrote :
Sales Last 24 Mths (n-1) =
var Article = 'Sales and Warranty'[Ref.Article]
var FirstDayOfMonth =
CALCULATE(
FIRSTDATE('Date'[Date]),
FILTER('Date','Date'[MonthDateSAP]='Sales and Warranty'[Date Month Year]))
var FirstDayPrevMonth = EDATE(FirstDayOfMonth,-1)
var SAPMonth =
LOOKUPVALUE('Date'[MonthDateSAP],'Date'[Date],FirstDayPrevMonth)
return
CALCULATE(
SUM('Sales and Warranty'[Sales Last 24 Mths]),
FILTER('Sales and Warranty',
'Sales and Warranty'[Date Month Year] = SAPMonth && 'Sales and Warranty'[Ref.Article]=Article))
Thanks to read until here, and sorry for my poor english and the long long text, but I tried to describe my trouble as best I can.
Solved! Go to Solution.
I succeed !!!
I can't convert into to date in PowerBI, but I did it into power query.
Then I used my original code (very similar to your except you miss the article filter)
Download sample PBIX file with data and DAX
If you create a proper Date column in your Sales and Warranty table this is very easy. Here's the DAX for the new column
Date = DATEVALUE([Date Month Year])
You can then create measures for Sales This Month and Sales Last Month
Sales This Month = CALCULATE(SUM('Sales and Warranty'[Sales]), FILTER('Sales and Warranty', MONTH('Sales and Warranty'[Date]) = MONTH(TODAY())))
Sales Last Month = CALCULATE(SUM('Sales and Warranty'[Sales]), FILTER('Sales and Warranty', MONTH('Sales and Warranty'[Date]) = MONTH(TODAY())-1))
Which gives these results
Check my example file for this code.
Regards
Phil
Proud to be a Super User!
Thanks for your very quick response. The problem, I already tried to convert it into date, but it failed. The format of the [Date Month Year] look not standard. (and in French...)
I succeed !!!
I can't convert into to date in PowerBI, but I did it into power query.
Then I used my original code (very similar to your except you miss the article filter)
User | Count |
---|---|
65 | |
61 | |
60 | |
53 | |
30 |
User | Count |
---|---|
181 | |
83 | |
68 | |
49 | |
46 |