Cumulative sum by months in Powerbi DAX - powerbi

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

Related

DATEADD throwing an error when adding MAX

I am looking to get the sum of sales for the last 3 months. not including the unfinished month which is this month.
I have this Dax but its not accepting the DATEADD with the MAX. Is there any workaround?
Measure1 = CALCULATE (
SUM ( table1[Sales] ),
DATESINPERIOD (
table1[date],
DATEADD( MAX ( table1[date] ), -1,MONTH),
-3,
MONTH
)
)
DATEADD function requires a set of dates to computation. That is why it not works with MAX, which returns single value. Find more here.
You need to find the maximum date in the dataset, then filter out the current month from the date table. Try the measure as follows:
Measure1 =
VAR maxDate =
CALCULATE(
MAX( 'Calendar'[Date] ),
ALL('Calendar' )
)
VAR firstDay = DATE( YEAR( maxDate ), MONTH( maxDate ), 1 )
VAR maxK =
CALCULATE(
MAX('Calendar'[Date] ),
'Calendar'[Date] < firstDay
)
VAR result =
CALCULATE(
SUM( AmountPaid[PaymentAmt] ),
DATESINPERIOD('Calendar'[Date], maxK, -3, MONTH )
)
return result
The maxK part calculates maximum date excluding the latest month in my dataset. You have to adjust the measure a bit for your needs (e.g. use TODAY() instead maxDate).
Hope it helps.

Prev years YTD sales Dax formula

I am having some difficulties in creating a dax formula for calculating prev yr YTD sales.
I have written a formula but the same is not working.
I need to calculate the performance % yr over yr by comparing YTD sales of current year to YTD of prev yr sales.
any help would be appreciated
Sales sameperiod =
VAR first_date =
FIRSTDATE ( DATEADD ( 'Date'[Date], -12, MONTH ) )
VAR last_date =
LASTDATE ( DATEADD ( 'COGS Data'[Invoice Date], -12, MONTH ) )
RETURN
IF (
ISBLANK ( first_date ) || ISBLANK ( last_date ),
BLANK (),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
DATESBETWEEN ( 'Date'[Date], first_date, last_date )
)
)
there are multiple ways, but my go-to is creating a Date table, I assume you already have it.
Then you would create relationship to Fact table from DateKey, and a new matrix visual with rows from Date Table, for example Date and Month. And Measure would be like -
Revenue last year = IF(
HASONEVALUE ('Date'[Month]),
IF (
SUM ('COGS Data'[Final Unit Cost] ) <> BLANK(),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
SAMEPERIODLASTYEAR ('Date'[Date])
)
),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
DATESBETWEEN (
'Date'[Date],
EDATE ( MIN ('Date'[Date]), -12 ),
EDATE ( MAX ('COGS Data'[Invoice Date]), -12 )
)

Power BI- Query Edit

I am trying to create new measures that will only show the data value of the Last day every month and another measure will show the data value of every week Saturday in Power BI.
Many thanks in Advance.
The data is continuing and the next series of data will appended data wise
Output:
Need to create a measure which will show only last day of the Week value ( Example- Saturday is my Last of the Week and Count numbers I need to see as output)
Similarly for Month - Last day of the Month I need to see the Numbers of each service.
Do you have Calendar table mark as "Date Table", which is connected by relationship to your Table? If so, then you can use ENDOFMONTH ( 'Calendar'[issue_date] ) to get lastDateOfMonth
then simply:
ShowValueAtLastDayOfMonth =
calculate( sum(Table[Total sale]),
KEEPFILTERS(
filter(ALL('Calendar'[issue_date]),
'Calendar'[issue_date] = ENDOFMONTH ( 'Calendar'[issue_date] )
)
)
)
ShowOnlyOnSaturday =
calculate(sum(Table[Total sale]), KEEPFILTERS( FLITER(ALL('Calendar[issue_date]),
WEEKDAY ( SELECTEDVALUE('Calendar'[issue_date])) = 7 )))
Without a "Date Table" we need to replace 'Calendar'[issue_date] with your Table[Date] and ENDOFMONTH with:
VAR LastDateVisible =
CALCULATE ( MAX ( 'Table'[Date] ) )
VAR LastYearVisible =
YEAR ( LastDateVisible )
VAR LastMonthVisible =
MONTH ( LastDateVisible )
VAR DaysInMonth =
FILTER (
ALL ( 'Table'[Date] ),
YEAR ( 'Table'[Date] ) = LastYearVisible
&& MONTH ( 'Table'[Date] ) = LastMonthVisible
)
VAR LastDayInMonth =
MAXX (
DaysInMonth,
'Table'[Date]
)
VAR Result =
CALCULATETABLE (
VALUES ( 'Table'[Date] ),
'Table'[Date] = LastDayInMonth
)
RETURN
Result

Trailing Twelve Months SUM not calculating in powerBI correctly

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

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: