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.
Related
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
SCENARIO:
Monitor “Task” duration. Purpose is to track trend in changing Durations.
There is a framework that stores information about tasks in place.
There is a hierarchy setup using parent/child ID.
Columns; Task_ID, Parent_Task_ID, Start_Time, End_Time, Duration.
A hierarchy has been set up With Calculated Columns
Depth, Path, Level1-Level4. (according to DAX Patterns on Hierarchy)
Measures; Browsedepth and RowDepth has been set up to remove blanks in the report setup for empty levels.
DAX;
Sum Hierarchy =
VAR Val = [Sum Duration]
VAR ShowRow=[BrowseDepth] <= [RowDepth]
RETURN IF(ShowRow,Val)
CHALLENGE:
In the Task table all durations are correct, so there are no need to aggregate the duration of the children up to parent.
In Daxpatterns connected to hierarchies in the examples I have found, there are always aggregation up from children to parent, as the black numbers in the matrix under are showing
The goal is to find a way to create a measure that avoid aggregation from children to parent, and present the "Blue numbers in the picture above.
Do anyone have any pointers on the pattern or logic to use, it would be greatly appreciated.
Kind regards,
Atle Røen
The answer to this was to use MAX( 'Task'[Duration] ) and filter on the last task execution. Simple when you finally find the answer :D
DAX;
Max Task duration - Last run:=
CALCULATE (
MAX( 'Task'[Duration] ),
LASTDATE ( (
FILTER (
VALUES ( 'Task'[Start_Time] ),
MAX ( 'Calendar'[Date] ) >= 'Task'[Start_Time]
)
) )
)
Task duration - Last run - Hier:=
VAR Val = [Max Task duration - Last run]
VAR ShowRow = [BrowseDepth] <= [MaxNodeDepth]
RETURN IF(ShowRow,Val)
I'm trying to calculate the Total Price per Order number. It specifically needs to be a column, because I'll be needing it for further calculations.
Can someone help me write code that calculates the total per Order Number, instead of line amount as it does now?
Since it's a calcualted column, just avoiding any context transition gives a straightforward solution
Total Price Per Order =
VAR CurrentOrder = SalesDetail[Order Number]
RETURN
SUMX (
FILTER (
SalesDetail,
SalesDetail[Order Number] = CurrentOrder
),
SalesDetail[Unit Price] * SalesDetail[Quantity]
)
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.
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!