Distinctcount entrys with date slicer - powerbi

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.

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.

How do i bring userelationship in this calculation

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

DAX Grouping Evaluation

In this measure - I'm creating a temp table to group by customer ID and return the year of the min order_date for each customer. I then want to count the # of customers that show up for a given year (basically just a row count).
What I'm struggling to understand is - this formula doesn't seem to look at the SUMMARIZE table within it. If I put year and this measure in a matrix, it counts based on the raw table, not the grouped version built by SUMMARIZE in the formula. Any ideas why it's evaluating this way?
COUNTROWS(
SUMMARIZE(
Orders,
Orders[Customer ID],
"min_order_date_year",
MIN(Orders[Order Date].[Year])))
The formula is OK as per logic you provided to it. You have to clear about both your requirement and what your are writing in your code. Your formula is returning exactly what you are instructing it.
If I understand correct, you simply need the Customer count for the Minimum Year. For say if you have 6 unique customer for Year 2019 and 11 unique customer for Year 2020, you are looking here for value 6 to return by your measure.
Now, what your summarize table actually returning? if you create a separate custom table for the Summarize code only as below, you can see the table will actually hold all your customer name/id in first column and the second column will hole the MIN year available for that customer.
orders_summarize =
SUMMARIZE(
Orders,
Orders[customer id],
"min_order_date_year",MIN(Orders[Order Date].[Year])
)
So basically you have list of all customer in your summarize table. And now your are counting rows of your summarize table which is actually returning the total number of unique customers.
Finally, if you wants customer count for a specific Year (like MIN year), follow these below steps-
Step-1: Create a custom summarized table as below-
store_summarized_table =
SUMMARIZE(
store,
store[Customer ID],
"mindate",MIN(store[Order Date])
)
Step-2: create a measure as-
count_cust_id = COUNT('store_summarized_table'[Customer ID])
Step-3: Now configure your Matrix visuals as shown in the below image. You can also get the output in the image-
To avoid the Physical Table, you can do this below-
Step-1: Create a Custom Column as below-
is_min_year =
// -- keep current row's customer id to a variable
VAR current_cust_id = store[Customer ID]
// -- keep current row's YEAR value to a variable
VAR current_year = store[Order Date].[Year]
// -- find the MIN YEAR from order date for the current row customer id
VAR min_year_current_custommer_id =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
store,
store[Customer ID] = current_cust_id
)
)
// -- check the current row's year is the MIN year of order date for the customer as well or not.
RETURN IF(current_year = min_year_current_custommer_id, 1,0)
OR create a measure as-
is_min_year_measure =
VAR min_order_year_for_current_customer =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
ALL(store),
store[Customer ID] = MIN(store[Customer ID])
)
)
RETURN
IF ( MIN(store[Order Date].[Year]) = min_order_year_for_current_customer, 1,0)
Step-2: Create a Measure as below-
For created Custom Column
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
store[is_min_year] = 1
)
)
For Created Measure
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
[is_min_year_measure] = 1
)
)
Now add "Order Date" and measure "count_cust_for_min_year" to your Matrix. The out put will same as below-

Count datetime intervals which contains timestamp with DAX

I have two tables:
A calendar table with both dates and hours.
And a table that contains incidents, with start time and end time in addition to set of attributes (only one, incident_code, in the example table).
For a specific date range, I want to show how many incidents occurred each hour.
The following measure works, but struggles when the incident table becomes large and there is several slicers
incidentCnt =
CALCULATE(
COUNTROWS(incidents);
FILTER(
incidents;
incidents[start_datetime] < MAX(date_hour[datetime]) &&
incidents[end_datetime] > MAX(date_hour[datetime])
)
)
What would be a more efficient way to calculate this with DAX in Power BI?
EDIT: This expression will work with the data presented above (minor edits to the accepted answer).
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
DATE(
YEAR(incidents[start_datetime]);
MONTH(incidents[start_datetime]);
DAY(incidents[start_datetime])
) + TIME(HOUR(incidents[start_datetime]);0;0);
incidents[end_datetime];
TIME(1;0;0)
);
"Expanded_incident_time"; [Value]
)
)
A more effectiv way would be to expand the incidents table so that each incident occurs for each time between the start and end datetime. This you can do with a calculated table like this:
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
incidents[start_date];
incidents[end_date];
TIME(h;m;s)
);
"Expanded_incident_time"; [Value]
)
)
Change the TIME(h;m;s) to the time resolution of your choosing.
Then make a relationship between your dateTime dim table and the [Expanded_incident_time] column (this should be a 1:*). Then you only have to make a measure which counts the number of rows in the new table. This will give you the number of active incidents during a specific time and I believe it will be faster than reitterating over the table each time.

PowerBI DAX - Using a measure as a filter within a CALCULATE function

I want to create a DAX measure in PowerBI that will provide an aggregate of sales on a specific date.
I need that date to be controlled by a date slicer. Specifically the the maximum date on the slicer.
I would expect this to be a Calculate() function. So something like the following if it was hard coded with a date.
=CALCULATE(SUM(FactInternetSales2[Sales]), DimDate2[Dim Date] = DATE(2018, 06, 18))
But I need the filter component of the the Calculate() function (i.e , DimDate2[Dim Date] = DATE(2018,06,18)) to be dynamically populated from the max date on the date slicer. I understand, however that a measure can't be used as a filter in a calculate function - so I can't create a measure such as follows to identify the maximum date
=LASTDATE(DimDate2[Dim Date])
and then use it in the Calculate() function such as
=CALCULATE(SUM(FactInternetSales2[Sales]), DimDate2[Dim Date] = LASTDATE(DimDate2[Dim Date])
Can anyone outline how I can use the maximum date from the slicer to filter the Calculate() function, or achieve the same outcome?
A copy of my working file is located here
https://drive.google.com/file/d/1d1JiyPm1jOD9XkVqv3Q5pm0vk1FMotH9/view?usp=sharing
Cheers
Steve
You can read in the parameter value into a variable.
SalesSum =
VAR EndDate = LASTDATE ( DimDate2[Dim Date] )
RETURN
CALCULATE ( SUM ( FactInternetSales2[Sales] ), DimDate2[Dim Date] = EndDate )
You can use the Calculate() function with a filter like this:
=CALCULATE(SUM(Table1[SalesValue]), FILTER(Table1, Table1[Year] = 2019))
To get the value from a slicer you go to Modeling > New Parameter. Here you can specifie your parameter needs and hit OK. Now you gat a new table and column for this parameter on the fields pane. Just reference now on this column with the following code:
=SELECTEDVALUE(ParameterTable[ParameterValue])