Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DAX Service recall rate measure

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

enter image description here

like image 761
jhowe Avatar asked Mar 05 '21 10:03

jhowe


People also ask

What are DAX measures?

Measures. A measure is a formula that is created specifically for use in a PivotTable (or PivotChart) that uses Power Pivot data. Measures can be based on standard aggregation functions, such as COUNT or SUM, or you can define your own formula by using DAX. A measure is used in the Values area of a PivotTable.

Can Earlier be used in DAX measure?

EARLIER is a DAXDAXData Analysis Expressions (DAX) is the native formula and query language for Microsoft PowerPivot, Power BI Desktop and SQL Server Analysis Services (SSAS) Tabular models.https://en.wikipedia.org › wiki › Data_Analysis_ExpressionsData Analysis Expressions - Wikipedia function that acts exclusively on row context, and its purpose is to create a reference to a column value on an outer loop of the evaluation of the expression. It is commonly used in calculated columns during nested row by row iterations. This is easy enough.

How does DAX calculate total sales?

A measure [Total Sales] in the same table as an expression. The Total Sales measure has the formula: =SUM(Sales[SalesAmount]).


2 Answers

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:

Matrix Visual

like image 75
Alexis Olson Avatar answered Oct 19 '22 10:10

Alexis Olson


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.

like image 1
Mike Honey Avatar answered Oct 19 '22 09:10

Mike Honey