DAX function in Power BI - powerbi

I have a problem that is looking for dates in the same month in a column and if the dates have the same id, I want to take the difference of the dates. Can someone help me?

Can there only be 2 dates?
If so (or if you are ok with max and min dates) you could write a measure like:
Date_difference =
var sid=Table[Id]
return
Calculate(DATEDIFF(MIN(Table[Date]), MAX(Table[Date]), DAY),Filter(Table,Table[Id]=sid))
What this measure does is first "saves" the Id you are looking for, then calculates day difference for max and min date when id is the "saved" Id.
If you want to not use varibales, you can use Earlier function:
Date_difference = Calculate(DATEDIFF(MIN(Table[Date]), MAX(Table[Date]), DAY),Filter(Table,Table[Id]=earlier(Table[Id])))

Related

Subtracting measure value from each row of a table in Microsoft Power BI

I have created two slicers in Power BI for Month and Year. These take the month and year value and in return generate a date.
Eg. Month - May & Year- 2019 generates 01/05/2019 with the help of the a measure formula :
MyFilterDate = DATE(SELECTEDVALUE(ListoY[Year]),SELECTEDVALUE(ListoM[Month],0),"01")
I want to subtract this date with one that is already in a column in the table.
I'm facing an issue when I try to subtract the measure from the existing column by writing DAX.
SUBVALNEW = DATEDIFF([MyFilterDate],Table[Date],DAY)
But when I try to do so, the result is not right. In fact when I try to check the value of [MyFilterDate] in the Data Model, it is not the same value as one calculated in the measure. (It instead looks like it displays the date in Table[Date])
Is there any way to subtract a Dynamically chosen date from a date in a column?
Any help would be appreciated.
I understand what your thinking patern is but power bi works a bit different. Dynamic measures cannot be used with calculated columns. When doing so the value of the measure is not set.
The way to go about this is by creating a Date table:
Date = CALENDAR(MIN(CalendarWeek[Date]);MAX(CalendarWeek[Date]))
Do NOT connect it to the model. From here you can make 2 slicers (based on the Date table). One your set to the month and the other to the year.
Next step is to adjust the MyFilterDate
MyFilterDate = DATE(SELECTEDVALUE('Date'[Date].[Year]);SELECTEDVALUE('Date'[Date].[MonthNo]);"01")
Make SUBVALNEW a measure (cannot be a column because it is dynamic!)
SUBVALNEW = DATEDIFF([MyFilterDate];SELECTEDVALUE(CalendarWeek[Date]) ;DAY)
Ass the Measuer to your visual, see end result below)

Dax Finding date based on Criteria calculated Column

I couldn't find an answer for my issue elsewhere, so trying my luck here.
I have sales table and my final result should determine if there were sales made for same person in specific period of time, for example within 7 business days. for example:
For ID 123 I have to flag it that sale for products A,B,C where within specified period.
For ID 1234 only sales of products A and B meet the criteria product C was sold in irrelevant time frame.
I've created a date table with indicators that determine for each date if the date is a working day, but i am struggling to calculate the relevant last working day
For example: I need that for 01/01/2019 i will get 10/01/2019 date, based on NUMOFDAYS and FinalWorkday = TRUE, which basically means that i have to count NUMOFDAYS times TRUE statement for each date and return corresponding date.
After that step done I think that it would be pretty easy for me to determine if the sale for a person was made in specific time frame.
If someone can give me direction for that much appreciated
Thank you in advance
You could use a DateTable like this one:
I used the following DAX-expressions for the calculated columns:
nrDays = 7
isWorkDay = WEEKDAY('DateTable'[Date],2) < 6
rankWorkingDays = RANKX ( FILTER ( DateTable, DateTable[isWorkDay] = TRUE () ),
DateTable[Date] , , ASC )
LastWorkDay = LOOKUPVALUE ( DateTable[Date],
DateTable[isWorkDay], TRUE (),
DateTable[rankWorkingDays], DateTable[rankWorkingDays] + DateTable[nrDays])
This issue can be solved by the following, since three are non-working days/holidays we can filter them out via POWERQUERY, than add an Index Column and Another column Which is basically Index column + Number of days wanted, then simply merge duplicate of dates query on Index+number of days wanted column on Index column. And we get the correct date

Calculate minute differences in a column

I'm working with Power BI. I wanna know from the Datestamp column see how much minute difference it's between the previous value and the last one.
I tried this formula below but got totally different value.
Datediff =
DATEDIFF(
DataView[CommTimestamp];
TODAY();
minute
)
The date-stamp column look like YYYY-MM-DD-HOUR-MINUTE-SECOND
The code you have written compares the [CommTimestamp] column with the TODAY() function. TODAY() returns YYYY-MM-DD-00:00:00 when it's calculated (different for Calculated columns and measures).
Therefore what you will see is the number of minutes from [dateTime] until today:
NB. That the [DiffAdd] column which is calculated like: [DateTime]+[DateDiffToday]/1440 doesn't take the seconds into account.
So without more info about your data table and your expected results, it's difficult to help you.
If you need to know difference in minutes you cannot use TODAY() as Oscar stated, it only provides 'Day' grannularity. You ca try using NOW() instead:
Datediff =
DATEDIFF(
DataView[CommTimestamp];
NOW();
minute
)

DAX: Subtract X Years/Days/Months from NOW()

I'd like to create a dynamic date table, the date range is NOW - 3yr to NOW. How is this done in DAX?
I know how to use the calendar function, and have used the MIN and MAX on a table of invoices to get the start/end dates, but now need to get the current date -3 years.
Figured it out, I used this:
Date Filter = CALENDAR(DATE(YEAR(TODAY()) - 3,MONTH(TODAY()),DAY(TODAY())),today())

Power BI - Getting a forecast date from a measure

I have a "DaysLeft" measure that I created that contains the number of days (Decimal) left until a specific Plant area is completed. So for each value in my "PlantArea" column, there is a unique "DaysLeft" measure value.
I now want to get the forecast date for when each "PlantArea" will be complete. Thus, Today()+DaysLeft for that particular plant area.
I've tried:
DATEADD(TODAY(), +[DaysLeft], DAY)
but that gives me nothing :(
Any idea how I can do this?
DATEADD() only works for column. For measure, you can simply try
Forecast Date = NOW() + [DaysLeft]
NOW() is better than TODAY() for this case as it returns the current datetime instead of just date.
Result: