I'm trying to calculate the daily population of patients at our center for any given date using the patient's booking date (start date) and their release date -- using DAX and Power Pivot. I'm planning on using it in PowerBI eventually.
I've tried the formula below. I'm not getting any errors, but when I go to create a pivot table/chart in excel, I'm not getting the correct output.
I'm only using two tables: 1) a main table with the patient data and 2) a date table (calendar) and have established a relationship between the two tables.
The booking table has Patient ID, Booking Date, Release Date columns. I'd like to be able to create a graph showing the total population by day taking both dates into consideration.
DailyPop :=
CALCULATE (
COUNTROWS ( Patients ),
FILTER (
Patients,
AND (
Patients[Booking Date] > MIN ( 'Calendar'[Date] ),
Patients[Release Date] < MAX ( 'Calendar'[Date] )
)
)
)
You can add an extra column on your date table:
PatientCount:= SUMX(Patients,IF(Patients[Booking Date]<='Calendar'[Date] && Patients[Release Date]>='Calendar'[Date],1,0))
Related
I am trying to calculate the number of working days between 2 date columns in a table called Incident Table and the 2 columns are called Created Date and Resolved Date. I also have a standard date table.
Right now, the relationship is one to many between date and Created date column in Incident table
First I created a column in date table that would give true or false as to whether a day was a weekday
Is Working Day = if('Date'[Day Name Short]="Sat",FALSE(),if('Date'[Day Name Short]="Sun",FALSE(),TRUE()))
Then in the incident table i used this
Time to Resolve (days) = COUNTROWS ( FILTER ( 'Date', AND ( AND ( 'Date'[Date] >= 'Incidents'[Created Date], 'Date'[Date] <= Incidents[Resolved Date] ), 'Date'[Is Working Day] ) ) )
This formula is fine on its own but i need to connect resolved date with the date table so i can use date filter for both created and resolved. Any ideas?
You would ideally create two relationship from the Dates table with Incident table, one active and one inactive and then in the measure you would activate the inactive relationship using USERELATIONSHIP.
Something =
CALCULATE (
DISTINCTCOUNT ( Incident[Time to Resolve (days)] ),
USERELATIONSHIP ( Incident[Resolved Date], Dates[Date] )
)
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.
I am having issues getting the month over month percentage in power bi. I have column that contains the yyyy-mm data in dim table and i have a percentage column in fact table. I dont see Time intelligence under Quick measure as well.
Percentage MoM% =
IF (
ISFILTERED ( 'output'[Period] ),
ERROR ( "Time intelligence quick measures can only be grouped or
filtered by the Power BI-provided date hierarchy or primary date column." ),
VAR __PREV_MONTH =
CALCULATE ( [Percentage], DATEADD ( 'output'[Period].[Date], -1, MONTH ) )
RETURN
DIVIDE ( [Percentage] - __PREV_MONTH, __PREV_MONTH )
)
Getting error that time intelligence needs date hierarchy but i dont have one. Is there any other way to achieve this?
Thanks
I have a table which contains a list of products scores by date:
From this table, I have to make a plot of the cumulative percentage of each quality by date.
At this moment I have the percentage of each class by day:
For that I used this measurement:
Measure =
CALCULATE (
SUM ( Table1[Percentage_By_Class] ),
FILTER ( Table1, Table1[Date] = MAX ( Table1[Date] ) ),
ALLEXCEPT ( Table1, Table1[Score] )
)
/ CALCULATE (
SUM ( Table1[Percentage_By_Class] ),
FILTER ( ALL ( Table1 ), Table1[Date] = MAX ( Table1[Date] ) )
)
But this only considers the percentage of each day. I need to consider all previous dates. E.G. for day 2 I need to consider days 1 and 2, for day 3 I need to consider days 1,2,3 and so on.
How can I accomplish this?
in my opinion, you need a calendar for the date first, you can create a table easily bay dax function =CALENDARAUTO() And mark it as a calendar table,
After that, you can use a DATEMTD or a DATEYTD function for your coding purpose.
here are the steps:
1 - https://learn.microsoft.com/en-us/dax/calendarauto-function-dax
select left pane --> table --> modelling / create table and add dax formula
2- reference the table as a date table, right click on the table from the right pane
after then you can use data functions like YTD MTD ,
new measure :
1st mesure : AVG1 = AVG(DATA_)
2nd measure : YTD AVG ALL = CALCULATE([AVG1];DATESYTD(CALENDAR[DATE]))
REF: https://learn.microsoft.com/en-us/dax/dateadd-function-dax
then you can use MONTH(CALENDAR(DATE)) on left and YTD AVG as a value at any table...
regards.
SUNAY
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