Friday, January 26, 2018

Dynamic Update of Date Timestamps

When working with data warehouses, it's common to encounter scenarios where you need to track changes over time. One such situation is when you want to add timestamps to your data tables to record when data was added or modified. This can be especially useful for troubleshooting and reporting purposes.

Here's a sample query that demonstrates how to add a new timestamp column and update it when new data is inserted:

 -- Add a new column and insert the default timestamp when added

ALTER TABLE dbo.TableName ADD NewColumnName datetime
ALTER TABLE dbo.TableName ADD DEFAULT (getdate()) FOR NewColumnName

By following this approach, you can efficiently track when data was added or modified without having to recreate the entire table or add complex logic for timestamp management. It's a simple yet effective way to save time when troubleshooting table updates.

Remember that while adding timestamps directly to your fact tables can be convenient, it's also essential to consider other design patterns. For instance, some experts recommend having separate time and date dimensions. These separate dimensions allow you to handle different time granularities (such as hours, minutes, or seconds) more effectively. If your reporting requirements involve querying data based on specific hours or minutes, using a dedicated time dimension can be beneficial.

Adding timestamps to your data warehouse tables is a valuable practice for maintaining historical context and improving query performance. Whether you choose to include timestamps directly in your fact tables or use separate dimensions depends on your specific use case and reporting needs.

Sunday, January 21, 2018

Create Dynamic Date Periods in DAX

Date filters are a very common dimension in dashboards and here is a simple way to create dynamic date filters. This saves times as it prevents you from having to pull dates from other sources.

In other tables, I would add a DateKey as follows...

DateKey = Format('Sales[DATE],"YYYYMMDD")


Then the following...
DATES


DATE = ADDCOLUMNS( CALENDAR(DATE(2017,1,1), DATE(2020,12,31)) ,"DateKey", FORMAT ( [Date], "YYYYMMDD" ), //NumericDate
"Date2", FORMAT ( [Date], "M/d/yyyy" ),

"Year", YEAR([Date]),

"FiscalYear","FY" & IF (MONTH([Date]) <=5 ,Year([Date]),Year([Date]) +1) ,

"Quarter", "Q " & FORMAT( [Date], "Q"),

"MonthNameLong" , FORMAT ( [Date], "mmmm" ) ,

"MonthNameShort" , FORMAT ( [Date], "mmm" ) ,"Month Key", FORMAT ( [Date], "YYYYMM" ),

"MonthNumber", MONTH([Date]),

"MonthYear", FORMAT ( [Date], "mmm " ) & YEAR([Date]), "Month Year 2", FORMAT ( [Date], "mmm " ) & RIGHT(YEAR([Date]),2),

"DayName", FORMAT ( [Date], "dddd" ), //Name for Each day of the week

"DayShortName", FORMAT ( [Date], "ddd" ),

"DayNumber" , WEEKDAY ( [Date] ) )

DATE PERIOD


 DatePeriod = 

UNION (

ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date' , DATESBETWEEN('Date'[Date],today()-07+1,today()) ), 'Date'[Date]),"Period","Last 07 Days") ,

ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date' , DATESBETWEEN('Date'[Date],today()-14+1,today()) ), 'Date'[Date]),"Period","Last 14 Days") ,

ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date' , DATESBETWEEN('Date'[Date],today()-30+1,today()) ), 'Date'[Date]),"Period","Last 30 Days") ,

ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date' , DATESBETWEEN('Date'[Date],today()-90+1,today()) ), 'Date'[Date]),"Period","Last 90 Days") ,

ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date'), 'Date'[Date]),"Period","Overall") )




 Once you create both date and date period tables, you can need to join these together using Manage Relationships. Make sure you specific to Both for Cross Filter Directions.