Calculate minute differences in a column - powerbi

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
)

Related

DAX function in Power BI

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

Power BI: Percent Change Formula

I want to create a simple percent change formula in power BI. Specifically, I want to evaluate month over month %change in cost. I know that percent change can be defined in different ways, so to be clear this is the formula that I am referring to:
%change = ([current value] - [previous value])/[previous value].
Here is an example in Excel of what I want to achieve:
I cannot find a way to target a specific row in a DAX formula. It seems that power BI only wants to include aggregate values such as sum, max, etc.
As a final note, I know that PowerBI includes a percent change "quick measure". However, the 'Base Value' always requires you to select aggregate measure instead of a single row value.
As Alexis mentioned, there is no concept of referring to a specific row in DAX. It can be a hard notion for people who are used to Excel, but if you want to take advantage of PowerBi, you will have to "unlearn" Excel way of thinking.
To accomplish your goal, you need to do some data modeling first. As a common practice, replace your "Date" column with a proper Calendar table:
Calendar table explained
As a result, you will have a data model in PowerBi that looks something like this:
Once you have such structure in place, DAX is simple:
Current Month Cost = SUM(Expenses[Cost])
Previous Month Cost = CALCULATE( [Current Month Cost], PREVIOUSMONTH(Calendar[Date]))
% Change = DIVIDE( [Current Month Cost] - [Previous Month Cost], [Previous Month Cost])
I used Earlier to get the previous date value for Open
Accum = var previousOpen=CALCULATE(MAX(EOG[Open]),FILTER(EOG,EOG[Date]<EARLIER('EOG'[Date],1))) return Divide(EOG[Open]-previousOpen,previousOpen)+1

Power BI Rolling Average

I've used the new Quick Measures feature of Power BI to build a 3 month rolling average calculation and it's working well. The equation is displayed below. However, when I try to use this metric in a time series visualization, the calculations are displaying three months past the current month, but I'd like for the calculation to stop at the current month.
I've played around with the __DATE_PERIOD variable to no avail. My date filter for the page is set to show all dates in the current months or 12 months prior via a calculated column on the date table.
Is anyone aware of how I can get the visualization to end at the current month?
Average Days to Close Rolling Average =
IF(
ISFILTERED('Date'[Date]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy."),
VAR __LAST_DATE =
ENDOFMONTH('Date'[Date].[Date])
VAR __DATE_PERIOD =
DATESBETWEEN(
'Date'[Date].[Date],
STARTOFMONTH(DATEADD(__LAST_DATE, -3, MONTH)),
__LAST_DATE
)
RETURN
AVERAGEX(
CALCULATETABLE(
SUMMARIZE(
VALUES('Date'),
'Date'[Date].[Year],
'Date'[Date].[QuarterNo],
'Date'[Date].[Quarter],
'Date'[Date].[MonthNo],
'Date'[Date].[Month]
),
__DATE_PERIOD
),
CALCULATE(
'Closed Opportunities'[Average Days to Close],
ALL('Date'[Date].[Day])
)
)
)
In order to limit what is displayed within your chart, you need to filter the applicable date field so it only displays the dates you desire. In this case, you only want it to include dates <= today.
In order to automatically filter it when it is refreshed, I typically add a custom DAX column to the date table that I can filer on. In this case it would be something along the lines of:
excludeFutureDatesInd = 'Date'[Date] <= TODAY()
You can then add a visual, page, or report filter selecting all dates where [excludeFutureDatesInd] = True.
Not sure if you're still having issues with this, but I'd like to share a hack fix for those landing here. I fixed this issue by filtering on the base data (in your example, this would be "Average Days to Close"). Set a visual-level filter to include only those items where Average Days to Close > 0, and you should get the extra dates cut off the end of the graph.
So long as all of your base data passes through the filter, you should be good.

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:

Define the granularity for "day" when calculating NumOfDays?

When calculating the average sales per day, I have the following measure for NumOfDays:
NumOfDays = IF (
[Sales Amount] > 0;
COUNTROWS ( Date )
)
What this is doing is removing the number of days for those dates when there are no sales.
Thus, I have the following visual:
As you can see, the total is wrong.
This is due to the fact that the database has more years than those shown in the matrix.
How can I define the granularity for "day" when calculating NumOfDays?
That is, how can I count the rows for those days with sales only?
Thanks!
I recommend two things:
Use CALCULATE() instead of IF, which is the proper DAX way of defining measures with filters
Use DISTINCTCOUNT() instead of COUNTROWS(), to ensure you never run into double counting (in case your data ever becomes more granular)
This calculation may work - but you may need to specify the actual date column inside your Date table.
NumOfDays = CALCULATE(DISTINCTCOUNT(Date[Date]),FILTER(SalesTable,[Sales Amount] > 0))
If there is more to the problem, let me know what exactly you are expecting to see in your matrix.