How to Get Things Done in T-SQL

Whenever I have to fire up SQL Server to perform some analyses there are a few functions, keywords and capabilities that I always find myself referring to in order to analyze my data. As is the case with most T-SQL users, even those of us that have been using T-SQL for over a decade, in our heads we always know what we want to do but will refer to our favorite syntax reference sources in order to progress. I decided to make a handy reference sheet for myself and then decided to post it here for anyone else.

How to Create a Temporary Table in T-SQL / SQL Server

Temporary (i.e., temp) tables enable the storage of result sets from SQL scripts yet require less record locking overhead and thus increase performance. They remain in effect until they are explicitly dropped, or until the connection that created them is discontinued.

As I see it, their main benefit is that they preclude me from writing difficult to comprehend nested queries since I can place a result set inside a temp table and then join it back to a normal table at-will.

In this example, the results of permanent table ‘TABLE1’ will be placed into global temporary table ##TEMPTABLE:

SELECT 
     FIELDNAME1,
     FIELDNAME2,
     FILEDNAME3 
INTO ##TEMPTABLE 
FROM TABLE1

Temp tables are stored in the tempdb system database.

TempDB

“The tempdb system database is a global resource that is available to all users connected to the instance of SQL Server or connected to SQL Database.”

Additional Reference:

What do the Hashtags Mean in T-SQL Temp Table Creation?

 The number of hash signs “#” preceding the name of the temp table affects whether the scope of the table is local or global.

  • If you precede the temp table name with “#”, then the table will be treated as a local temp table.
  • If you precede the temp table with “##”, then the table will be treated as a global temp table.

“You can create local and global temporary tables. Local temporary tables are visible only in the current session, and global temporary tables are visible to all sessions. Temporary tables cannot be partitioned. Prefix local temporary table names with single number sign (#table_name), and prefix global temporary table names with a double number sign (##table_name).”

Additional References:


How to Drop a Temp Table
in T-SQL / SQL Server

 There are times when you will need to rerun code that creates a temp table. If the temp table has already been created, you will encounter an error.

“There is already an object named ‘##TEMP_TABLE_NAME’ in the database.”

Place the following code above the creation of your temp tables to force SQL Server to drop the temp table if it already exists. Change ##TEMP_TABLE_NAME to your table name and use the correct number of hashtags as applicable to a local (#) or global (##) temp table.

IF OBJECT_ID('tempdb..##TEMP_TABLE_NAME') IS NOT NULL
DROP TABLE ##TEMP_TABLE_NAME

How to Add a New Field to a Temp Table in T-SQL / SQL Server (ALTER TABLE)

Here is example T-SQL that illustrates how to add a new field to a global temp table. The code below adds a simple bit field (holds either 1 or 0) named FIELD1 to the temp table, declares it as NOT NULL (i.e., it must have a value) and then defaults the value to 0.

ALTER TABLE ##TEMP_TABLE
ADD FIELD1 Bit NOT NULL DEFAULT (0)

The following code changes the data type of an existing field in a global temp table. FIELD1 has its data type changed to NVARCHAR(2) and is declared as NOT NULL.

ALTER TABLE ##TEMP_TABLE
ALTER COLUMN FIELD1 NVARCHAR(20) NOT NULL;

Additional References:


How to Use a CASE Statement in T-SQL / SQL Server

The following information on the CASE statement is direct from Microsoft:

The CASE expression evaluates a list of conditions and returns one of multiple possible result expressions. The CASE expression has two formats:

  • The simple CASE expression compares an expression to a set of simple expressions to determine the result.
  • The searched CASE expression evaluates a set of Boolean expressions to determine the result.

Both formats support an optional ELSE argument.

CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.

Examples from Microsoft:

SELECT
ProductNumber,
Category =
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
 END,
Name
FROM Production.Product
ORDER BY ProductNumber;


SELECT
ProductNumber,
Name,
"Price Range" =
CASE
WHEN ListPrice =  0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
FROM Production.Product
ORDER BY ProductNumber ;

Here is a link to great post that highlights some of the unexpected results when using the CASE statement.


How to Use the Cast Function in T-SQL / SQL Server

When you need to convert a data field or expression to another data type then the cast function can be helpful. I typically have the need to take imported text fields and evaluate them as a datetime. The cast statement below helps me resolve this issue.

Select cast(txtOrder_Date as datetime) as Order_Date

This statement can also be used in a WHERE clause to filter the text as if it were a true datetime field/.

Where cast(txtOrder_Date as datetime)) between '20170101' and '20181231'

Furthermore, you can cast a literal string to an integer or decimal as needed.

Select cast(‘12345’ as int) as Integer_Field
Select cast(‘12345.12’’ as decimal (9,2)) as Decimal_Field

When your FIELDNAME is a text value, you can use the cast function to change its data type to an integer or decimal, and then sum the results. Here are a few examples I have had to use in the past with the sum function.

sum(cast(FIELDNAME as int)) as Sum_Overall_Qty

sum(cast(ltrim(rtrim(FIELDNAME2)) as decimal(38,2))) as Sum_Sales_Price

Additional Reference:


Using the REPLACE Function in T-SQL / SQL Server

The Replace function is useful when you need to replace all occurrences of one character or substring with another character or substring. The following select will replace the string ‘Anthony’ with ‘Tony’.

Select REPLACE(‘My name is Anthony’, ‘Anthony’, ‘Tony’);

REPLACE Function

Additional Reference:


How to Convert a Negative Text Number in Parenthesis Format to a Numeric Data Type (T-SQL / SQL Server)

I’ve found this particular expression useful when trying to convert a negative number in text format to a decimal value when the text is enclosed in parentheses; i.e., changing (123.45) to -123.45

It makes use of the REPLACE function to find the leading parenthesis and replace it with a negative sign. This first REPLACE is nested inside another REPLACE function in order to find the trailing parenthesis and replace

Select cast(replace(replace('(123.45)','(','-'),')','') as money);

REPLACE function2

You can also use the convert function to accomplish the same result. Below I used this line of code to sum the negative formatted text (represented by FIELD_NAME) by converting it to the money data type after replacing the parenthesis.

sum(convert(money,replace(replace(FIELD_NAME,'(','-'),')',''))) as Sum_Domestic_Price


COALESCE Function in T-SQL / SQL Server

The COALESCE function is very useful when replacing NULL field values with a substitute value. Per Microsoft, the COALESCE function evaluates in order a comma delimited list of expressions and returns the current value of the first expression that initially does not evaluate to NULL.

For example,

SELECT COALESCE(NULL, NULL, 'third_value', 'fourth_value');

returns the third value because the third value is the first value that is not null. I will use the COALESCE function at times to replace NULL values with 0 for use in calculations.

Select COALESCE(NULL_FIELD, 0)

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql?view=sql-server-2017

Advertisements

The Dos and Don’ts of Designing Efficient Tableau Dashboards

This following is a guest post contributed by Perceptive Analytics.

A dashboard is to a user what an assistant is to a boss. While an assistant helps manage multiple tasks for a boss, a dashboard helps manage multiple data sources for a user. Insights are only as good as the underlying data and dashboards are an excellent medium to provide those insights.

Dashboards provide “at-a-glance” views of key metrics which are relevant for business users to perform their tasks effectively. In other words, dashboards are an interactive form of reporting which provides users with consolidated views of different metrics to make impactful, data-driven decisions. A dashboard should speak on the creator’s behalf, acting as an expert providing actionable insights to its users. The dashboard should be self-sufficient when it comes to answering the question, “what can my data tell me?”

There are a plethora of tools available in the market for creating dashboards. However, a badly designed dashboard or incompatible (or wrong) tool can lead to hundreds of thousands of dollars in investment losses when accounting for inefficient time and effort spent by development and analysis teams. It becomes imperative for an organization to choose the right tool and have a step by step approach for dashboard development.

Currently, one of the top business intelligence tools available in the market is Tableau. It is used to create interactive dashboards for users. Tableau has been named a ‘Leader’ in the Gartner Magic Quadrant for six straight years in a row (Source – Tableau.com).

In this post, we will highlight a few best practices that you should follow when developing your Tableau dashboard. We will also talk about some of the pitfalls you should avoid while creating a Tableau dashboard.

We’ll divide the best practices into three different stages of dashboard development.

  1. Pre-Development: Ideation and Conceptualization
  2. Development
  3. Post Development: Maintenance

Ideation and Conceptualization

During the conceptualization and ideation stage, there are a few aspects that one should consider before starting to develop a dashboard.

1. Goal

Understand clearly why you are creating the dashboard in the first place. What is the end objective that you want to achieve via this dashboard? Is it automating a reporting process at month-end? Is it providing a better visualization to a complex calculation created in another platform?

Having a clear understanding of your dashboarding goal or objective keeps you focused and on the right track.

2. Audience

Keep in mind that your audience is a key part of creating a purposeful, impactful dashboard. The dashboard used by the CEO or other members of the C-suite will be very different from the dashboard used by business unit heads, which in turn will be very different from the dashboards used by branch managers. Thus, you need to consider who will use your dashboard and how will it be used?

For instance, a CEO is interested in key metrics at an overall organizational level like the overall financial and operational heath of the company. On the other hand, a procurement manager would be interested in the amount of material being procured from different vendors and their respective procurement costs. Having a GOAL in mind before development is essential because it helps identify the end user of the dashboard.

3. Key Performance Indicators (KPIs)

After thoroughly understanding the various stakeholder requirements, it is important to develop a list of KPIs for each user and/or department. Having the stakeholders sign-off on dashboard KPIs substantially reduces development and re-work time.

4. Data Sources

After achieving sign-off on KPIs, inventory the various data sources that are required for development. This step is important because each data source can potentially increase complexity and computing costs required to calculate the KPIs. It’s always better to only connect those data sources which contain relevant data.

5. Infrastructure

Storage and computation requirements should be taken into consideration commensurate with the dashboard’s degree of data volume and complexity. Having a right-sized backend infrastructure will improve dashboard performance considerably. Also, it is essential to understand the dashboard’s update frequency. Will the data be refreshed once a day? Is it going to be real-time? Having the answer to these questions will help generate infrastructure requirements that will prevent performance issues down the road.

Development

Once you have identified what needs to be presented on the dashboard and set up the infrastructure, it’s time to move to the second phase of dashboard development.

The following items should be considered during the development phase.

6. Design

Design is an important part of overall dashboard development. You should be very selective with the colors, fonts and font sizes that you employ. There is no rule book that establishes the right color or the right font for dashboard design; in our opinion, one should design with the company’s coloring scheme in mind.

This is a safe bet as it keeps the company’s brand identity intact, especially if the dashboard is accessible to external parties. Fonts should not be very light in color and the charts should not be very bright. Having a subtle color scheme that incorporates the brand’s identity resonates well with internal and external parties.

7. Visualization Impact

Identify the right type of visualization to create an impactful first glance for the users. Certain types of data points are better represented by certain types of visualizations. For instance, time trend analysis is usually represented on a line graph. A comparison of the same metric across different business lines are presented well via a heat map. Consider a sales dashboard where revenue and cost numbers for the current year should be presented as standalone numbers with a larger font size, while the historical trend analysis should be placed below.

8. Captions and Comments

Tableau provides users’ with the functionality to add captions and comments to visualizations. Bear in mind that you won’t be around all the time to explain what the different charts in the dashboard represent. Therefore, add relevant descriptions, comments and/or captions wherever it can be useful for the viewer.

Post Development: Maintenance

Once you have created the dashboard, there are additional aspects you should consider for effective and smooth dashboard operation.

9. Robust Testing

After creating the dashboard, conduct robust testing of the entire platform. Testing helps identify any bugs and deployment errors which if not rectified can lead to system failure or erratic results at a later stage.

10. Maintenance

This is the most ignored phase in the dashboard development lifecycle but it is a crucial phase. Once you have created a dashboard, proper maintenance should be conducted in terms of software updates, connections to databases and infrastructure requirements. If the volume of data increases at a fast pace, you will need to upgrade the storage and computing infrastructure accordingly so that the system doesn’t crash or become prohibitively slow.

Avoid the Following

Up to this point we have highlighted some of the best practices to consider while creating a dashboard. Now, let’s broach the aspects you should avoid while creating a dashboard.

1. Starting with a Complex Dashboard

Remember that creating a dashboard is a phased approach. Trying to develop an overly complicated dashboard in one phase may complicate things and led to project failure. The ideal approach is to inventory and prioritize all requirements and proceed with a phased approach. Start development with the highest priority requirements or KPIs and gradually move to the lower priority KPIs in subsequent phases.

2. Placing Too Many KPIs on a Single Chart

Although Tableau has the capability to handle multiple measures and dimensions in a single chart, you should be judicious while choosing the dimensions and measures you want to present in a single graph. For instance, placing revenue, expenses and profit margins in a single chart may be of value; while placing revenue and vendor details in the same chart may not be as valuable.

3. Allocating Too Little Time to Deployment and Maintenance

The appropriate amount of time, budget and resources should be allocated to each constituent phase of the deployment cycle (i.e., KPI identification, dashboard development, testing and maintenance).

We are sure that after reading this post, you have a better idea regarding what practices should be considered while developing a Tableau dashboard. The principles offered here are from a high level perspective. There may be other project nuances to consider in your specific endeavors. We would be happy to hear your thoughts and the best practices that you follow while creating a Tableau dashboard.

Author Bio

This article was contributed by Perceptive Analytics. Prudhvi Sai Ram, Saneesh Veetil and Chaitanya Sagar contributed to this article.

Perceptive Analytics provides Tableau Consulting, data analytics, business intelligence and reporting services to e-commerce, retail, healthcare and pharmaceutical industries. Our client roster includes Fortune 500 and NYSE listed companies in the USA and India.

Yet Another Market Basket Analysis in Tableau

This video represents part two in my Market Basket Analysis series.

The steps in the post were inspired by the book Tableau Unlimited written by former co-worker of mine, Chandraish Sinha. I wasn’t planning to construct another market basket analysis video but when I saw the approach outlined in his book, I felt like it warranted sharing with my readers and followers.

In this version we’ll use default Tableau Superstore data to show the relationship between sub-categories on an Order; all without using a self table join. The visualization and analysis is driven by a user selection parameter.

Once the user selects a sub-category, the bar chart visualization updates to reflect the number of associated sub-category items on the same order.

Sample Superstore Data 2

Watch the video and as always get out there and do some great things with your data!

Feel free to also check out Part 1 here where we create a simpler correlation matrix version that shows all the sub-category relationships in one visual.

 

 

 

Market Basket Analysis in Tableau

 

A favored analysis technique employed by retailers to help them understand the purchase behavior of their customers is the market basket analysis. When you log on to Amazon, most likely you’ve noticed the “Frequently Bought Together” section where Jeff Bezos and company would like to cross-sell you additional products based upon the purchase history of other people who have purchased the same item.

Market Basket Analysis influences how retailers institute sales promotions, loyalty programs, cross-selling/up-selling and even store layouts.

If a retailer observes that most people who purchase Coca-Cola also purchase a package of Doritos (I know they’re competing companies), then it may not make sense to discount both items at once as the consumer might have purchased the associated item at full price anyhow. Understanding the correlation between products is powerful information.

In this video, we’ll use Tableau Superstore data to perform a simple market basket analysis.

Sample Superstore Data 2

Watch the video and as always get out there and do some great things with your data.

Feel free to also check out Part 2 here where we’ll create an analysis driven by a user selection parameter.

When Corporate Layoffs Don’t Work

“When downsizing is a knee-jerk reaction, it has long-term costs. Employees and labor costs are rarely the true source of the problems facing an organization. Workers are more likely to be the source of innovation and renewal.” [1]

Case in Point: Circuit City Laid Off Employees for Over-performance

There were a combination of factors that lead to the demise of former electronics retailer Circuit City. A number of these reasons were self-inflicted wounds. The company located its stores in subprime locations, stopped selling appliances to cut warehouse storage and distribution costs and underinvested in its web presence at a time when consumer preferences were beginning to shift online.

However, the company’s biggest blunder was its decision to layoff its most experienced and knowledgeable sales persons while trying to compete in the competitive electronics retail marketplace. In March of 2007, Circuit City announced a scheme to layoff 3,400 hourly workers (roughly 8% of its workforce), while offering a severance package with the ability to reapply to former jobs at a reduced salary. Any reapplications had to occur after a mandatory 10 week cooling off period. Circuit City practiced genteelism by branding its cost cutting and de-skilling scheme a “wage management initiative”.

Management decided to staff its stores with fewer people, with fewer skills, making less money and expected this combination to yield long term positive results. As a result of the layoffs, Circuit City placed knowledgeable, experienced sales staff on a platter and served them to its main competitor, Best Buy. Additionally, where did Circuit City expect to find quality people who would work for a company that did not value loyalty, experience and wage increases?

“From a strategy perspective, customer-facing sales personnel would appear to be a core resource and potential differentiator for a consumer products retailer,” he [Kevin Clark, an assistant professor of management at Villanova School of Business] says. “Especially in an era of rapidly changing and more complex consumer electronics, knowledgeable sales personnel who are perceived by customers as ‘experts’ can be a source of competitive advantage.” [2]

Reportedly, “employees who were paid more than 51 cents above a set pay range for their departments were fired.” [3] However, solidifying the trope of senior executives reaping the gains without the pains, the CEO and Chairman of Circuit City received almost $10 million in various kinds of compensation for steering the company to its imperiled state. [4]

In under two years (i.e., November 2008), Circuit City announced it was going out of business. By laying off its highest paid hourly workers and replacing them with cheaper less skilled workers, in-store customer service levels plummeted which negatively impacted customer perception and sales.

Southwest Airlines Gets it Right

Waving flag of Southwest Airlines editorial 3D rendering

Treating employees as mere cogs and judging employees by costs and not by the overall value they create is self-defeating.

Some companies don’t understand that making workers happy leads to elevated productivity and higher retention levels. High employee morale should be table-stakes, instead it is a strategic key differentiator. Southwest Airlines has never had a layoff in its 47 plus years of existence. That’s laudable when you consider that airlines endured the fallout from 9/11 and the Great Recession (when oil prices spiked over $100 a barrel). As a well deserved consequence, Southwest Airlines routinely leads domestic airlines in customer satisfaction.

Consider this example of how Southwest Airlines treated its recruiting team during the global financial crisis:

“At one point, however, Southwest Airlines was staring at a tough time financially and it did ‘corporate redeployment’. It had 82 employees in the recruiting team. When the company put [in] a hiring freeze, it also wondered what to do with 82 of its employees in this particular team. The company utilised them for customer service. The result: Customer satisfaction went up as a result of this team’s enhanced skill set. When the economy recovered, the team went back to its original job; only this time, they had an additional skill set, which helped the company and the customers alike.” [1]

If you were in the airline industry would you rather work for Southwest Airlines or another domestic competitor (that I mercifully will not name) which embodies layoffs, labor strife and toxic mismanagement of employees?

The Negative Impact of Layoffs

There is a time and place for layoffs. However, more often than not, companies layoff employees during down times in the business cycle to simply lessen the impact on profits, not to avoid a collapse of the business. Against their own best interests, companies also announce layoffs during times of rising profits which causes their best people to head for greener pastures. Any expected cost savings are negated by lower productivity (when the best performers leave), lower innovation and a remaining demoralized workforce subjected to the negative effects of survivor syndrome (i.e., the feeling of guilt after seeing longtime co-workers discarded).

Additionally companies are impacted by “Brand equity costs—damage to the company’s brand as an employer of choice.” [1]. Sites like Glassdoor offer unfairly laid off employees the opportunity to share their sense of betrayal online which can significantly impact a company’s reputation.

Shortsighted management typically operates under the assumption that layoffs will positively impact shareholders. While financial analysts may cheer downsizing efforts, research indicates that layoffs have negative effects on share prices.

“A recent analysis of 41 studies covering 15,000 layoff announcements in more than a dozen countries over 31 years concluded that layoff announcements have an overall negative effect on stock-market prices. This remains true whatever the country, period of time or type of firm considered.”[1]

It should come as no surprise that Circuit City’s stock price fell 4% the day after the company pulled the plug on its most experienced employees. [5]

References:

[1] Employment Downsizing and its Alternatives. Retrieved from https://www.shrm.org/foundation/ourwork/initiatives/resources-from-past-initiatives/Documents/Employment%20Downsizing.pdf

[2] Circuit City plan: Bold strategy or black eye? NBC News. April 2, 2007. Retrieved from http://www.nbcnews.com/id/17857697/ns/business-careers/t/circuit-city-plan-bold-strategy-or-black-eye/

[3] Circuit City Cuts 3,400 ‘Overpaid’ Workers: Washington Post. March 29, 2007. Retrieved from http://www.washingtonpost.com/wp-dyn/content/article/2007/03/28/AR2007032802185.html

[4] Thousands Are Laid Off at Circuit City. What’s New?. New York Times. April 2, 2007 https://www.nytimes.com/2007/04/02/business/media/02carr.html

[5] It’s the Workforce, Stupid! The New Yorker. April 30, 2007. Retrieved from https://www.newyorker.com/magazine/2007/04/30/its-the-workforce-stupid

Use Clustering Analysis in Tableau to Uncover the Inherent Patterns in Your Data

This following is a guest post contributed by Perceptive Analytics.

Clustering:

Clustering is the grouping of similar observations or data points. Tableau enables clustering analysis by using the K-means model and a centroid approach. This model divides the data into k segments with a centroid in each segment. The centroid is the mean value of all points in that segment. The objective of this algorithm is to place centroids in segments such that the total sum of distances between centroids and points in their segments is as small as possible.

In this post we will demonstrate some of clustering’s practical applications using Tableau. To get started, download the dataset from this link.

Let’s get our hands dirty!

Examine the data-set, it contains data about different characteristics of flowers. Once the data is loaded into Tableau it will look like the screenshot below.

Picture1

Now let’s plot a visualization between petal width and length. Just drag and drop the petal width and length onto rows and columns as shown below.

Picture2

Here we see that there is only one data point as Tableau by default aggregates measures. We can “un-aggregate” the data with a click as shown below.

Picture3

Just go to the analysis tab in the menu and un-tick the aggregate measures option.

Picture4

Now we can observe a scatter plot of two measures. Let’s cluster these data points according to their species by navigating to the analytics pane as shown below.

Picture5

Drag and drop the cluster option on to the plot.

Picture6

Clusters are formed automatically, although there is an option to change the number of clusters. Users can also select the variables used for cluster generation, although Tableau uses the fields in the view to form the initial clusters.

Picture7

We can visually observe the clusters and Tableau provides a handy option that displays cluster statistics.

Picture8

Click on the “describe clusters” option to observe a summary and model description.

Picture9

The summary tab provides a high level overview of the variables used in the model and various sum of squares information. Let’s turn our attention to the models tab and the main generated statistics.

Picture10

F-Ratio:

The F-Ratio is used to determine if the expected values of a variable within groups differ from one another. It is the ratio of sum of squares (variances).

F= Between Group Variability/Within Group Variability

The greater the F-statistic, the better the corresponding variable in distinguishing between clusters.

P-Value:

In a statistical hypothesis test the P-value helps you determine the significance of your results. The p-value is the probability that the F-distribution of all possible values of the F-statistic takes on a value greater than the actual F-statistic for a variable. If the p-value falls below a specified significance level, then the null hypothesis can be rejected. The lesser the p-value, then more the expected values of the elements of the corresponding variable differ among clusters.

Tableau provides an option to save formed clusters into a group that can be used for subsequent analyses. Simply drag and drop the cluster from the marks pane to the dimensions section to save it as group.

Picture11

Tableau doesn’t allow clustering on these types of fields:

  • Dates
  • Bins
  • Sets
  • Table Calculations
  • Blended Calculations
  • Ad-hoc Calculations
  • Parameters
  • Generated Longitude and Latitude Values

Let’s look at another example using the default World Indicators data set that comes with Tableau. Open the sample workbook named World Indicators and explore the data regarding various countries.

Picture12

Try using different variables to form clusters. Use the model description to learn about the various countries based upon their clusters.

Picture13_1

Here it shows average life expectancy, average population above 65 years and urban population. These statistics provide insight into the composition of the particular clusters. We can see which countries comprise each cluster as shown below. Select any cluster and go to the “Show Me” tab and select text “Table” to view the names of each country present in a cluster.

Picture14

Conclusion:

We’ve only covered a few scenarios using clustering and how it aids with the segmentation of data. Clustering is an essential function of exploratory data mining. Keep exploring the results of cluster analysis by using different types of data sets. Keep Rocking!

“Happy Clustering!!”

 Author Bio

This article was contributed by Perceptive Analytics. Juturu Pavan, Prudhvi Sai Ram, Saneesh Veetil and Chaitanya Sagar contributed to this article.

Perceptive Analytics provides Tableau Consulting, data analytics, business intelligence and reporting services to e-commerce, retail, healthcare and pharmaceutical industries. Our client roster includes Fortune 500 and NYSE listed companies in the USA and India.

Use the Power BI Switch Function to Group By Date Ranges

In this latest video, I’ll explain how to use a handy DAX function in Power BI in order to group dates together for reporting. We’ll examine a dashboard that contains fields corresponding to purchase item, purchase date and purchase cost. We’ll then create a calculated column and use the SWITCH function in Power BI to perform our date grouping on the purchase date.

Watch the video to learn how to group dates into the following aging buckets, which can be customized to fit your specific need.

  • 0-15 Days
  • 16-30 Days
  • 31-59 Days
  • 60+ Days

If you are familiar with SQL, then you’ll recognize that the SWITCH function is very similar to the CASE statement; which is SQL’s way of handling IF/THEN logic.

Even though we’re creating a calculated column within Power BI itself, best practice is to push calculated fields to the source when possible. The closer calculated fields are to the underlying source data, the better the performance of the dashboard.