PowerBI - Rolling Monthly OTD Calculated Column - powerbi

I am pretty new to PowerBI and I am trying to calculate a rolling "Monthly On-Time Delivery" calculated column.
ERROR IMAGE
ADDED DAILY SHIPMENT COUNT (Disregard the incorrect MTD Shipment Count title)
Each row represents a single day in the Matrix
For each day I want to get the MonthShippedTotal / (MonthShippedTotal-MonthPastDue), this should give me my rolling on time delivery percentage.
This is what I have so far but the results are way off:
> MonthlyOTD = VAR RowDate = opr_shipments[SHIP_DATE] RETURN
> CALCULATE (
> DIVIDE((COUNT(opr_shipments[CREATE_TIMESTAMP])-COUNTAX(opr_shipments,opr_shipments[PAST_DUE]="1")),COUNT(opr_shipments[CREATE_TIMESTAMP]),0),
> FILTER (
> opr_shipments,
> opr_shipments[SHIP_DATE] <= RowDate
> && MONTH ( opr_shipments[SHIP_DATE] ) = MONTH ( RowDate ) && YEAR ( opr_shipments[SHIP_DATE] ) = YEAR ( RowDate )
> )
> )
MTD Shipments Calc:
MTD Shipments =
VAR RowDate = opr_shipments[SHIP_DATE]
RETURN
CALCULATE (
SUM ( opr_shipments[Daily Shipments] ),
FILTER (
opr_shipments,
opr_shipments[SHIP_DATE] <= RowDate
&& MONTH ( opr_shipments[SHIP_DATE] ) = MONTH ( RowDate ) && YEAR ( opr_shipments[SHIP_DATE] ) = YEAR ( RowDate )
)
)
Past Due Total Calc:
PastDueTotal =
VAR RowDate = opr_shipments[SHIP_DATE]
RETURN
CALCULATE (
SUM(opr_shipments[Daily Shipments]),
FILTER (
opr_shipments,
opr_shipments[SHIP_DATE] <= RowDate
&& DAY ( opr_shipments[SHIP_DATE] ) = DAY ( RowDate ) && opr_shipments[PAST_DUE] = "1"
)
)
For each month this calc would start over.
My desired output is: Jan 1 | 98% Jan 2 | 98.3% Jan 3 | 95% ... Jan 31
| 94.5% Feb 1 | 100%

I think something like this could work for the MonthlyOTD calculated column.
MonthlyOTD =
VAR RowDate = opr_shipments[SHIP_DATE]
RETURN
SUMX (
FILTER (
opr_shipments,
[SHIP_DATE] <= RowDate
&& MONTH ( [SHIP_DATE] ) = MONTH ( RowDate )
&& YEAR ( opr_shipments[SHIP_DATE] ) = YEAR ( RowDate )
),
DIVIDE ( [MTD Shipments], [MTD Shipments] - [PastDueTotal] )
)
I'd create those calculations as measures instead of calculated columns, note measures can be calculated in any context and are affected by filters which usually is desired.
UPDATE: It seems OP doesn't require a monthly cumulative total so the final expression was:
MonthlyOTD = DIVIDE ( ([MTD Shipment Count] - [Lines]), [MTD Shipment Count] )

Related

Rank non-contiguous dates with a monthly reset

With reference to the below sample data, I have a table in Power BI that currently only contains the Bus_Day column. I am attempting to add the MTD column (in red) that counts the monthly cumulative number of Bus_Day. The rows highlighted in yellow are reflective of the requirement that the MTD Day column should reset to 1 for the first Bus_Day per month.
My challenge is the Bus_Day column does not contain contiguous dates, so using DATEDIFF is not an option.
For this type of problem where you want to label non-contiguous dates, RANKX is your go-to unless you can solve it further upstream in your stack.
Here is a DAX column solution that adds a calculated column according to your specifications:
MTD Day =
VAR _yr = YEAR ( [Bus_Day] )
VAR _mth = MONTH ( 'Table'[Bus_Day] )
RETURN
RANKX (
FILTER (
'Table' ,
YEAR ( [Bus_Day] ) = _yr
&& MONTH ( [Bus_Day] ) = _mth
),
[Bus_Day] , , ASC
)
You can also calculate this using a measure, this formula does the trick with your sample data:
MTD Day Measure =
VAR _day = SELECTEDVALUE ( 'Table'[Bus_Day] )
VAR _tbl =
FILTER (
ALL ( 'Table' ) ,
YEAR ( 'Table'[Bus_Day] ) = YEAR ( _day )
&& MONTH ( 'Table'[Bus_Day] ) = MONTH ( _day )
)
RETURN
RANKX (
_tbl,
CALCULATE ( SELECTEDVALUE ( 'Table'[Bus_Day] ) ) , , ASC
)
Result:

Show cumulative SalesAmount column chart to max date in slicer

I am looking to build a column chart in Power BI that displays cumulative sales by month up to max date selected in date slicer. I have a supplementary Date2 table that is related to Date table (inactive relation). Tried something like this, but I keep getting all the months irrespective which one is selected in slicer:
Sales to date selected =
VAR ReferenceDate = MAX ( 'Date'[Date] )
Return
CALCULATE ( [SalesAmount],
ALLEXCEPT('Date', 'Date'[Year]),
FILTER( all(Date2[Date]), Date2[Date] <= MAX( Date2[Date] )
&& Date2[Date] <= ReferenceDate),
USERELATIONSHIP ( 'Date'[Date], Date2[Date] )
)
This is what I get as a result (Slicer selects month 7):
It looks like CALCULATE does not honor filter Date2[Date] <= ReferenceDate . What am I doing wrong here?
OK, looks like this scenario solves my issue. I removed inactive relationship between Date and Date2 and introduced active relationship from Sales to Date2.
Sales to date selected =
VAR ReferenceDate = MAX ( 'Date'[Date] )
Return
IF( MAX(Date2[Date]) <= ReferenceDate,
CALCULATE ( [SalesAmount],
ALLEXCEPT('Date', 'Date'[Year]),
Date2[Date] <= MAX( Date2[Date] )
)
)
I made a correction to your code; but It seems that It takes a long time for it to be approved by community members:
So test this:
Sales to date selected =
VAR ReferenceDate =
MAX ( ALLSELECTED ( 'Date2'[Date] ) )
RETURN
CALCULATE (
[SalesAmount],
FILTER ( ALLEXCEPT ( 'Date2', 'Date2'[Year] ), 'Date2'[Date] <= ReferenceDate )
)
Or This:
Sales to date selected =
VAR ReferenceDate =
MAX ( ALLSELECTED ( 'Date2'[Date] ) )
RETURN
CALCULATE (
[SalesAmount],
FILTER ( ALL ( 'Date2'[Date] ), 'Date2'[Date] <= ReferenceDate )
)

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

Power BI calculate Measure for previous working day

in Power BI I need to write a measure, which returns yesterdays Sales Amount. I can use PREVIOUSDAY() for Tuesday to Friday. However, for Monday I would like to return Friday's Sales Amount. I know I can do this with DATEADD(Calendar[Dates], -3, DAX).
My problem is that I cannot seem to get the two conditions working together. Is it even possible to do something like this with a measure?:
Yesterdays Sales =
Var TueToFri = CALCULATE([Sales Amount], PREVIOUSDAY(Calendar[Date])
Var Mon = CALCULATE([Sales Amount], DATEADD(Calendar[Date], -3, DAY)
IF WEEKDAY(Calendar[Date]) = 1
Return Mon,
Return TueToFri
If not, do you have any other ideas?
Cheers
This solution works for me:
Sales Amount PD =
IF (
WEEKDAY ( SELECTEDVALUE ( Calendar[Date] ) ) = 2;
//Monday
CALCULATE (
[Sales Amount];
DATEADD ( Calendar[Date]; -3; DAY )
);
IF (
WEEKDAY ( SELECTEDVALUE ( Calendar[Date] ) ) = 1;
//Sunday;
BLANK ();
IF (
WEEKDAY ( SELECTEDVALUE ( Calendar[Date] ) ) = 7;
//Saturday;
BLANK ();
CALCULATE (
[Sales Amount];
PREVIOUSDAY ( Calendar[Date] )
)
)
)
)
I believe the problem was caused by the calendar table returning multiple values. By using SELECTEDVALUE() only one date is returned, which can be used to do the comparision required.
I haven't tested it, but this should work:
Yesterday Sales =
VAR Mon = CALCULATE([Sales Amount], DATEADD(Calendar[Date], -3, DAY)
VAR TueToFri = CALCULATE([Sales Amount], PREVIOUSDAY(Calendar[Date])
VAR SatSun = BLANK()
RETURN
IF(
WEEKDAY(SELECTEDVALUE(Calendar[Date]), 2) = 1;
Mon;
IF(
WEEKDAY(SELECTEDVALUE(Calendar[Date]), 2) >= 6;
SatSun;
TueToFri
)
)

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: