How do i bring userelationship in this calculation - powerbi

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

Related

DAX max date in dimension ignore filters on date

In my Power BI model I have a fact table (factSales) that links to a date dimension (dimDate) through a surrogate key DateId. Now I want to add a measure to obtain the max invoice date for each client. But it should be the maximum date ignoring the context (for the date filters). (So if I filter all sales in Q1 2020, then I still want the max invoice date in e.g. 2021).
This is how I got it working:
Add new column in factSales:
Invoice Date = RELATED(DimDate[Date])
Add new measure in factSales:
Last Contract =
CALCULATE( MAX(FactSales[Invoice Date]),
ALL( DimDate )
)
This works, but is there a better way to do this ? Without the extra calculated column. (And without using both-directional filtering).
You can use CROSSFILTER inside CALCULATE:
Last Contract =
CALCULATE (
MAX ( Dates[Date] ),
REMOVEFILTERS ( DimDate ),
CROSSFILTER ( Sales[Date], Dates[Date], BOTH )
)
You can create a measure to return max date using the following dax formula:
Measure = MAX(Sheet1[Date])
To always display the latest date without filter by slicer, you need to click on the slicer then goto Format >Edit Interaction >click none on the specific visual. In the following case, the max date is still 8 Nov 21 even though the slicer latest date is Sep 21
Try this:
Last Contract =
CALCULATE ( MAX ( DimDate[Date] ), ALL ( DimDate ), FactSales )
This removes filtering from a slicer on DimDate[Date] by still applies FactSales as a filter table.

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.

Distinctcount entrys with date slicer

this is probably pretty simple but I couldn't find any solution.
I have 2 tables: 'DateTime' and 'Usage' and I am using a date slicer (from to) which refers to 'DateTime'[Date]. Now I want to DISTINCTCOUNT 'Usage'[LPNumber] but only the ones which have a date that is included by the slicer. The table 'Usage' does also have a column with dates: 'Usage'[ConnectionStart Day]
I tried this but getting an error:
ActiveLP =
VAR start_date =
MIN ( 'DateTime'[Date] )
VAR end_date =
MAX ( 'DateTime'[Date] )
RETURN
CALCULATE(
DISTINCTCOUNT( 'Usage'[LPNumber] );
FILTER(
'Usage';
'Usage'[ConnectionStart Day] >= start_date
&& 'Usage'[ConnectionStart Day] <= end_date
)
)
The error says: A circular dependency was detected: DateTime[different columns]
Can someone please help me? Thank you very much :)
Just create a realtionship between your DateTime table and your Usage table (by the date). Then use this simple measure:
Distinct Count = Distinctcount('Usage'[LPNumber])
If you put now a slicer (from the DateTime table) on your report and filter it, the other table also get filtered, because of the relationship. Thus the value of Distinct Count will change, according to your date slicer.

How to calculate daily population in DAX

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

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