Pages

Monday, June 30, 2014

Difference Between COALESCE and ISNULL

ISNULL is a T-SQL function to replace NULL values as COALESCE does. At some cases these two functions get the same results but there are some differences. Below is a summary of their differences and I hope that it will help you decide which function to use when you need to determine the one with better performance or better concurrency control.


COALESCE ISNULL
Standard ANSI SQL? Yes No (Only for T-SQL)
Numbers of Parameters Two or more parameters. Two parameters only.
Data type determination of the resulting expression Determined by the data type of parameters with the highest precedence Determined by data type of the first parameter
Allow both inputs to be untyped NULLs?
No. For SELECT COALESCE(NULL, NULL); , it will return an error. You need to have at least one of the null values be a typed NULL. 

SELECT COALESCE(CAST(NULL AS INT), NULL);

Yes. For SELECT ISNULL(NULL, NULL); it will return a NULL typed as an integer.
NULLability of the resulting expression Considered to be NULL with non-null parameters. Always considered NOT NULLable
Working with subquery as input parameters When executing COALESCE((subquery), 1), the subquery is evaluated twice.  you can get different results depending on the isolation level of the query. Evaluated only once.

Monday, June 16, 2014

How to: Create Hierarchies in PowerPivot for Excel 2013

Just like hierarchies in SSAS, hierarchies in PowerPivot for Excel 2013 provides a very convenient way to let you group data through different layers. Today we will use date dimension to demonstrate how to create hierarchies in PowerPivot.

First of all, you need to be in the Diagram View of the PowerPivot Window. Click Diagram View button under Home Tab.

diagramView

Create Hierarchies

Select the table you would like to build hierarchies in. Here we choose DimDate table. Then click the “Create Hierarchies” button on the upper right corner of the table. After renaming the hierarchy you can drag the fields from the table down to the hierarchy that was just created. An alternative is to select a column first and then right click the column > Create Hierarchies.

CreateHierarchy

Add Hierarchies

Right click the column that you would like to add to a hierarchy. A column can only be in one hierarchy. If a column already exists in a hierarchy, there won’t have an option to “Add to Hierarchy”.

addHierarchy

If you would like to add a column from another table, you need to use RELATED DAX function to add columns from one table to another table. Before you can add columns, you must create a relationship between these tables.

Adjust Hierarchy Levels

If you would like to adjust the levels between hierarchies, right click any one and move up or down, or remove.

adjustHierarchy

After hierarchies are created, you can directly drag it to PivotTable Fields just as columns.

HierarchyInPivot

Monday, June 2, 2014

How to: Enable PowerPivot Add-ins for Excel 2013

Power Pivot in Microsoft Excel 2013 is a build-in add-in you can use to perform powerful data manipulation and analysis in Excel 2013. However, it is hidden somewhere and you have to enable it first before using it.

Here are steps to dig it out:

  1. Go to File > Options > Add-Ins.

    Step1Option

  2. Change In the Manage box, click COM Add-ins from the drop down list and click Go. Note that the default is Excel Add-ins.

    Step2Addin

  3. Check Microsoft Office Power Pivot in Microsoft Excel 2013 box > OK. You can check Power View and Power Query in similar way as you can see they are also listed in the COM Add-ins list.

    Step3Check

  4. Bingo! You are ready to use Power Pivot for Excel 2013! PowerPivot tab should appear in your ribbon as shown below: 

    Step4Result