08/19/2015
by Marlon Ribunal
4 Comments

Getting Started With Power BI Desktop: Getting Data

Now that the Power BI Desktop is installed, let’s get some data. As of this writing, Power BI can get data from 37 different data sources and data connections – File (Excel, CSV, XML, Text), Database (SQL Server, Oracle, IBM DB2, MySQL, PostgreSQL, Terada, etc.), Azure (SQL Database, Azure Marketplace, etc.) and other data connections (Web, OData Feed, Hadoop File, Dynamic CRM Online, Salesforce Objects/Reports, Facebook, etc.).

Let’s work with a CSV file.

Go to Data.gov and download the Customer Complaint Database. Choose the CSV file. Here’s the link: http://catalog.data.gov/dataset/consumer-complaint-database

Data Gov Customer Complaint Database

Save the file where it’s convenient to access it. Let’s fire up the Power BI Desktop app. On the Home tab (ribbon), click Get Data then select CSV.

Get Data From Home Ribbon

The Load dialog displays a preview of the CSV data.

Power BI Load Data Dialog

As you can see from the dialog, aside from the Load option, you can also Edit the data. To simplify this demo, let’s delete some columns (Complaint ID, Submitted via, Company Response, Timely Response?, and Consumer Disputed). Select Choose Columns from the Home ribbon. Un-check the columns that we want to exclude from our data.

Edit Data source in Power BI Desktop

Click OK. Now, were ready to load the data. Click Close & Load from the Home ribbon. Select Close & Load.

Load Data Power BI Desktop

After the data load is finished, the Power BI Desktop brings us to the report designer. You can always go back to the data by clicking the table icon Power BI Desktop Show Data Icon on the left-hand panel.

On the next post, we’ll create our first report.

08/13/2015
by Marlon Ribunal
4 Comments

Getting Started With Power BI Desktop: Installation

Power BI is becoming popular for a lot of reasons. Simply put, Power BI is “BI on your fingertips.” Companies looking to adopt BI technology need not look further. Power BI is easy to learn with its intuitive tools. Gone are the days when building BI solutions always involve massive resources.

Power BI lowers the bar in the BI know-how. It eliminates the complexities of building BI solutions.

Power BI has three (3) components: Power BI, Power BI Mobile, and Power BI Desktop. Power BI is “cloud-based business analytics service.” Power BI Mobile provides you the capability to get connected with your data anywhere you go. Power BI Desktop gives you the platform to author reports intuitively.

In this series, let’s take a look at Power BI Desktop.

Before we get started, let’s see how easy it is to download and install the app.

Go to http://powerbi.microsoft.com/desktop and download Power BI Desktop.

power bi desktop download

 

Once downloaded, execute the executable file to initialize the installation. Note: Power BI Desktop requires Internet Explorer 10 or higher versions.

Install Power BI desktop

Click Next, and just follow the installation wizard. Accept the terms in the app’s license agreement.

POwer Bi Desktop License Agreement

Specify where you want the app installed. I wouldn’t change the default.
default directory of Power Bi Desktop

Confirm installation of the Power BI Desktop app. Click the Install button

confirm installation of power bi desktop

Then wait for the installation to complete. It should only take a couple of minutes.

actual installation of power bi desktop

After the installation is completed, the wizard confirms the success of the installation. Select the Launch Microsoft BI Desktop and click Finish.

completed power bi desktop installation

Wait for the Power BI Desktop to initialize.

power bi desktop initializing

After the successful initialization of the app, the Power BI Desktop splash screen shows up. The screen provides some useful resources to learn more about Power BI. I’d keep the Show this page on startup option checked just so I have  a bookmark to these useful resources. You can also access this screen in the File tab then select Getting Started.

power bi desktop splash

That’s it. Power BI Desktop is now ready for analytics or report development!

Next, let’s get some data for our Power BI Desktop.

 

05/29/2015
by Marlon Ribunal
5 Comments

Finding gaps or missing dates in a date range using TSQL

Here’s a quick how-to on returning temporal data set that includes missing dates. Suppose you are tasked to query an employee’s “time sheet”. You’d want to return not only the days he’s reported to work but also all the days that he missed.

The expected result would look something like this:

finding missing dates in sql server

Actually, this was asked in the Facebook page of a SQL Server user group.

These are the answers that I gave. There is more than one way to skin a cat (no cats were harmed in the writing of this post).

These are only two of the many options. Feel free to add yours in the comment section.

Test Data

Let’s create our sample primary table (TimeSheet)

 

CREATE TABLE TimeSheet
(
logdate DATETIME NULL
,empno CHAR(3) NULL
,timein SMALLDATETIME NULL
,timeout SMALLDATETIME NULL
);

 

Let’s insert some sample data

 

INSERT INTO dbo.TimeSheet
( logdate, empno, timein, timeout )
VALUES ( '5/18/2015', '001', '08:30AM', '04:30PM' )
,( '5/20/2015', '001', '09:00AM', '03:30PM' )
,( '5/22/2015', '001', '08:30AM', '05:30PM' );

 

Now that we have our sample data let’s run a couple of scripts.

Using Calendar Table

One of the most convenient ways of finding missing dates in a date range is by using a Calendar table. There’s a ton of materials on this topic. Here’s one that tells you why you need a Calendar table.

For our demo purpose, lets create a simple calendar table that will contain our date range.

 

CREATE TABLE calendar ( date SMALLDATETIME );

 

Then, let’s populate that with a small sample of date range:

 

INSERT INTO dbo.calendar
( date )
VALUES ( '05/18/2015' )
,( '05/19/2015' )
,( '05/20/2015' )
,( '05/21/2015' )
,( '05/22/2015' );

 

Let’s use the calendar table to come up with the expected result:

 

SELECT CONVERT(VARCHAR(10), c.date, 101) AS logdate
,t.empno
,CONVERT(CHAR(5), t.timein, 108) AS timein
,CONVERT(CHAR(5), t.timeout, 108) AS timeout
FROM dbo.TimeSheet t
RIGHT OUTER JOIN dbo.calendar c
ON t.logdate = c.date;

 

And here is what we got using that calendar table:

 

logdate empno timein timeout
---------- ----- ------ -------
05/18/2015 001 08:30 16:30
05/19/2015 NULL NULL NULL
05/20/2015 001 09:00 15:30
05/21/2015 NULL NULL NULL
05/22/2015 001 08:30 17:30

(5 row(s) affected)

 

Using Common Table Expression (CTE)

Introduced in SQL Server 2005, Common Table Expression (CTE) has been a convenient “tool” for many SQL Developers. Basically, a CTE is a temporary result set. It is not stored as an object in SQL Server. One of the advantages of CTE’s is it can reference itself. How is that useful? Well, we can see that in our example.

 

DECLARE @startdate DATETIME
,@enddate DATETIME;

SET @startdate = '05/18/2015';
SET @enddate = '05/22/2015';
WITH calendardates
AS ( SELECT date = @startdate
UNION ALL
SELECT DATEADD(DAY, 1, date)
FROM calendardates
WHERE DATEADD(DAY, 1, date) <= @enddate
)
SELECT CONVERT(VARCHAR(10), c.date, 101) AS logdate
,t.empno
,CONVERT(CHAR(5), t.timein, 108) AS timein
,CONVERT(CHAR(5), t.timeout, 108) AS timeout
FROM dbo.TimeSheet t
RIGHT JOIN calendardates c
ON t.logdate = c.date;

 

You probably noticed that our CTE named “calendardates” is referenced in the FROM clause within the CTE statement.

Here’s the result.

 

logdate empno timein timeout
---------- ----- ------ -------
05/18/2015 001 08:30 16:30
05/19/2015 NULL NULL NULL
05/20/2015 001 09:00 15:30
05/21/2015 NULL NULL NULL
05/22/2015 001 08:30 17:30

(5 row(s) affected)

 

Update: Jeff Moden called my attention to this beautiful article he wrote about the negative impact of Recursive CTE’s, which is a must-read for anyone interested in Recursive CTE’s or rCTE’s as he called them. He’s that good at making up terms like that. Well, if you heard about “RBAR” (ree-bar) and “Tally Table”, he coined those terms.

Please read Jeff’s “Hidden RBAR: Counting with Recursive CTE’s” before considering using rCTE’s in your TSQL codes.