Power BI Microsoft Dynamics CRM 2013 – Part 15

Colin Maitland, 03 December 2014

In my previous blog I demonstrated how to transform DateTime columns, from the Microsoft Dynamics CRM Online Organisation ODataSets loaded into Microsoft Excel, into Date columns and Year columns. There is however a step to be completed prior to that if you want the DateTime values to be converted from UTC Date/Time to Local Date/Time values.

Using the example of the CreatedOn column from the Opportunity ODataSet, the steps to perform this conversion are:

1.  Select Add Custom Column from the Add Column tab on the Query Editor ribbon bar.

Power BI Microsoft Dynamics CRM 2013 – Part 15

2.  From the Add Custom Column dialog provide a Name for the column; e.g. CreatedOnLocal.

Power BI Microsoft Dynamics CRM 2013 – Part 15

3.  Add the following custom DateTimeZone.RemoveZone(DateTimeZone.ToLocal(DateTime.AddZone([CreatedOn], 0))) custom formula into the Custom Column Formula field and then click OK to close the Add Custom Column dialog.
Power BI Microsoft Dynamics CRM 2013 – Part 15

The formula works by:

•  Adding the UTC Zone to the UTC Date/Time; e.g. using DateTime.AddZone([CreatedOn], 0) converts 5/11/2014 8:01:54 p.m. to 5/11/2014 8:01:54 p.m. +00.

•  Converts the UTC Date/Time/Zone to Local Date/Time/Zone; e.g. DateTimeZone.ToLocal( … ) converts 5/11/2014 8:01:54 p.m. +00 to  6/11/2014 9:01:54 p.m. +13.

•  Removes the Zone from the Local Date/Time/Zone; e.g. using DateTimeZone.RemoveZone( … ) converts 6/11/2014 9:01:54 p.m. +13 to 6/11/2014 9:01:54 p.m.

The following image shows the result:

Power BI Microsoft Dynamics CRM 2013 – Part 15

You can convert the CreatedOnLocal column to a Date in the same step by modifying the custom formula to: DateTime.Date(DateTimeZone.RemoveZone(DateTimeZone.ToLocal(DateTime.AddZone([CreatedOn],0))))

Power BI Microsoft Dynamics CRM 2013 – Part 15

The following formulas may be used, in conjunction with Add Custom Column, to add CreatedOn, EstimatedCloseDate and ActualCloseDate Date and Year columns, based on Local Date/Time rather than UTC Date/Time, to the ODataSet:

• DateTime.Date(DateTimeZone.RemoveZone(DateTimeZone.ToLocal(DateTime.AddZone([CreatedOn],0))))

• Date.Year(DateTimeZone.RemoveZone(DateTimeZone.ToLocal(DateTime.AddZone([CreatedOn],0))))

• DateTime.Date(DateTimeZone.RemoveZone(DateTimeZone.ToLocal(DateTime.AddZone([EstimatedCloseDate],0))))

• Date.Year(DateTimeZone.RemoveZone(DateTimeZone.ToLocal(DateTime.AddZone([EstimatedCloseDate],0))))

• DateTime.Date(DateTimeZone.RemoveZone(DateTimeZone.ToLocal(DateTime.AddZone([ActualCloseDate],0))))

• Date.Year(DateTimeZone.RemoveZone(DateTimeZone.ToLocal(DateTime.AddZone([ActualCloseDate],0))))

Notice that DateTime.Date() is used to convert to Date and Date.Year() is used to convert to Year.

This approach achieves the same results as demonstrated in my previous blog, but with the advantage that the Date/Time values are also converted to Local Date/Time prior to being converted to Date and Year.

Power BI Microsoft Dynamics CRM 2013 – Part 15

In my next blog I will demonstrate how to reorder and rename the columns. In subsequent blogs I will then demonstrate how to build some simple PowerPivot, Power View and Power Map reports in Microsoft Excel using the ODataSets loaded from the Microsoft Dynamics CRM Online Organisation.