Trailing Twelve Months SUM not calculating in powerBI correctly - powerbi

Trying to add a measure in PowerBI that calculates the rolling 12-month sum of sales and the measure works fine up until the most recent 12 months worth of data. Not sure what's causing this error. Below is the data and code in PowerBI I'm using.
TTM MRR =
CALCULATE (
SUM ( MRR[MONTHLY_REV] ),
FILTER (
ALL ( MRR[CLOSE_MONTH] ),
AND (
MRR[CLOSE_MONTH] <= MAX ( MRR[CLOSE_MONTH] ),
DATEADD ( MRR[CLOSE_MONTH], 1, YEAR ) > MAX ( MRR[CLOSE_MONTH] )
)
)
)
Data:
[Excel Data]

It might be easier with DATESINPERIOD.
TTM MRR =
VAR PeriodEnd = MAX ( MRR[CLOSE_MONTH] )
RETURN
CALCULATE (
SUM ( MRR[MONTHLY_REV] ),
DATESINPERIOD ( MRR[CLOSE_MONTH], PeriodEnd, -12, MONTH )
)

Related

Cumulative sum by months in Powerbi DAX

I want to show the cumulative sum per month, I have the number per month but need to show the sum up of previous months in each month..
I used the following measure:
RT FAC =
CALCULATE (
CALCULATE(SUM('Injuries'[Total]), 'Injuries'[Classification] = "FAC"),
FILTER(ALLSELECTED('Calendar Table'),
'Calendar Table'[Date]<= 'Calendar Table'[Date]))
But it gave me the total in all the months:
How can I show the running total such as:
What IF you try this with small change?
RT FAC =
VAR MaxDate =
MAX ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
SUM ( 'Injuries'[Total] ),
FILTER (
ALL ( 'Injuries'[Classification] ),
'Injuries'[Classification] = "FAC"
),
FILTER ( ALLSELECTED ( 'Calendar Table' ), 'Calendar Table'[Date] <= MaxDate )
)

Running total of a measure which use DATESINPERIOD and months from fact table

I am using a measure below to display the months from fact table as described here:
Billings12Months =
CALCULATE (
SUM ( 'Datatable'[Allowable] ),
DATESINPERIOD ( DimDate[Date], MIN ( DimDate[Date] ), +12, MONTH )
)
My attempt to get the running total of above measure is failing:
BillingsRunningTotal =
CALCULATE (
[Billings12Months],
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
BillingsRunningTotal2 =
SUMX (
FILTER (
ALLSELECTED ( DimDate[Date] ),
DimDate[Date] <= MAX ( ( DimDate[Date] ) )
&& YEAR ( DimDate[Date] ) = YEAR ( MIN ( DimDate[Date] ) )
),
[Billings12Months]
)
[BillingsRunningTotal] return same values as [Billings12Months] (please see screen 1 attached) and
[BillingsRunningTotal2] return wrong values and month start from Jan, 17 instead of May, 17 (please see screen-2)
Please help me to calculate the running total. If possible please describe how your solution is working so that I can be better in DAX.
Update:
Please see the screen-3 below for the output when I use the measure suggested by Kosuke:
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
The months are from fact table (not from a Date table) and I think DATESINPERIOD plays a role to calculate and display the months. When we use SUM ( Datatable[Allowable] ), there would be a single month as dictated by the slicer. So we need to use DATESINPERIOD with rolling month calculation logic (DimDate[Date] <= MAX ( DimDate[Date] )) or virtually sum the [Billings12Months], It is where I am failing.
Thanks
You are almost there with the first attempt, however what to calculate is not [Billings12Months], but SUM( Datatable[Allowable] ).
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
Essentially, [Billings12Months] and [BillingsRunningTotal] are same in calculating the sum of Datatable[Allowable], but the only difference is each measure calculates for different scope of period. Therefore, the right way of thinking is to wrap SUM ( Datatable[Allowable] ) in CALCULATE, with different filter parameters.

My measure doesn`t work in case of date filters

enter image description hereI have daily values of closing balances of balance sheet. I want to have a measure which calculates the percentage change between two dates. The issue is that on weekdays the values are empty. So I have written the following measure which works when there are no filters, but when I place a date filter, the values of the measure become zeros.
Change_BS %(daily) =
IF (
SUM ( 'DB Daily_BS'[Value] ) = 0,
0,
IF (
SUM ( 'Date'[Weekday] ) = 1,
DIVIDE (
SUM ( 'DB Daily_BS'[Value] )
- CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), DATEADD ( 'Date'[Date], -3, DAY ) ),
CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), DATEADD ( 'Date'[Date], -3, DAY ) )
),
DIVIDE (
SUM ( 'DB Daily_BS'[Value] )
- CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), PREVIOUSDAY ( 'Date'[Date] ) ),
CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), PREVIOUSDAY ( 'Date'[Date] ) )
)
)
)
de a new Column called BS_Value which fills in the gaps for the weekends.
BS_Value =
var wday = MOD(WEEKDAY(Balance[Date]);7)
var daysBack = IF(wday < 2; wday + 1)
return CALCULATE(SUM(Balance[Value]);Balance[Date] = EARLIER(Balance[Date]) - daysBack)
Based on this column I made BS-Change what is the increment/decrement form previous:
BS_Change = Balance[BS_Value] - CALCULATE(SUM(Balance[BS_Value]);Balance[Date] = EARLIER(Balance[Date]) - 1)
End result:
You can now make measures or other columns based on those values. I need to say a more dynamic solution is possible where we would go for a lookup of the last value not zero. I did not do this because I can imagine the balance is zero and it would then skip those values.

How do I calculate the sum of Value for the last 6 sprints using DAX

Problem:
I need a calculated measure in DAX that sums the Value column for the last 6 sprints. I am basing the last 6 sprints on the DimSprintEndDateKey in descending order.
Table structure in PowerBI
The DAX that I am using:
CALCULATE (
SUM ( factSprint[Value] ),
FILTER (
ALL ( factSprint ),
COUNTROWS (
topn(6,
FILTER (
factSprint,
EARLIEST( RELATED ( dimSprint[DimSprintEndDateKey] ) )
> RELATED ( dimSprint[DimSprintEndDateKey] )
),RELATED ( dimSprint[DimSprintEndDateKey] ), DESC
)
)
)
)
I am assuming that the relationship on your tables is between 'dimSprint'[dimSprintKey] and 'FactSprint'[dimSprintKey].
That being the case, this measure could work for you.
Total Value Last Six Sprints =
VAR endDateSprint =
LOOKUPVALUE (
'dimSprint'[dimSprintEndDateKey],
'dimSprint'[dimSprintKey], SELECTEDVALUE ( 'FactSprint'[dimSprintKey] )
)
VAR dimTableFiltered =
FILTER ( 'dimSprint', 'dimSprint'[dimSprintEndDateKey] <= endDateSprint )
RETURN
CALCULATE (
SUM ( 'FactSprint'[Value] ),
ALL ( 'FactSprint' ),
TOPN ( 6, dimTableFiltered, [dimSprintEndDateKey], DESC )
)
Use it on a matrix visual (or pivottable). Be sure to put 'FactSprint'[dimSprintKey] or 'FactSprint'[SprintPK] on Rows and [Total Value Last Six Sprints] on Values.

How can I calculate a rolling average in DAX/PowerBI?

After going through several posts on StackOverflow and the PowerBI forums, I still can't figure out how to calculate a rolling average based on a given period- in my case, a 30-day rolling average.
Most of the posts I've seen advocate something either identical or really similar to this:
Rolling Sum :=
CALCULATE (
[Sales],
FILTER (
ALL ( Sales ),
[Date]
>= MAX ( Sales[Date] ) - 365
&& [Date] <= MAX ( Sales[Date] )
)
)
(code taken from this post)
...and yet, I can't seem to get the proper values.
In my case, I have the following:
"closing date" for a given loan (column)
loan count (measure)
closing length (column)- length of time (in days) to close a loan
What I'd like to calculate is the rolling 30 day average for any given day. I coded the following:
Rolling Average =
CALCULATE (
SUM(Query1[Closing_Length])/[Loan Count],
FILTER (
ALL ( Query1 ),
[Closing Date].[Date]
>= MAX ( Query1[Closing Date] ) - 30
&& [Closing Date] <= MAX ( Query1[Closing Date] )
)
)
To check the results, I used a visual filter to examine one month's worth of data and these are the results:
Note the totals row at the bottom; for this given period, there are 102 loans and it took an aggregate of 3922 days for them to close. The average I'd like to calculate is 3922/102, which should equal approximately 38.45 days. Instead, we see 42.
How can I fix this?
Measure based solution:
Rolling Average Measure =
VAR A =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Closing Length], 2 )
)
VAR B =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Loan Count], 2 )
)
RETURN
A / B
Calculated column based solution:
Rolling Average =
VAR CurrentDate = 'Query'[Closing Date]
VAR T =
FILTER ( 'Query', [Closing Date] <= CurrentDate )
RETURN
ROUND ( SUMX ( T, 'Query'[Closing Length] ) / SUMX ( T, [Loan Count] ), 2 )
Print Screen: