Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now
I want to calculate the average for L3 and L6, dynamically, which means with every new month sales value added in the table, BI automatically adjusts the period to calculate the average for last 3 months and 6 months. E.g. In Jan, it should calculate L3 as average{nov,dec,jan} and L6 as {aug,sep,oct,nov,dec,jan}; similarly, when I update the feb values in this table, upon refreshing, the DAX formula should automatically adjust the period as follows : L3{dec,jan.feb} and L6{sep,oct,nov,dec,jan,feb}
The additional problem is that the year field in my table is in numeric type and the month field is in text type and I am not able to convert or combine either of the fields to date type, so that I may use any of the date-based DAX functions
Solved! Go to Solution.
Assuming you only have 1 table - Table1 with column headers [Sales], [Date] then adding new measures:
3mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-3,MONTH))/3
6mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-6,MONTH))/6
12mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-12,MONTH))/12
24mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-24,MONTH))/24
Do you have separate Calendar table?
3mth = CALCULATE(SUM(sales[sale]),DATESINPERIOD(Calendar[Date],LASTDATE(Calendar[Date]),-3,MONTH))/3
Julian - nope , I dont't have a calendar table...what is that and do I have it? will this help the solution?
How do you set up the calendar table and connect it to my data table?
No you dont need one. There is alot of information/discussion on this topic - see this link
You could alway add a new column Date = FORMAT(Table1[Column1] &"-"& Table1[Column2],"MM-YYYY")
Julian - thanks for this.
couple of questions :
1. When u say "add column" - this is thru' the "add column" option under the "modelling" tab, right?
2. in your formula, what would be my corresponding columns for [column1] and [column2]? - should I have column1 filled with the names of the month, and column2, filled with the years?
3. i have 2 columns - year , filled with years but in numeric format and month, filled with names of the month, but in txt format - hope your solution will work on these columns
thanks
Yes, thats right "add column" from modelling tab. Put your month header name into [column1] and year into [column2], and the table name in front of each. "Format MM-YYY" sorts out the text and numeric formats.
so now I have a column added called [date]. So in your original formula, the calendar is a function or it should be replaced with the name of the table , [date], is now added to?
thanks
Assuming you only have 1 table - Table1 with column headers [Sales], [Date] then adding new measures:
3mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-3,MONTH))/3
6mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-6,MONTH))/6
12mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-12,MONTH))/12
24mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-24,MONTH))/24
This formula will not calculate the average correctly on the first two months of your data (here for Jan and Feb 2024).
To fix it you need to create a measure called count.
3mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-3,MONTH))/count
Thanks
Hi @cjulianm,
This is pretty much everything I am also trying to accomplish. But instead of "Table [Sale]" I need to get the average 3, 6, 12, and 24 from a measure "Ratio_Measure" I created.
This formula 3mth = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-3,MONTH))/3 is not working for me properly. Do you think you can help me?
I have a sample of my excel data and Power BI here at this link HERE
Thank you in advance
Michelle
Best practice is to create a Calendar table. then use the
3mth = CALCULATE([Ratio_Measure],DATESINPERIOD(Calendar[Date],LASTDATE(Calendar[Date]),-3,MONTH))/3
Hi @cjulianm,
Thank you for the help on this.
I found this measure below for the Average of 6 I needed, but do you know if there is a way we can make this measure a little bit more dynamic? Like, do some sort of variable where I can change the number of months I need? this project I am working on I need to take lots of different averages.
Hope I am not overcomplicating things.
@cjulianm Hi cJulianm, my apologies for brining this up again. I am very new to DAX and powerBI trying my best to find my way. Similiarly I am trying to calculate the average of the last 3 month's data. And have tried to follow you solutions.
Average Lead Time =
CALCULATE(average('View Shipments'[Total Transit Time]), DATESINPERIOD('Date Table'[Date], max('View Shipments'[Shipped (ASN)]),-3,MONTH))
However,I did not use the "Latestdate" command becuase my date column "Shipped (ASN)" contains duplicates, therefore "Datesinperiod" also give me error that there is duplicate. I created a calander table from 2010 - 2020, and used MAX instead and no error come out from this.
However, I tried to double check with my Excel to see of the calculation is correct. But it is not correct, would hope that you can help me why my formula isnt working. Thank you so much!
Hi - I know this post is ancient - however did you find a solution to your issue?
I'm finding the LASTDATE example does work in latest power bi (March 2023)
when using a percentage measure...
very frustrating....
so for me this:
Ah you know using MTD (totalmtd) in measures only works outside of this logic - ha, answered my own question in the end..
The LASTDATE formula works fine as long as you dont use totalmtd in the measure its refering to lol...
Check out the October 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
112 | |
108 | |
102 | |
94 | |
71 |
User | Count |
---|---|
173 | |
134 | |
132 | |
102 | |
95 |