DAX Service recall rate measure - powerbi

I'm struggling to write/calculate this measure in DAX. The definition of recall rate is count of repeat service bookings (count of distinct booking number, should be distinct anyway but just in case) for a customer, asset combination within a week (Closed on date, 7 day period). So I go out to fix a machine, if I get called out again to fix the same machine for the customer within a week that is then a recall of 1 (or more if i get called out multiple times within a week). I've highlighted the groups in different colours. Null Assets, Closed On and null booking number needs to be filtered out (this is done by inner join in SQL in below code, needs to be in DAX query) Thanks! EDIT : Sorry realised it would be more helpful if I posted sql code to generate data please see below :
SELECT
FB.BookingNumber,
FB.EngineerEmployeeID,
FWO.ServiceAccountRecID AS Customer,
FWO.AssetRecID AS Asset,
FWO.ClosedOn
FROM dbo.FactWorkOrder AS FWO JOIN dbo.FactBooking AS FB ON FB.WorkOrderID = FWO.WorkOrderID
WHERE FWO.WorkOrderType = 'Breakdown'
AND AssetRecID IS NOT NULL
AND ClosedOn IS NOT NULL
ORDER BY BookingNumber

It's most efficient if you first define a calculated column that gives the first CloseOn date for each Customer/Asset combination.
FirstClosed =
CALCULATE (
MIN ( WorkOrder[ClosedOn] ),
ALLEXCEPT ( WorkOrder, WorkOrder[Customer], WorkOrder[Asset] )
)
and then write a measure
TotalRecalls =
COUNTROWS (
FILTER (
WorkOrder,
WorkOrder[ClosedOn] > WorkOrder[FirstClosed] &&
WorkOrder[ClosedOn] < WorkOrder[FirstClosed] + 7
)
)
However, you can do this all within a single measure if you prefer.
TotalRecalls =
VAR AddCol =
ADDCOLUMNS (
WorkOrder,
"#FirstClosed",
CALCULATE (
MIN ( WorkOrder[ClosedOn] ),
ALLEXCEPT ( WorkOrder, WorkOrder[Customer], WorkOrder[Asset] )
)
)
RETURN
COUNTROWS (
FILTER (
AddCol,
WorkOrder[ClosedOn] > [#FirstClosed] &&
WorkOrder[ClosedOn] < [#FirstClosed] + 7
)
)
Either way, here's what this looks like used in a visual:

I would first create a "Booking Key" column:
Booking Key = [Customer] & "|" & [Asset] & "|" & WEEKNUM ( [ClosedOn] )
Then I would create a Measure to return a modified distinct count on the Booking Key:
# Repeat Service Bookings =
VAR v_Count = DISTINCTCOUNT ( 'Table'[Booking Key] )
RETURN IF ( v_Count > 1, v_Count - 1 )
I would add # Repeat Service Bookings to the Visual Level Filters of your table visual, with the filter set to: is greater than 1.

Related

How to count dates in Power BI DAX

I have a table like below.
I want to show count of LicenseEndDate. Like In my slicer I have taken WeekEnding column suppose if I select weekending date like 09/25/2022 then I want show the count of LicenseEndDate dates that are ended between 09/18/2022 to 09/25/2022. How to create measure for this. I want to show that count in card visual. I created a measure like below
License_Expired_Count = COUNT(Trade[LicenseEndDate]).
But It giving me count of all. That mean the License which are expiring in the feature count also it was showing. But want to show count of license which are expired in the selected period. How create measure for this.
Please check this code, let me know If It works for you.
License_Expired_Count =
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
Trade[LicenseEndDate] <= SELECTEDVALUE ( Trade[WeekEndingDate] ),
Trade[LicenseEndDate]
> SELECTEDVALUE ( Trade[WeekEndingDate] ) - 7
)
try this :
License_Expired_Count =
VAR _slt =
SELECTEDVALUE ( Trade[WeekEndingDate] )
RETURN
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
ALLSELECTED ( Trade[WeekEndingDate] ),
DATESBETWEEN ( Trade[LicenseEndDate], _slt - 7, _slt )
)
check sample file attached

Power BI: Use unrelated date table to create a measure

Hi I have been struggling with this for a bit now and I hope I didn't miss a previous question. I am trying to get a count of the vehicles we have based on an EOMONTH date. We are buying and selling cars on a regular basis and for reporting we need to know how many we had at the end of each month and the report is a rolling 12 months.
I've tried creating the relationship with the purchasedate of the vehicle to the date of my date table but when I create the measure (Used to calculate the number of vehicles purchased but haven't been sold):
SalesBlank = CALCULATE(
COUNT(Vehicles[MVANumber]),
FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate])))
I only get a count of vehicles purchased that month and don't have a sale date - I'm not surprised because my relationship with the date table is the purchase date.
How can I set up a measure to look at the date table and filter the vehicles table with this logic:
purchasedate <= date[EOMONTH] && ISBLANK(salesdate)
Any help would be greatly appreciated!!
Thanks,
Matt
Sample Data and Desired Results
Relationships
If I understand you correctly, you want to get a count of the vehicles on hand at the end of each month. That could be calculated by counting the vehicles with a purchase date less than or equal to the selected end of month and subtracting the count of vehicles with a sale date less than or equal to the selected end of month.
You can create an active relationship between Vehicle[PurchaseDate] and Date[Date]. Then create an inactive relationship based upon Vehicles[SaleDate] and Date[Date].
You could use a measure that is something like this:
Inventory Count =
VAR MaxDate =
MAX ( 'Date'[Date] )
VAR MinDate =
CALCULATE ( MIN ( 'Date'[Date] ), ALL ( 'Date' ) )
VAR Purch =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR Sales =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
USERELATIONSHIP ( 'Date'[Date], Vehicles[Sale Date] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR MaxPurDt =
CALCULATE ( MAX ( 'Vehicles'[Purchase Date] ), ALL ( 'Vehicles' ) )
VAR MaxSlDt =
CALCULATE ( MAX ( 'Vehicles'[Sale Date] ), ALL ( 'Vehicles' ) )
RETURN
IF (
MIN ( 'Date'[Date] ) <= MaxPurDt
|| MIN ( 'Date'[Date] ) <= MaxSlDt,
Purch - Sales
)
This measure gets a cumulative count of purchases and a cumulative count of sales and then subtracts them. The IF check is to avoid propagation of cumulative totals beyond the maximum date in the Vehicle table.
I'm not sure how to interpret your showing of just 3 months in the desired results. This will produce the same answers as what you have, but without a filter applied to the table, it starts at 3/31/2016 (the date of the first sale).
Edit: There's probably a more efficient way along the lines you were thinking, but it is escaping me at the moment.

DAX alternative for IN function

I have a table in SSAS Tabular model having delivery date of vehicles.
There is a dimension table with Age values -
I need to write a DAX to show the count of delivery for each year.
In the PowerBI report which connects to the Tabular model, the user can select the Age (multiple) from the filter which is on Dim_Age. So if the user selects Age 1 it should show the count of 2019 deliveries for the year 2020.
Now the challenge is I am working on SSAS 2016 build version 13.0.5426.0. This version does not support "IN" function. I have tried below and this does not work.
VAR selecteyear =
IF ( HASONEVALUE ( Dim_Cal[year] ); VALUES ( Dim_cal[year] ) )
CALCULATE (
COUNT ( Delivery_Data[Vechile] );
( selectedyear - Delivery_Data[DeliveryYear] )
IN VALUES ( Dim_Age[Age] )
)
Please help to know the alternate solution.
In general, the CONTAINS function is the workaround in older versions that don't support IN.
Here's how you could use it in you situation:
AgedCount =
VAR selectyear =
IF ( HASONEVALUE ( Dim_Cal[year] ), VALUES ( Dim_Cal[year] ) )
RETURN
CALCULATE (
COUNT ( Delivery_Data[Vechile] ),
FILTER (
ALL ( Dim_Cal ),
CONTAINS ( Dim_Age, Dim_Age[Age], selectyear - Dim_Cal[year] )
)
)
This iterates through the whole Dim_Cal table and for each row, checks if selectyear - Dim_Cal[year] is one of the values selected from the Dim_Age[Age] filter.

Daily Table out of Start and End Date

I'm trying to create a calculated table with daily information of employees based on MIN and MAX dates. I'm on Power BI.
This won't be super-efficient if you have too many employees and dates, but you can take the cartesian product of the list of employees with the list of all possible days and filter that down:
CalculatedTable =
VAR CartesianProduct =
CROSSJOIN (
VALUES ( Main[employee_number] ),
CALENDAR ( MIN ( Main[start] ), MAX ( Main[end] ) )
)
RETURN
FILTER (
CartesianProduct,
[Date] >= LOOKUPVALUE ( Main[start], Main[employee_number], [employee_number] ) &&
[Date] <= LOOKUPVALUE ( Main[end], Main[employee_number], [employee_number] )
)
The CALENDAR function generates a table with column Date starting from the first argument and includes all days up to the second argument.
I'm not sure how to do this more efficiently in DAX, but I can think of a cleaner solution in M (query editor language).

How to work out Attrition for Staff members with a Start & End Date in Power BI?

I have a table with Staff data with columns DateEmployed & TerminationDate.
I would like to work out the number of people that started & left (which I used a count formula) as well as the net growth for all date periods.
The formula would count each DateEmployed as 1 & if an individual does not have a Termination Date then it would not count it.
e.g. 4 people starts in June 2016 & 2 leaves in June 2016 giving me a net value of 2.
The issue arises further as I need a date dimension to view them for each month for each year.
I would like to display all 3 dimensions of data in one graph as well.
The data should read like the bar graphs consolidated:
enter image description here
The best practice in every case is to create a calendar/date table. Fortunately calendar tables are easy to create in Power BI. With an expression you can create it.
In Power BI / Modeling tab / New Table icon, you can create a custom date table using this expression:
DateTable = CALENDAR (DATE(2016,1,1), DATE(2016,12,31))
This expression generates dates from 2016-1-1 to 2016-12-31 so if your data expands to a wider range you have to modify it to suit to your requeriment.
Create these measures:
Starting: Measure counting the people starting in each month or year.
Starting =
CALCULATE (
COUNT ( 'Table'[FullName] ),
FILTER (
ALL ( 'Table' ),
COUNTROWS (
FILTER (
'Table',
NOT ISBLANK ( [DateEmployed] )
&& MONTH ( EARLIER ( 'Table'[DateEmployed] ) ) = MONTH ( MAX ( DateTable[Date] ) )
)
)
)
)
Ending: Measure for counting the people ending in each month or year.
Ending =
CALCULATE (
COUNT ( 'Table'[FullName] ),
FILTER (
ALL ( 'Table' ),
COUNTROWS (
FILTER (
'Table',
NOT ISBLANK ( [TerminationDate] )
&& MONTH ( EARLIER ( 'Table'[TerminationDate] ) )
= MONTH ( MAX ( DateTable[Date] ) )
)
)
)
)
Net: Total for each month:
Total = ABS([Starting] - [Ending])
Now in a bar chart or any visualization set the measures something like this: