Daily Table out of Start and End Date - powerbi

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

Related

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

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.

The last day of month from the last date with sales

How can I get the last day of month from the last date with sales? I want the result to be returned as a DAX calculated table.
The first step, calculated table:
LastDate = LASTNONBLANK( T[Date], CALCULATE( SUM( T[Amount] ) ) )
It returns a table (one column, one row) with the last date with sales. So far, correct, as expected. Say the last day with sales is 2020-04-15. But I want the end of month so 2020-04-30.
I hoped this should work, but it doesn't.
LastDate =
ENDOFMONTH (
LASTNONBLANK (
T[Date],
CALCULATE ( SUM ( T[Amount] ) )
)
)
It still returns 2020-04-15, instead of expected 2020-04-30.
The problem is caused by using dates from table T. Instead, you need to use dates from the calendar table, because list of dates for the time intelligence functions must be continuous.
Assuming you have a properly designed and connected calendar table 'Date' in your data model, change your DAX to:
LastDate =
ENDOFMONTH (
LASTNONBLANK (
Date[Date],
CALCULATE ( SUM ( T[Amount] ) )
)
)
ENDOFMONTH will use now a different table (Date), because LASTNONBLANK function will keep data lineage to it.

Cumulative total by group in Power BI (DAX)

After googling for two pages, I'm struggling to find a simple way to create a cumulative sum measure by date and item in Power BI (using DAX). I have a table which contains:
Username
Date (DD-MM-YYYY)
Number of requests in that day
I have managed to obtain the cumulative sum by using the following expression (extracted from DAXPatterns):
CALCULATE (
SUM ( Table[Requests] ),
FILTER (
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX ( 'Date'[Date] )
)
)
But I would like to obtain a measure indicating how many requests have been made by a user up to a certain date.
Is there a simple way to do this?
Create calculated table using SUMMARIZECOLUMNS function and apply filter that you need on the top of that calculated table.
YourCalcTableName =
SUMMARIZECOLUMNS (
'UsernameTable'[Username],
'Date'[Date],
"Number Of Requests", SUMX ( 'UsernameTable', 'UsernameTable'[NumberOfRequests] )
)
This calculated table essencialy produces 3 column table with user name, date and number of requests sent by this user on this date.
SUMMARIZECOLUMNS

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: