{"id":3844,"date":"2022-03-10T11:17:53","date_gmt":"2022-03-10T19:17:53","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3844"},"modified":"2022-03-10T11:17:55","modified_gmt":"2022-03-10T19:17:55","slug":"how-to-fix-data-type-errors-in-power-bi","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3844","title":{"rendered":"How to Fix &#8220;Data type errors&#8221; in Power BI"},"content":{"rendered":"\n<p>Sometimes, when you import data into Power BI, the columns appear blank. This situation happens because of an error in interpreting the data type in Power BI. The resolution to this error is unique to the data source. For instance, if you are importing data from SQL Server and see blank columns, you could try to convert to the correct data type in the query.<\/p>\n\n\n\n<p>Instead of using this query:<\/p>\n\n\n\n<p><code>SELECT CustomerPostalCode FROM Sales.Customers<\/code><\/p>\n\n\n\n<p>Use this query:<\/p>\n\n\n\n<p><code>SELECT CAST(CustomerPostalCode as varchar(10)) FROM Sales.Customers<\/code><\/p>\n\n\n\n<p>By specifying the correct type at the data source, you eliminate many of these common data source errors.<\/p>\n\n\n\n<p>Read below for more detail if needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to SQL Server&nbsp;<code>CAST()<\/code>&nbsp;function<\/h2>\n\n\n\n<p>Let\u2019s see the following query:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT 1 + '1' AS result;<\/code><small>Code language: PHP (php)<\/small><\/pre>\n\n\n\n<p>It returns 2 as a number:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result\n-----------\n2\n\n(1 row affected)<\/code><\/pre>\n\n\n\n<p>In this statement, SQL Server implicitly converts the character string&nbsp;<code>'1'<\/code>&nbsp;to the number&nbsp;<code>1<\/code>.<\/p>\n\n\n\n<p>When you use two values with different&nbsp;data types, SQL Server will try to convert the lower data type to the higher one before it can process the calculation. This is known as an implicit conversion in SQL Server.<\/p>\n\n\n\n<p>In contrast to implicit conversions, we have explicit conversions where you call the&nbsp;<code>CAST()<\/code>&nbsp;function to explicitly convert a value of one type to another:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT 1 + CAST(1 AS INT) result;<\/code><small>Code language: PHP (php)<\/small><\/pre>\n\n\n\n<p>The syntax of the&nbsp;<code>CAST()<\/code>&nbsp;function is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>CAST ( expression AS target_type [ ( length ) ] )  <\/code><small>Code language: CSS (css)<\/small><\/pre>\n\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>expression<\/code>&nbsp;can be a literal value or a valid expression of any type that will be converted.<\/li><li><code>target_type<\/code>&nbsp;is the target data type to which you want to convert the expression. It includes&nbsp;<code>INT<\/code>,&nbsp;<code>BIT<\/code>,&nbsp;<code>SQL_VARIANT<\/code>, etc. Note that it cannot be an alias data type.<\/li><li><code>length<\/code>&nbsp;is an optional integer that specifies the length of the target type. The&nbsp;<code>length<\/code>&nbsp;defaults to 30.<\/li><\/ul>\n\n\n\n<p>The&nbsp;<code>CAST()<\/code>&nbsp;function returns the expression converted to the target data type.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SQL Server&nbsp;<code>CAST()<\/code>&nbsp;function examples<\/h2>\n\n\n\n<p>Let\u2019s take some examples of using the&nbsp;<code>CAST()<\/code>&nbsp;function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A) Using the&nbsp;<code>CAST()<\/code>&nbsp;function to convert a decimal to an integer example<\/h3>\n\n\n\n<p>This example uses the&nbsp;<code>CAST()<\/code>&nbsp;function to convert the decimal number&nbsp;<code>5.95<\/code>&nbsp;to an integer:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT CAST(5.95 AS INT) result;<\/code><small>Code language: CSS (css)<\/small><\/pre>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result\n-----------\n5\n\n(1 row affected)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">B) Using the&nbsp;<code>CAST()<\/code>&nbsp;function to convert a decimal to another decimal with different length<\/h3>\n\n\n\n<p>The following example uses the&nbsp;<code>CAST()<\/code>&nbsp;function to convert the decimal number 5.95 to another&nbsp;decimal number&nbsp;with the zero scale:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT CAST(5.95 AS DEC(3,0)) result;<\/code><small>Code language: CSS (css)<\/small><\/pre>\n\n\n\n<p>The output is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result\n-------\n6<\/code><\/pre>\n\n\n\n<p>When you convert a value of the data types in different places, SQL Server will return a truncated result or a rounded value based on the following rules:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>From Data Type<\/th><th>To Data Type<\/th><th>Behavior<\/th><\/tr><\/thead><tbody><tr><td>numeric<\/td><td>numeric<\/td><td>Round<\/td><\/tr><tr><td>numeric<\/td><td>int<\/td><td>Truncate<\/td><\/tr><tr><td>numeric<\/td><td>money<\/td><td>Round<\/td><\/tr><tr><td>money<\/td><td>int<\/td><td>Round<\/td><\/tr><tr><td>money<\/td><td>numeric<\/td><td>Round<\/td><\/tr><tr><td>float<\/td><td>int<\/td><td>Truncate<\/td><\/tr><tr><td>float<\/td><td>numeric<\/td><td>Round<\/td><\/tr><tr><td>float<\/td><td>datetime<\/td><td>Round<\/td><\/tr><tr><td>datetime<\/td><td>int<\/td><td>Round<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">C) Using the&nbsp;<code>CAST()<\/code>&nbsp;function to convert a string to a datetime value example<\/h3>\n\n\n\n<p>This example uses the&nbsp;<code>CAST()<\/code>&nbsp;function to convert the string&nbsp;<code>'2019-03-14'<\/code>&nbsp;to a&nbsp;DateTime:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT \n    CAST('2019-03-14' AS DATETIME) result;<\/code><small>Code language: PHP (php)<\/small><\/pre>\n\n\n\n<p>The output is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>result\n-----------------------\n2019-03-14 00:00:00.000\n\n(1 row affected)<\/code><small>Code language: CSS (css)<\/small><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">D) Using&nbsp;<code>CAST()<\/code>&nbsp;function with arithmetic operators<\/h3>\n\n\n\n<p>We will use the&nbsp;<code>sales.orders<\/code>&nbsp;and&nbsp;<code>sales.order_items<\/code>&nbsp;tables from the&nbsp;sample database&nbsp;for the demonstration:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/orders-order_items.png\" alt=\"Sample Tables\" class=\"wp-image-139\"\/><\/figure>\n\n\n\n<p>The following statement uses the&nbsp;<code>CAST()<\/code>&nbsp;function to convert the monthly sales in 2017 to integer values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT \n    MONTH(order_date) month, \n    CAST(SUM(quantity * list_price * (1 - discount)) AS INT) amount\nFROM sales.orders o\n    INNER JOIN sales.order_items i ON o.order_id = i.order_id\nWHERE \n    YEAR(order_date) = 2017\nGROUP BY \n    MONTH(order_date)\nORDER BY \n    month;<\/code><small>Code language: PHP (php)<\/small><\/pre>\n\n\n\n<p>The following picture shows the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CAST-Function-example.png\" alt=\"SQL Server CAST Function example\" class=\"wp-image-1568\"\/><\/figure>\n\n\n\n<p>In this tutorial, you have learned how to use the SQL Server&nbsp;<code>CAST()<\/code>&nbsp;function to convert a value from one type to another.<\/p>\n\n\n\n<p>Ref: https:\/\/www.sqlservertutorial.net\/sql-server-system-functions\/sql-server-cast-function\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, when you import data into Power BI, the columns appear blank. This situation happens because of an error in interpreting the data type in Power BI. The resolution to this error is unique to the data source. For instance, if you are importing data from SQL Server and see blank columns, you could try <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3844\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,621,625],"tags":[995,993,994],"class_list":["post-3844","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-power-bi","category-sql","tag-fix-data-type-errors","tag-fix-data-type-errors-in-power-bi","tag-sql-cast"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3844"}],"version-history":[{"count":2,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3844\/revisions"}],"predecessor-version":[{"id":4072,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3844\/revisions\/4072"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}