DAX use slicer selection in calculate statement - powerbi

I am trying to use a calculate statement with a measure to filter results of a table but I need the values to change if the user selects 1 date from a slicer. Below is the code I am using.
PCurrentDay = calculate(sum('Sheet0 (2)'[Toys_Count]),datediff('Sheet0 (2)'[SCAN_Date],'Sheet0 (2)'[SHIP_Date],DAY)=0)
This returns a value but it won't update if the user selects a specific scan date. Any ideas?

Can you replace the measure with following
Measure =
CALCULATE (
SUM ( 'Table'[val] ),
FILTER (
'Table',
DATEDIFF (
CALCULATE ( MAX ( 'Table'[scan] ) ),
CALCULATE ( MAX ( 'Table'[ship] ) ),
DAY
) = 0
)
)
CALCULATE inside DATEDIFF provides the correct context for the filter to work.

Related

How to count dates in Power BI DAX

I have a table like below.
I want to show count of LicenseEndDate. Like In my slicer I have taken WeekEnding column suppose if I select weekending date like 09/25/2022 then I want show the count of LicenseEndDate dates that are ended between 09/18/2022 to 09/25/2022. How to create measure for this. I want to show that count in card visual. I created a measure like below
License_Expired_Count = COUNT(Trade[LicenseEndDate]).
But It giving me count of all. That mean the License which are expiring in the feature count also it was showing. But want to show count of license which are expired in the selected period. How create measure for this.
Please check this code, let me know If It works for you.
License_Expired_Count =
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
Trade[LicenseEndDate] <= SELECTEDVALUE ( Trade[WeekEndingDate] ),
Trade[LicenseEndDate]
> SELECTEDVALUE ( Trade[WeekEndingDate] ) - 7
)
try this :
License_Expired_Count =
VAR _slt =
SELECTEDVALUE ( Trade[WeekEndingDate] )
RETURN
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
ALLSELECTED ( Trade[WeekEndingDate] ),
DATESBETWEEN ( Trade[LicenseEndDate], _slt - 7, _slt )
)
check sample file attached

DAX alternative for IN function

I have a table in SSAS Tabular model having delivery date of vehicles.
There is a dimension table with Age values -
I need to write a DAX to show the count of delivery for each year.
In the PowerBI report which connects to the Tabular model, the user can select the Age (multiple) from the filter which is on Dim_Age. So if the user selects Age 1 it should show the count of 2019 deliveries for the year 2020.
Now the challenge is I am working on SSAS 2016 build version 13.0.5426.0. This version does not support "IN" function. I have tried below and this does not work.
VAR selecteyear =
IF ( HASONEVALUE ( Dim_Cal[year] ); VALUES ( Dim_cal[year] ) )
CALCULATE (
COUNT ( Delivery_Data[Vechile] );
( selectedyear - Delivery_Data[DeliveryYear] )
IN VALUES ( Dim_Age[Age] )
)
Please help to know the alternate solution.
In general, the CONTAINS function is the workaround in older versions that don't support IN.
Here's how you could use it in you situation:
AgedCount =
VAR selectyear =
IF ( HASONEVALUE ( Dim_Cal[year] ), VALUES ( Dim_Cal[year] ) )
RETURN
CALCULATE (
COUNT ( Delivery_Data[Vechile] ),
FILTER (
ALL ( Dim_Cal ),
CONTAINS ( Dim_Age, Dim_Age[Age], selectyear - Dim_Cal[year] )
)
)
This iterates through the whole Dim_Cal table and for each row, checks if selectyear - Dim_Cal[year] is one of the values selected from the Dim_Age[Age] filter.

How do I average a column of values while excluding rows that contain a certain value from another column? PowerBi

I am trying to write a code for a data table in Power BI that averages values of a table but categorizes them based on ID and Project but at the same time exclude a value from another column. Below is what I am trying to accomplish and column AVG is the goal. Excluding Type = "II" and averaging the values based on category columns [ID] and [Project].
Below is the code I am working on but it is incorrect. What would be the best solution?
AVG =
CALCULATE (
AVERAGEX ( FILTER ( Table, Table[Type] <> "II" ), Table[Values] ),
ALLEXCEPT ( 'Table', 'Table'[ID], 'Table'[Project] )
)
How about this?
AVG =
CALCULATE (
AVERAGE ( Table[Values] ),
ALLEXCEPT ( Table, Table[ID], Table[Project] ),
Table[Type] <> "II"
)
I don't see a reason to use an iterator function (AVERAGEX) and a simple Boolean filter should work how you want (instead of using FILTER).

Using summarize and userelationship to generate a sum based on a condition

Story:
I have two date columns in my fact table, one is for orderdate and the second is for orderdate/refund/cancelled date.
I created two relationships between the date table and the fact table.
Active: Date > OrderDate
Inactive: Date > OtherDate
I would like to sum the # of refunds per day using the inactive relationship.
What I tried:
Returns =
VAR summary =
SUMMARIZE (
FILTER ( Query1, Query1[kind] = "refund" ),
Query1[orderId],
"returns", MAX ( Query1[amount] )
)
RETURN
CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[OtherDate], DateTable[Date] ),
summary
)
For some reason, it's using the active date column. Any suggestion on how to fix the above formula?
I'm not sure I understand how you are intending to use the summary variable here, but note that USERELATIONSHIP doesn't affect it at all since it's already computed.
You might not need that variable at all. Try this:
Returns =
CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[OtherDate], DateTable[Date] ),
Query1[kind] = "refund"
)

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: