calculate running total using multiple filters - dax

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

Related

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.

Converting simple DAX expression to MDX

I need to convert a DAX expression I'm using i PowerBI to MDX to use in Excel > Olap > Calculated Measure (or member?). I'm no developer and do not have full access to the database, but connect to it using analysis services.
This DAX measure is working. I want to the same logic in Excel as a calculated measure (or calculated member?). Running measure displays sales quantity where the products has a 1 in either FilterA, FilterB or FilterC.
CALCULATE(
[SalesQtn],
FILTER(
'Product',
'Product'[FilterA] = "1" || 'Product'[FilterB] = "1" || 'Product'[FilterC] = "1"
)
)
I'm 98% newbie in In DAX and 100% newbie in MDX. I've managed to do like below in Calculated Measure to filter using FilterA = "1", but I recon a function is needed for combination FilterB and C using OR operation
([Measures.[SalesQtn],
[Product].[FilterA].&[1])
Try the following:
SUM(
{
([Product].[FilterA].&[1], [Product].[FilterB].[All], [Product].[FilterC].[All]),
([Product].[FilterA].[All], [Product].[FilterB].&[1], [Product].[FilterC].[All]),
([Product].[FilterA].[All], [Product].[FilterB].[All], [Product].[FilterC].&[1])
},
[Measures].[SalesQtn]
)
Or if there are some rows where two FilterA and FilterB are 1 that might produce a wrong result. In that case try:
Sum(
Filter(
[Product].[FilterA].[FilterA].Members
* [Product].[FilterB].[FilterB].Members
* [Product].[FilterC].[FilterC].Members,
[Product].[FilterA].CurrentMember is [Product].[FilterA].&[1]
Or [Product].[FilterB].CurrentMember is [Product].[FilterB].&[2]
Or [Product].[FilterC].CurrentMember is [Product].[FilterC].&[1]
),
[Measures].[SalesQtn]
)

Calculated column that ignores row context

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]
)

Tabular 2016 - DAX measure: How to reference a calculation alias for aggregation

I want a total of a sum of all values which have expiry dates greater than the current context date, for each date.
Code:
EVALUATE(
VAR v = ADDCOLUMNS(
FILTER(
SUMMARIZE(
'Dates Expiry',
[Year]
),'Dates Expiry'[Year] <> BLANK()
) ,"X", CALCULATE(SUM('LM Property Tenant Unit'[CurrentRent]), FILTER(ALL('LM Property Tenant Unit'), 'LM Property Tenant Unit'[ExpiryYear] > 'Dates Expiry'[Year]))
)
RETURN(v)
)
The result of my DAX is 2 columns; year and value:
Dates Expiry[Year] [X]
2018 68917213.56
2019 68399351.84
2020 67828645.39
What I want to do now to is reference column "X" and sum the list of values to get a total.
I have tried referencing the column in another summarize but the sum
function want a table context.
Is there a way to reference "X" and aggregate to a single value?
Answer:
In the end I discovered the DAX GROUPBY function which allows one to reference the result set that was internally defined and apply further calculations to it:
GROUPBY(
FILTER(FILTER(
SUMMARIZE(
'Dates Expiry',
[Year]
,"X", CALCULATE(SUM('LM Property Tenant Unit'[CurrentRent]),
FILTER(FILTER(ALL('LM Property Tenant Unit'), 'LM Property Tenant Unit'[ExpiryYear] > 'Dates Expiry'[Year]), 'LM Property Tenant Unit'[CommenceDate] < TODAY()))
),'Dates Expiry'[Year] <> BLANK()), AND('Dates Expiry'[Year] > YEAR(TODAY()), 'Dates Expiry'[Year] < YEAR(TODAY())+16)
)
, "SX", SUMX(CURRENTGROUP(), [X])
)

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