How to find date periods between 2 dates? - azure

I have 2 dates one is stored inside my date and for other date I am using calculated column in order to store the end date into that, how an I calculate the difference in time period between those dates, I need the date period between all those dates is that possible with DAX?
How can I use calculated column inside my DAX and also I dont have a calender table inside my database.
2019-05-31 and end date is 2019-06-03 then the difference will give me 3 dates that is 2019-05-31,2019-06-01 2019-06-02 and 2019-06-03

Totally possible and easy. If you just need the difference between dates in two columns you can create a calculated column using the following:
DateDiff =
DATEDIFF ( 'Table'[Date1], 'Table'[Date2], DAY )
This will take the difference between Date1 and Date2 in days.

DECLARE #start_date [date] = CAST(‘2012-08-01’ as [date])
DECLARE #end_date [date] = CAST(‘2012-09-01’ as [date])
SELECT
DATEADD(day, [v].[number], #start_date)
FROM
[master].[dbo].[spt_values] [v]
WHERE
[v].[type] = ‘P’ AND
DATEADD(day, [v].[number], #start_date) <= #end_date

Related

Pandas - Remove Timestamp

I am trying to calculate basic statistics using pandas. I have precip values for a whole year from 1956. I created a "Date" column that has date for the entire year using pd.date_range. Then I calculated the max value for the year and the date of maximum value. The date of maximum value show "Timestamp('1956-06-19 00:00:00" as the output. How do I extract just the date. I do not need the timestamp or the 00:00:00 time
#Create Date Column
year = 1956
start_date = datetime.date(year,1,1)
end_date = datetime.date(year,12,31)
precip_file["Date"] = pd.date_range(start=start_date,end=end_date,freq="D")
#Yearly maximum value and date of maximum value
yearly_max = precip_file["Precip (mm)"].max(skipna=True)
max_index = precip_file["Precip (mm)"].idxmax()
yearly_max_date = precip_file.iat[max_index,2
Image of output dictionary I am trying to create
May be a duplicate of this question, although I can't tell whether you are trying to convert one DateTime or a column of DateTimes.

How can I split weekly data to monthly using Excel/ Power Pivot?

My Data is in weekly buckets. I want to split the number into a monthly number but, since there is an overlap in days falling in both the months, I want a weighted average of the data in terms of days that fall in each of the months. For example:
Now, in the above picture, I want to split that 200 (5/7*200 in Jan, 2/7 in Feb). How can I do that using Excel/ Power Pivot/ Dax Functions? Any help here is much appreciated.
Thank you!
Assuming your fact table looks something like below. Values are associated with the starting date of the week it occurred.
Although it may actually be a more granular data, having multiple rows for each week with additional attributes (such as identifiers of a person, a store, depending on the business), what being shown below will work the same.
What we need to do first is to create a date table. We can do that in "Design" tab, by clicking "Date Table", then "New".
In this date table, we need to add a column for starting date of the week which the date of each row is in. Set the cursor to "Add Column" area, and input following formula. Then rename this column to "Week Start Date".
= [Date] - [Day Of Week Number] + 1
Now, we can define the measure to calculate the number allocated to each month with following formula. What this measure is doing is:
Iterating over each row of the fact table
Count the number of days for the week visible in the filter context
Add the value portion for the visible days
Value Allocation := SUMX (
MyData,
VAR WeekStartDate = MyData[Week]
VAR NumDaysInSelection = COUNTROWS (
FILTER (
'Calendar',
'Calendar'[Week Start Date] = WeekStartDate
)
)
VAR AllocationRate = DIVIDE ( NumDaysInSelection, 7 )
RETURN AllocationRate * MyData[Value]
)
Result in the pivot table will be looking like this.

DAX average including zeroes

My question is that I'd like to calculate a daily average taking into account days with zeroes.
Let me clarify it:
I'd like to calculate the average daily value of units for each category, with the following table:
When I sum up the values for each day and category, I get:
I'd like to include in the average calculation the zeroes.
I use the following code:
SUMMARIZE(
Data,
Data[Category],
"Average",
AVERAGEX(
SUMMARIZE(
Data,
Data[Date],
"Sum of Units",
SUM(Data[Units])
),
[Sum of Units]
)
)
But the problem is that for category B it doesn't take into account those days with 0s.
Could you please guide me how to solve it?
Thanks in advance!
Jorge
One way to solve it would be to create a calendar table, which can then be used to count the number of days in any of your grouping periods. This also means you can use non standard calendars, for example something like a 4-4-5
With a calendar table created you can leverage the FIRSTDATE and LASTDATE functions in DAX.
I recommend adding a past dates column to the calendar table, which can be created using DAX with the following formula. The today function in DAX when used in a calculated column will only evaluate when the model is updated.
In my example I created a calculated column in the date table called Past_Dates
Past_dates =IF( TODAY() > [Date], TRUE(), BLANK())
So for example if I create the following measures, the Today one just being used for illustration.
Start_date:=FIRSTDATE( Dates[Date] )
End_Date:=LASTDATE( Dates[Date])
Today:=TODAY()
EndPhased:=CALCULATE( LASTDATE( Dates[Date] ), Dates[Past_dates] = TRUE())
Which when added to an empty pivot table evaluate to the following.
Note that you would want to have year somewhere in the pivot if you have multiple years of data.
The idea of having the Past flag is to keep from counting days where they would not be any data due to being in the future. So for example in September it would only use 11 days of sales and not the full 30.
As the below example shows, the finding of the start and end date even work on a Quarter basis.
So now that we have a way to get the Start and End date of a period, the next step is adding it into our Calculated measure.
In the below example, we are iterating though every unique Category name. Within the category, we are summing the units sold, and then dividing by the number of days between EndPhased and Start_Date + 1. Then averaging the results by the number of categories that have data in that period.
Average:=AVERAGEX (
VALUES ( Data[Category] ),
CALCULATE ( DIVIDE ( SUM ( Data[Units] ), [EndPhased] - [Start_date] + 1 ) )
)
It seems to me that you need to have an underlying row with the zero value in it (in your initial Data table). Right now, you don't actually have a zero value for B on 02/01/2017. If you add a row with the values | 02/01/2017 | B | 0 | I believe you will see that the average accounts for it. As things stand for you right now, I believe the pivot table actually reflects no value (blank) rather than zero value, so the zero isn't currently counted for the average.
I think the best way is to add the missing zeroes, using UNION function:
SUMMARIZE(
Data,
Data[Category],
"Average",
AVERAGEX(
UNION(
SUMMARIZE(
Data,
Data[Date],
"Sum of Units",
SUM(Data[Units])
),
ADDCOLUMNS(
EXCEPT(
ALL(Data[Date]),
VALUES(Data[Date])
),
"Sum of Units",
0
)
),
[Sum of Units]
)
)

How to calculate difference in time from the same column grouped by another column?

I have an excel power query (also added to powerpivot data table) with two fields: Location and OpenedTime. Location column has many duplicates such as this example:
Location OpenedTime
LOC_1 9/25/2016 1:48:56 AM
LOC_2 9/30/2016 2:01:00 PM
LOC_1 10/10/2016 12:08:00 PM
LOC_3 9/17/2016 9:35:43 PM
LOC_1 10/15/2016 11:14:21 AM
LOC_2 10/16/2016 8:27:34 PM
LOC_3 10/18/2016 11:52:27 AM
LOC_2 10/22/2016 9:09:00 AM
What I am looking for is a way to calculate the difference in open time for the 3 locations.
So a new column would display the difference between 10/10/2016 12:08:00 PM and 9/25/2016 1:48:56 AM for LOC_1.
Since there are multiple timestamps to calculate differences of, I was thinking if it would be possible to average all differences into one average difference for each LOC
Thank you!!
This is something you can do to get the desired result:
Add a new column to your PowerPivot table using this DAX expression:
Previous Date=
CALCULATE (
MAX ( [OpenedTime] ),
FILTER (
TableName,
EARLIER ( TableName[OpenedTime] ) > [OpenedTime]
&& EARLIER ( TableName[Location] ) = [Location]
)
)
This expression get the previous date against you should calculate the difference for each row, note that if there is no a previous date it left it BLANK.
Then add a second calculated column to your PowerPivot table to calculate the difference in Minutes:
Diff (Min)=DATEDIFF([Previous Date],[OpenedTime],MINUTE)
If you want to calculate the difference in hourse just replace MINUTE by HOUR or DAY or SECONDS, etc.
It will give you a column with the difference then you can just calculate the average easily by creating a measure:
Avg OpenedTime (Min) := AVERAGE(TablaName[Diff (Min)])
Let me know if this helps.

PowerPivot - relating data by date range

Does anyone know what DAX function I should use to display information from one table in another table.
I've got 2 tables in my data model:
Tasks
- Task ID
- Task Name
- Start Date
- End Date
Fiscal Periods
- ID
- Period Name
- Start Date
- End Date
What I'm trying to do is for each Task, add a calculated column the is populated with the corresponding Fiscal Period ID. I'm trying to add a filter or calculation that specifies:
- if the task start date is between Fiscal Start Date and Fiscal End Date, return the fiscal period id.
Anyone have any ideas?
Thanks,
Ro
This might help:
In case Fiscal Period ID is a number:
=CALCULATE(MAX(Periods[ID]),FILTER(Periods,Periods[Start Date]<=Tasks[Start Date] && Periods[End Date]>=Tasks[Start Date]))
In case Fiscal Period ID is not a number:
Put Start Date to calculated column first, say 'Period Start Date'
=CALCULATE(MAX(Periods[Start Date]),FILTER(Periods,Periods[Start Date]<=Tasks[Start Date] && Periods[End Date]>=Tasks[Start Date]))
and then use LOOKUPVALUE for ID
=LOOKUPVALUE(Periods[ID],Periods[Start Date],Tasks[Period Start Date])

Resources