Calculate the average for the last 30 days excluding today. DAX - dax

I would like to calculate the average revenue for the last 30 days (not including today)
I tried the following formula but the amount calculated is incorrect:
CALCULATE(
AVERAGE(table[Revenue]),
FILTER(table,DATEADD(table[date],-30,DAY))
)
How can I exclude today in the average?
If i wanted to compare that result with the 30 days before that (i.e between -30 days and -60days) should i use datesinperiod?

The DATESBETWEEN function is the most intuitive to me.
Previous30DayAverage =
VAR CurrentDate = LASTDATE(table[date]) --Or TODAY() or whatever you choose
RETURN
CALCULATE(
AVERAGE(table[Revenue]),
DATESBETWEEN(table[date], CurrentDate - 30, CurrentDate - 1)
)
I think you can see how to tweak this for -30 to -60 days.

Related

calculate running total using multiple filters

I am trying to output a value on a power BI report, equal to the running total for the current fiscal year.
The underlying table contains multiple FY values, and is organized such that each charge has a calculated field containing the fiscal year it was applied to, but there are also projected monthly charges in the table which are in the future.
For example, if I choose FY 2022, I want to return a sum of all charges for FY 2022 which have occurred prior to today.
The following snippet returns all FY charges, including future ones labelled FY 2022
Cum FY Invoice Total = CALCULATE('Invoice Amounts'[Invoice Total],FILTER(ALL('Calendar'),'Calendar'[Fiscal Year]=2022))
It looks like you have posted wrong cumulative sum formula because correct formula should include && 'Calendar'[Date] < MAX('Calendar'[Date]) condition.
Also do not forget that you can write more complex conditions inside FILTER() function using && (AND) or || (OR).
I added && 'Calendar'[Date] < NOW() to your measure and filtered out future charges.
Cum FY Invoice Total =
CALCULATE(
SUM('Invoice Amounts'[Invoice Total]),
FILTER(
ALL('Calendar'),
'Calendar'[Fiscal Year] = 2022
&& 'Calendar'[Date] < MAX('Calendar'[Date])
&& 'Calendar'[Date] < NOW()
)
)
If you do not want to see future charges at all then you should write one more measure:
Hide Future Charges =
INT(
SELECTEDVALUE('Calendar'[Date]) < NOW()
)
and add it to your visual on Filters panel choosing 1 as filter value:
Future charges hiding

Dax Power Pivot Cumulative Total Over Development Period Dimension

I'm new to Dax and I'm struggling to get the results I require with a running total.
I hope I've give you enough information below to help..
thank for any help and pointer in advance.
I'm trying to create an insurance triangle, I have 3 tables fact_transaction_claims_payments, dimension_development_Periods and reporting_upto_information
fact_transaction_claims_payments
dimension_development_Periods
reporting_upto_information
diagram view
I've managed to get my running total to work but only where there is a development period held in the fact table and not all the ones in between held in my development period dimension
Power Pivot Running Total
for example i'd expect development period 1 - 10 to total 0.00 and 13 to 18 to total 103,710
but i can seem to get these to appear in my pivot.
Running Total Dax Expression
RT_TotalAmount1:=VAR CurrentDevelopmentPeriod =
CALCULATE(
DATEDIFF(
IF(
MAX('fact_transaction_claims_payments'[cUWYear]) < 2011
, DATE(MAX('fact_transaction_claims_payments'[cUWYear]),1,1)
, DATE(MAX('fact_transaction_claims_payments'[cUWYear]),4,1)
)
,MAX('fact_transaction_claims_payments'[EndOfMonthDate])
,MONTH)
, 'dimension_development_Periods' ) +1
VAR MaxDevelopmentPeriod =
CALCULATE(
DATEDIFF(
IF(
MAX('fact_transaction_claims_payments'[cUWYear]) < 2011
, DATE(MAX('fact_transaction_claims_payments'[cUWYear]),1,1)
, DATE(MAX('fact_transaction_claims_payments'[cUWYear]),4,1)
)
,MAX('reporting_upto_information'[EndOfMonthDate])
,MONTH)
, 'dimension_development_Periods' ) +1
VAR RunningTotal =
CALCULATE (
SUM('fact_transaction_claims_payments'[TotalAmount])
,FILTER (
ALL ('fact_transaction_claims_payments')
,'fact_transaction_claims_payments'[DevelopmentMonthNumber] <= CurrentDevelopmentPeriod
)
)
RETURN
RunningTotal
I've tried adding the ALL('dimension_development_Periods') into the VAR DevelopmentPeriod
but this just puts the grand total against every development period.
I'm thinking I now need to use the RunningTotal to calculate against the Development Period Dimension filtered <= the max development period for the cUWYear but I'm not sure on how to implement this and I need some advice can anyone help.

Carbon its parsing my date range +1 day

I have the date range
{"from":"2018-02-12T23:00:00.703Z","to":"2018-02-13T22:59:59.703Z"}
When I do a
$periodFrom = Carbon::parse($request->from);
$periodTo = Carbon::parse($request->to);
And then I want to get the number of dates between those dates I do
$days = $periodTo->diffInDays($periodFrom);
And for some reason its giving me 0 days instead of 1 day
Edit:
You can do this:
$days = round($periodTo->diffInHours($periodFrom) / 24);
Instead of round() you can also use ceil() method, it depends on what exactly result you want to get for 1.2 days, 1.8 days etc.
If you want to get 1 day instead of 0, but for all other cases you want to get just full days (8 for 8.9 for example), do this:
$diff = $periodTo->diffInHours($periodFrom);
$days = $diff === 0 ? 1 : $diff;

Get what date it is based on week number and day number asp classic?

I need to know what date it is based on a week number and a day number.
So if I have weeknr=3 and day=1(for today- sunday) and lets say the year=2016 how can I make this into 2016-01-24.
Any input really appreciated, thanks.
Your best help will be the DatePart function (see here for docs). It's not altogether straightforward, because there are options to consider like "first day of week" and "first week of year", but you can define those in DatePart.
This would be my solution:
option explicit
function getDateByWeek(year, week, day)
dim dt, woy, add_days
dt = DateSerial(year, 1, 1)
do
woy = DatePart("ww", dt, vbSunday, vbFirstFullWeek)
' possibly change options [,firstdayofweek[,firstweekofyear]]
dt = DateAdd("d", 1, dt)
loop while woy<>1
add_days = week * 7 + day - 2
' -1 because we already added one day extra in the loop
' -1 to correct given day (sunday = 1)
getDateByWeek = DateAdd("d", add_days, dt)
end function
Response.Write "RESULT: " & getDateByWeek(2016, 3, 1) ' -> 24.01.2016
I start by finding the first day of the first week in a loop and then adding the cumulative amount of days to have a result.
Well the best way is to work out what the week start of the year is and take it from there.
Firstly, the first Thursday of any year is always in the first week of the year, so a simple calculation of adding the correct number onto the 1st January is obvious. After this we simply mulitply out the weeks and add the days:
Const C_THURSDAY = 5
Function GetDateFromYWD(y, w, d)
Dim tDate, aDate
'Get the first of the year...
tDate = DateSerial(y, 1, 1)
'Workout the start of the first week in the year...
aDate = tDate + Int(WeekDay(tDate)>C_THURSDAY And 7) - WeekDay(tDate)
'Add on the number of weeks plus days we're looking for...
aDate = aDate + (w * 7) + d
GetDateFromYWD = aDate
End Function

Calculating holidays

A number of holidays move around from year to year. For example, in Canada Victoria day (aka the May two-four weekend) is the Monday before May 25th, or Thanksgiving is the 2nd Monday of October (in Canada).
I've been using variations on this Linq query to get the date of a holiday for a given year:
var year = 2011;
var month = 10;
var dow = DayOfWeek.Monday;
var instance = 2;
var day = (from d in Enumerable.Range(1,DateTime.DaysInMonth(year,month))
let sample = new DateTime(year,month,d)
where sample.DayOfWeek == dow
select sample).Skip(instance-1).Take(1);
While this works, and is easy enough to understand, I can imagine there is a more elegant way of making this calculation versus this brute force approach.
Of course this doesn't touch on holidays such as Easter and the many other lunar based dates.
And it gets complicated if you have to consider non-christian holidays. For example, jewish holidays are based on the jewish calender..
Maybe not so elegant, but more error prone - you can just add a table of all relevant holidays for the next 100 years.
int margin = (int)dow - (int)new DateTime(year, month, 1).DayOfWeek;
if (margin < 0) margin = 7 + margin;
int dayOfMonth = margin + 7*(instance - 1) //this is for 0-based day number, add 1 if you need 1-based. instance is considered to be 1-based
Please note that I wrote it without trying to compile, so it is to give the idea, but may require some clean up. Hope this helps!

Resources