Calendar table in power bi - calendar

I am trying to use calendar table for creating fiscal year
i tried this but this shows red mark under the dates
Calendar = ADDCOLUMNS(CALENDAR (DATE("1-Jul-2015"), DATE("30-Jun-2017")),
"Year",YEAR([Date]))

The DATE function takes three arguments, like this:
DATE(<year>, <month>, <day>)
So your table should be:
Calendar = ADDCOLUMNS(
CALENDAR(
DATE(2015, 7, 1),
DATE(2017, 6, 30)
),
"Year", YEAR([Date])
)
Here is the documentation: https://learn.microsoft.com/en-us/dax/date-function-dax

I would refer to a date than hard copy a date value. To cover all dates, it is better to use MIN and MAX of dates from your fact table and base your calendar on them:
Here is a good video on this:
https://www.youtube.com/watch?v=Oq5WOmo94_Q

Related

Generating date array from several start and end dates

I am trying to generate a dates array in BQ with dates that lie within several start and end dates.
For example, for days between one start and end date it looks like this:
SET DATES = GENERATE_DATE_ARRAY(DATE(2020,02,01), DATE(2022, 04, 25), INTERVAL 1 WEEK);
But how would I generate DATES if I want all these dates included: event_date BETWEEN "2020-02-01" AND "2020-04-25", event_date BETWEEN "2021-02-01" AND "2021-04-25", event_date BETWEEN "2022-02-01" AND "2022-04-25")
I didn't come across any easy fix.
Consider below
with dates_ranges as (
select "2020-02-01" start_date, "2020-04-25" end_date union all
select "2021-02-01", "2021-04-25" union all
select "2022-02-01", "2022-04-25"
)
select date
from dates_ranges,
unnest(generate_date_array(date(start_date), date(end_date), interval 1 week)) date
You can just create all those separate arrays and then concat_array() them, no?

Date filter on column in sql

I have query that filtered by date, for now it take only the last 24h from the moment I execute it, for doing that I'm using the next code:
( DateDiff(HH, vw_public_task.complete_date, getdate()) < 25)
There is a way that my date filter will give query results for the last 24h but not depending on my current hour but according to "day 08:00am" -- "day+1 08:00am" at any time that I execute it?.
For example if I execute my query now I want to see date results from yesterday 08:00am till today 08:00am.
You can calculate yesterday 8am using the formula:
-- Yesterday at 8 am.
SELECT
DATEADD(HOUR, 8, CAST(CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS DATETIME)) AS Yesterday8AM
;
GetDate returns the current date. The innermost date add subtracts one day. Casting this as a date removes the timestamp. Casting this back to a DateTime gives yesterday at midnight. Now we are dealing with a DateTime we can use date add, again, to add 8 hours.
If you are using SQL Server 2012, or above, consider the native function DATETIME2FROMPARTS instead.
Use Date() (How to part DATE and TIME from DATETIME in MySQL).
( DateDiff(HH, vw_public_task.complete_date, Date(getdate())+8 ) < 25)

Get total sum for each month in DAX

I have a tabular mode analysis server connected to a sql server database. I want to get the total x per month, and I have the total x per day. So for example, I have a table DailyEvent with the first 2 columns like this, and I want the column "MonthXCount":
TimeID XCount MonthXCount
20160429 3 11
20160430 8 11
20160501 4 4
So the total XCount for April is 11, so for every day in April I want 11. The total X count for may is 4 (so far).
What I have now is a MonthToDate total I think, calculated as:
=TOTALMTD(SUM('DailyEvent'[XCount]),'DailyEvent'[TimeID])
But I want to then have a column that puts the last value of the month in for every day of the month.
The end goal is to have a XCount by month graph in PowerBI, so I might not need to add the column here... I might be able to just tell PowerBi to graph the last day of the month, but I'm not sure how to do that and thought this would be easier.
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "begin transaction t
update table dailyevent
set monthxcount = (select top(1) monthxcount
from dailyevent
where MONTH(timeID) = 4
order by monthxcount desc)
where MONTH(timeID) = 4
--commit only if the rows affected corresponds to the number of rows found for the month of april.
commit";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
Do you have a Calendar Table in your model connected to your data table?
If yes, let's say the Calendar Table is called DimDate and that it contains a column called YearMonth, then the formula would be:
Month Sales := SUMX(
VALUES(DimDate[YearMonth]),
SUM(DailyEvent[XCount])
)
If you don't have a Calendar Table, then you can create a calculated column in your table called YearMonth with this formula:
=LEFT(DailyEvent[TimeID], 6)
Then calculate the sum of Month Sales with:
Month Sales := SUMX(
VALUES(DailyEvent[YearMonth]),
SUM(DailyEvent[XCount])
)
Hope this helps!
A note with respect to the formula you were using:
Time intelligence functions, such as TOTALMTD, require Calendar Tables. Therefore, be sure to add one to your data model before using them.
Edit:
Another solution could be to create a date column:
= DATE(
LEFT(DailyEvent[TimeID], 4),
MID(DailyEvent[TimeID], 5, 2),
1)
Then drop that column in the X-axis of the graph and the XCount column in the Y-axis.
You could use a measure calculation in Power BI, or in SSAS model, if there is a calendar table, make sure it is marked as date table otherwise the time logic will not work. I used the following query (DAX) it calculates the Month To Date
MtD :=CALCULATE(SUM('DailyEvent'[XCount]),DATESMTD('DateTabe'[calendarDate]))
If however there is no calendar or you are working with the be-spoke calendar, this may help
Running Totals Whitout a Traditional Calendar Table

Is there a way to create a date column from other columns?

As the title says, can I create a new column from two fields? For example, I created a new column from the master values table to represent the months, and I have another column with the year. Can I combine them to create a date with whatever day? What I have is on the left, what I want is on the right:
Month Year NewDate
---- ---- -------
3 2015 2015-03-01
4 2015 2015-04-01
Whenever I tried to cast the month as datetime, it would look like this:
1900-01-03
This is probably a dumb question, but I created a timeline that lists out the months, and the trouble lies whenever the end date parameter chosen is less than the start date parameter, such as December 2015 to January 2016. I created the report in SSRS and the parameters aren't quite working correctly. Thanks guys!
Also, here's a snapshot of what the timeline looks like:
SELECT
DATEFROMPARTS ( [Year], [Month], 1 ) AS NewDate
FROM
YourTable
;
select dateadd(mm, t.Month -1, dateadd(yy,t.Year-1900,'19000101'))
from table1 t

SSIS: How to Update Dates via a Transformation for Sundays and Holidays

What is the best way in SSIS and which transformation(s) should I use to change (update) dates from Sundays to the previous Friday with the exception of the 1st day of the month. The exception would then do an Update to change the date to Monday.
I would also then need an update to change dates that are on certain Holidays like Thanksgiving and Christmas to the previous day, unless of course that day is a Sunday.
I know how to find these dates using SQL, but I'm somewhat of a newbie in SSIS and not sure how this works using transformations or what the best method would be.
For reference, using SQL I use this to identify the dates within a month that are Sundays. I then do an update to change those to either Fridays, or like I said, if that causes it go outside of the current month to Mondays:
SELECT * FROM DriverRoutes
where [Date] BETWEEN dateadd(month,datediff(month,0,'1/1/2014')-1,0)
AND dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0, '1/1/2014'), 0))
and DATEPART(w,[Date]) = '1'
update DriverRoutes
set DATEPART(w,[Date]) = '6'
where DATEPART(w,[Date]) = '1'
etc.... And then I also change dates manually when they are on Holidays, but don't want to do that each month anymore.
Thanks in advance!
Create a calendar table that holds a list of dates and their business meanings. The dates that are recognized locally as holidays, and the holidays that are recognized by a business are in constant flux. Instead of embedded formulas, you end up with easy-to-read code.
UPDATE t1
SET [DeliveryDate] = t2.[NextBusinessDay]
FROM DriverRoutes t1
INNER JOIN Calendar t2 ON (t1.[Date] = t2.[Date])
WHERE t2.[IsHoliday] = 1

Resources