Working with Duration (Availability) in PowerBi - powerbi

I'm trying to create a line graph in PowerBI. What I am trying to plot is somewhat complex.
I have the following tables:
Staffing - this table describes staffing for every employee in the company. By "staffing" I'm referring to how their time is allocated. For example, Employee #7 is staffed in "Chicken Manufacturing" with a StartDate of 1/1/2016 and an EndDate of 1/10/2016
EmployeeID Project StartDate EndDate
5 Cutting Lemons 12/1/2015 12/31/2015
5 Chicken Manufacturing 1/1/2016 1/10/2016
6 Fishing Lobsters 1/2/2016 1/5/2016
7 Chicken Manufacturing 1/5/2016 2/1/2016
8 Drinking 2/1/2016 null
I also have a standard Date dimension as well as an Employees table and a Project table. The Employees table has a row for every employee and the Project has a row for each activity.
I am trying to create a line graph that has dates on the x-axis and the line will show me how many employees are active at the given date. So for dates 12/1/2015 - 1/10/2016 Employee 5 should be counted as "Staffed" but on 1/11/2016, he should not be included in the total.
What I'm actually trying to do is calculate Availability and by that I mean, how many Employee hours are available on each day (I have a project called Available) so ultimately I will want to count the number of Hours rather than employees, but I think if I can get to work with counting employees, I shouldn't have too much trouble multiplying number of employees by 8 hours per day.

Try something like this:
Count of Emp =
CALCULATE (
DISTINCTCOUNT ( Employee[EmployeeID] ),
FILTER (
Staffing,
[StartDate] <= MAX ( 'Date'[Date] )
&& (
[EndDate] >= MAX ( 'Date'[Date] )
|| ISBLANK ( [EndDate] )
)
)
)
It is not tested but should work as long as you have relatinship between Employee - Staffing. Also be sure to use your date column in the Axis setting.
Let me know if this helps.

Related

Why do i get the avrage of the subcategory and not the avrage of category per year?

When i drag in a tabel
and just have
-YEAR
-Kategory
-Summa
I will get the medium value of 895,50 (815 + 976 / 2) for february
I don't want it to separeate the Subcategory
I want it to show
1791
when i just mark february
and if i use February and March
It sould say
2022 Inköp 2435,5
Becouse that the Medium of February and Marsh on the category
It's difficult to understand what you are asking (likely the reason for your downvotes). I think you are after a monthly average measure. Since your sample data is extremely limited I assume that each month will have exactly one date. Ideally this type of calculation should be handled by using a calendar table as a basis for month values, but for your sample data, try this:
Monthly average =
AVERAGEX (
VALUES ( 'Table'[Datum] ) ,
CALCULATE ( SUM ( 'Table'[Summa] ) )
)

Counting a conditional flag within a group over a subgroup in Power BI

I am looking for help in calculating a total by month, that counts how many applications met an aggregated criteria within that month.
A simplified version of the data is that we receive applications, and complete a number of tasks to process that application. Each task has a duration, and adding all the tasks for an application gives the total processing time for the application. We then want to know how many applications were processed in 10 days or less.
Note - the tasks to sum can vary, so we want to calculate the application total duration at the DAX layer, rather than in PowerQuery.
Example raw data:
Application
Task
Period
Duration
A
2859
Aug-22
2
A
2860
Aug-22
2
A
2861
Aug-22
1
B
1990
Aug-22
1
B
1991
Aug-22
20
C
9940
Sep-22
0
C
9941
Sep-22
27
D
1891
Sep-22
1
D
1892
Sep-22
8
E
3697
Sep-22
-
E
3698
Sep-22
26
The calculation condition would then look like this (we don't need to create this view, I'm just including it to explain the logic):
The actual output we're looking to create is a total by month (based on when application was received):
I've got a version of this working by creating a summary table in PowerQuery that makes a unique list of Application and Period combinations, and then links back to the main table to sum the duration for each, but I feel like it should be possible to do this using a DAX formula - as much for my own learning as anything else. Any suggestions on a) how to do it directly in DAX and b) whether this is sensible would be greatly appreciated!
You'll have to adjust the table and column names, but you can accomplish this logic with the following measures. The variable applicationsLessThan10 is a virtual table by Month and application and total duration for each. This is then filtered accordingly and we return the number of rows.
I split out month name into a new column with powerquery - ideally, you would have a date table which would account for year and month and the sorting of the month name.
LessThan10Days =
VAR applicationsLessThan10 =
FILTER (
ADDCOLUMNS (
SUMMARIZE (
ApplicationProcessing,
ApplicationProcessing[Month Name],
ApplicationProcessing[Application]
),
"Days", CALCULATE ( SUM ( ApplicationProcessing[Duration] ) )
),
[Days] <= 10
)
RETURN
COUNTROWS ( applicationsLessThan10 )
MoreThan10Days =
VAR applicationsMoreThan10 =
FILTER (
ADDCOLUMNS (
SUMMARIZE (
ApplicationProcessing,
ApplicationProcessing[Month Name],
ApplicationProcessing[Application]
),
"Days", CALCULATE ( SUM ( ApplicationProcessing[Duration] ) )
),
[Days] > 10
)
RETURN
COUNTROWS ( applicationsMoreThan10 )

Rolling 6 month Open Contracts in Power BI

I need to show how many active contracts we have open for each month in the last 6 months. I am trying to figure out a way to display this. Here is my table
Machine Enrollment# StartDate EndDate
A 1 1/2/2016 6/18/2019
B 2 12/15/2012 5/12/2034
C 3 3/25/2019 4/25/2021
D 4 1/7/2000 7/15/2019
A 5 10/1/2019 10/1/2025
I have thousands of rows. I want to be able to show a rolling 6 month visual for how many machines are under contract. So in this small example it would look like this
Apr-19 June-19 Jul-19 Aug-19 Sep-19 Oct-19
4 4 3 2 2 3
Where do I even begin in creating this? In the past, we have just looked at the numbers for the current month and tacked those results onto the end of a static table and deleted the column from over 6 months ago. I have been assigned to automate this report in Power BI. I am guessing I need to create a column/measure that looks at the EndDate and compares it to the filtered Date in the visual (ie: Aug-19) and determines if the contract was open at that time. But I do not know. Any help is much appreciated. Thanks in advance!
I think I found a solution for what you are looking for. You may find a sample pbix file here.
1. Create a calendar table
A calendar table is required to filter/slice the time periods. The calendar table needs to have a unique Date column, and optional columns such as Year, Quarter, and Month, depending on what units of period you need in the analysis.
A calendar table can be most easily created as a DAX calculated table. Here is an example of a minimal calendar table required in this use case.
Calendar =
ADDCOLUMNS(
CALENDAR( MIN( Contracts[StartDate] ), MAX( Contracts[EndDate] ) ),
"Year Month", FORMAT( [Date], "mmm-yy" ),
"Year Month Number", YEAR( [Date] ) * 100 + MONTH( [Date] )
)
2. Create a measure to calculate number of open contracts
Every numbers calculated and shown in reports need to be defined as measures.
Let's think about the number of May-19. The current filter context includes all 31 dates in the Calendar table between 2019-05-01 and 2019-05-31. In this case, how can we think of an open contract? If the contract starts after 2019-05-31, it is not open. If the contract ends before 2019-05-01, it is not open as well. Therefore the open contract meets this condition.
Starts on or before 2019-05-31 and
Ends on or after 2019-05-01
Below is the measure definition to count the number of contracts based on this condition.
# Open Contracts =
VAR MinDate = MIN( 'Calendar'[Date] )
VAR MaxDate = MAX( 'Calendar'[Date] )
RETURN COUNTROWS(
FILTER(
Contracts,
Contracts[StartDate] <= MaxDate
&& Contracts[EndDate] >= MinDate
)
)
3. Add dynamic filter for last 6 months
If I was understanding correctly, the requirement is to show monthly number of last 6 calendar months, excluding this month. I could not find a straightforward way for this. My solution may contain a bit of hacky scent.
Power BI does not have built-in filter support based on calendar months relative to now. We need to build a custom logic to achieve this. I did it by creating a measure that indicates whether current filter context is within the desired period. This measure is a flag that returns 1 if the filter context is a single calendar month which is included in the last 6 calendar months, or returns BLANK otherwise.
__Last6MonthFlag =
VAR YearMonths = CALCULATETABLE(
VALUES( 'Calendar'[Year Month] ),
REMOVEFILTERS( 'Calendar'[Year Month] ),
'Calendar'[Date] > EOMONTH( TODAY(), -7 )
&& 'Calendar'[Date] <= EOMONTH( TODAY(), -1 )
)
RETURN IF(
HASONEVALUE( 'Calendar'[Year Month] )
&& SELECTEDVALUE( 'Calendar'[Year Month] ) IN YearMonths,
1
)
Then I used this measure in the visual filter like this.
You need to do below activities to achieve your requirement.
Define a calendar table holding all the dates for your report. Define a calculated column for Month-Year. Month-Year = FORMAT('CalendarTable'[Date], "MM-YYYY")
You can define a calculated measure, which will return 1 if the end date of the contract is < 6 months from current date (you can use TODAY() function). This function will help in rolling calculation based on current date. Otherwise, this calculated measure will return NULL and SUM them.
You can drag the month-Year calculated column, defined in step no. 1, to the column axis. You can drag the calculated measure, defined in step no.2, to the values section. PowerBI control by default filters out the NULL values. So, when you use the calculated measure defined in step no. 2, you will get values only for the last 6 months. As the calculation is defined based on TODAY(), it will be a running calculation.

Daily hospital census: Summarize a table filtered for each date

I'm trying to count how many patients were in the hospital from one day to another and in which clinical department and specialty.
There is one table 'Patient Flow' with all patient's path during his hospitalization.
I was able to calculate this measure with another table 'Patient Entry-Out Date', where I filtered (ENTRY DATES < DATE REFERENCE) and (OUT DATES >= DATE REFERENCE or OUT DATES = null).
However, this way I can't identify what was the correct clinical department and specialty of this patient.
For example: the patient
03/13/2019 - shouldn't be counted
03/14/2019 - should be counted in clinical: UNIDADE CORONARIANA and specialty: CARDIOLOGIA
03/15/2019 - should be counted in clinical: UNIDADE CORONARIANA and specialty: CARDIOLOGIA
03/16/2019 - should be counted in clinical: CIRÚRGICA III and specialty: CARDIOLOGIA
03/17/2019 - should be counted in clinical: CIRÚRGICA III and specialty: CARDIOLOGIA
03/18/2019 - shouldn't be counted
I tried to create an index and get the maximum value between Admissions and Entry Transfer (table Last entry), but couldn't do it for each day.
Is it possible to do a summary in a filtered table(admissions and entries before) for each reference date ?
Or another solution?
Thanks in advance!
Pbix file https://www.dropbox.com/s/bqhyj4gihb5g4la/Daily%20hospital%20census.pbix?dl=0
Cássio A. Andrade 
Here is a solution I was able to create. (working pbix can be downloaded from here)
1. Transformation
I used PowerQuery to transform original "Patient Flow" table into a new table (named "Patient Attendance") like below.
In this transformed table, each row represents a patient's attendance with a clinical department. A patient may enter a clinical department for an occurrence of either "Admission" or "Enter transfer", and may exit the department for either "Output transfer", "Medical release", or "Death". Each row has two time stamps; when the patient entered and exited the department.
Note, patients may be still staying with the department at the time when data is collected. In this case, exit time stamp for that patient is blank.
2. Modeling
I created a data model, which is a star schema composed of one fact table PatientAttendance illustrated above, and 3 dimension tables Clinics, Specialties, and Patients. Additionally, we have the Calendar dimension table, which does not have any physical relationship to other tables, but is used for measure calculation and slicing.
Then, I created the measure on top of this model which calculate how many patients were staying with the clinical department on any given day or period. The basic concept of the calculation logic is,
Look at each individual patient.
Look at each calendar date.
If the patient is staying with any department on the given day, add 1 count.
Patient-Days Stayed =
SUMX(
'Patients',
SUMX(
'Calendar',
IF(
CALCULATE(
COUNTROWS(
FILTER(
'PatientAttendance',
VAR CurrentDate = VALUES( 'Calendar'[Date] )
RETURN
'PatientAttendance'[Enter Timestamp].[Date] <= CurrentDate
&& (
ISBLANK( 'PatientAttendance'[Exit Timestamp] )
|| (
NOT( ISBLANK( 'PatientAttendance'[Exit Timestamp] ) )
&& 'PatientAttendance'[Exit Timestamp].[Date] > CurrentDate
)
)
)
)
) > 0, 1
)
)
)
The result looks to be working correctly (ignore too small numbers in early January, this is due to the sampling method of source data). If I am making any error, or if there is any suggestion of better approach, I am very interested to hear.

DAX measure: project duration (days) from dimension starting & ending date

I have following scenario which has been simplified a little:
Costs fact table:
date, project_key, costs €
Project dimension:
project_key, name, starting date, ending date
Date dimension:
date, years, months, weeks, etc
I would need to create a measure which would tell project duration of days using starting and ending dates from project dimension. The first challenge is that there isn't transactions for all days in the fact table. Project starting date might be 1st of January but first cost transaction is on fact table like 15th on January. So we still need to calculate the days between starting and ending date if on filter context.
So the second challenge is the filter context. User might want to view only February. So it project starting date is 1.6.2016 and ending date is 1.11.2016 and user wants to view only September it should display only 30 days.
The third challenge is to view days for multiple projects. So if user selects only single day it should view count for all of the projects in progress.
I'm thankful for any help which could lead towards the solution. So don't hesitate to ask more details if needed.
edit: Here is a picture to explain this better:
Update 7.2.2017
Still trying to create a single measure for this solution. Measure which user could use with only dates, projects or as it is. Separate calculated column for ongoing project counts per day would be easy solution but it would only filter by date table.
Update 9.2.2017
Thank you all for your efforts. As an end result I'm confident that calculations not based on fact table are quite tricky. For this specific case I ended up doing new table with CROSS JOIN on dates and project ids to fulfill all requirements. One option also was to add starting and ending dates as own lines to fact table with zero costs. The real solution also have more dimensions we need to take into consideration.
To get the expected result you have to create a calculated column and a measure, the calculated column lets count the number of projects in dates where projects were executed and the measure to count the number of days elapsed from [starting_date] and [ending_date] in each project taking in account filters.
The calculated column have to be created in the dim_date table using this expression:
Count of Projects =
SUMX (
FILTER (
project_dim,
[starting_date] <= EARLIER ( date_dim[date] )
&& [ending_date] >= EARLIER ( date_dim[date] )
),
1
)
The measure should be created in the project_dim table using this expression:
Duration (Days) =
DATEDIFF (
MAX ( MIN ( [starting_date] ), MIN ( date_dim[date] ) ),
MIN ( MAX ( [ending_date] ), MAX ( date_dim[date] ) ),
DAY
)
+ 1
The result you will get is something like this:
And this if you filter the week using an slicer or a filter on dim_date table
Update
Support for SSAS 2014 - DATEDIFF() is available in SSAS 2016.
First of all, it is important you realize you are measuring two different things but you want only one measure visible to your users. In the first Expected result you want to get the number of projects running in each date while in the Expected results 2 and 3 (in the OP) you want the days elapsed in each project taking in account filters on date_dim.
You can create a measure to wrap both measures in one and use HASONEFILTER to determine the context where each measure should run. Before continue with the wrapping measure check the below measure that replaces the measure posted above using DATEDIFF function which doesn't work in your environment.
After creating the previous calculated column that is required to determine the number of projects in each date, create a measure called Duration Measure, this measure won't be used by your users but lets us calculate the final measure.
Duration Measure = SUMX(FILTER (
date_dim,
date_dim[date] >= MIN ( project_dim[starting_date] )
&& date_dim[date] <= MAX ( project_dim[ending_date] )
),1
)
Now the final measure which your users should interact can be written like this:
Duration (Days) =
IF (
HASONEFILTER ( date_dim[date] ),
SUM ( date_dim[Count of Projects] ),
[Duration Measure]
)
This measure will determine the context and will return the right measure for the given context. So you can add the same measure for both tables and it will return the desired result.
Despite this solution is demonstrated in Power BI it works in Power Pivot too.
First I would create 2 relationships:
project_dim[project_key] => costs_fact[project_key]
date_dim[date] => costs_fact[date]
The Costs measure would be just: SUM ( costs_fact[costs] )
The Duration (days) measure needs a CALCULATE to change the filter context on the Date dimension. This is effectively calculating a relationship between project_dim and date_dim on the fly, based on the selected rows from both tables.
Duration (days) =
CALCULATE (
COUNTROWS ( date_dim ),
FILTER (
date_dim,
date_dim[date] >= MIN ( project_dim[starting_date] )
&& date_dim[date] <= MAX ( project_dim[ending_date] )
)
)
I suggest you to separate the measure Duration (days) into different calculated column/measure as they don't actually have the same meaning under different contexts.
First of all, create a one-to-many relationship between dates/costs and projects/costs. (Note the single cross filter direction or the filter context will be wrongly applied during calculation)
For the Expected result 1, I've created a calculated column in the date dimension called Project (days). It counts how many projects are in progress for a given day.
Project (days) =
COUNTROWS(
FILTER(
projects,
dates[date] >= projects[starting_date] &&
dates[date] <= projects[ending_date]
)
)
P.S. If you want to have aggregated results on weekly/monthly basis, you can further create a measure and aggregate Project (days).
For Expected result 2 and 3, the measure Duration (days) is as follows:
Duration (days) =
COUNTROWS(
FILTER(
dates,
dates[date] >= FIRSTDATE(projects[starting_date]) &&
dates[date] <= FIRSTDATE(projects[ending_date])
)
)
The result will be as expected: