{"id":3846,"date":"2021-12-22T10:03:58","date_gmt":"2021-12-22T18:03:58","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3846"},"modified":"2021-12-22T10:03:59","modified_gmt":"2021-12-22T18:03:59","slug":"how-to-use-unpivot-columns-and-pivot-columns-with-examples","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3846","title":{"rendered":"How to Use Unpivot\u00a0Columns\u00a0and Pivot Columns with Examples"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"unpivotcolumns\">Unpivot&nbsp;columns&nbsp;<\/h2>\n\n\n\n<p>Unpivoting is a useful feature of Power BI. You can use this feature with data from any data source, but you would most often use it when importing data from Excel. The following example shows a sample Excel document with sales data.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-excel-data-multiple-columns-ss.png#lightbox\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-excel-data-multiple-columns-ss.png\" alt=\"Excel data that needs to be unpivoted\"\/><\/a><\/figure>\n\n\n\n<p>Though the data might initially make sense, it would be difficult to create a total of all sales combined from 2018 and 2019. Your goal would then be to use this data in Power BI with three columns:&nbsp;<strong>Month<\/strong>,&nbsp;<strong>Year<\/strong>, and&nbsp;<strong>SalesAmount<\/strong>.<\/p>\n\n\n\n<p>When you import the data into Power Query, it will look like the following image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-original-data-ss.png#lightbox\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-original-data-ss.png\" alt=\"Original Power Query data\"\/><\/a><\/figure>\n\n\n\n<p>Next, rename the first column to&nbsp;<strong>Month<\/strong>. This column was mislabeled because that header in Excel was labeling the 2018 and 2019 columns. Highlight the 2018 and 2019 columns, select the&nbsp;<strong>Transform<\/strong>&nbsp;tab in Power Query, and then select&nbsp;<strong>Unpivot<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-unpivot-ss.png#lightbox\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-unpivot-ss.png\" alt=\"Unpivot results in Power Query\"\/><\/a><\/figure>\n\n\n\n<p>You can rename the&nbsp;<strong>Attribute<\/strong>&nbsp;column to&nbsp;<strong>Year<\/strong>&nbsp;and the&nbsp;<strong>Value<\/strong>&nbsp;column to&nbsp;<strong>SalesAmount<\/strong>.<\/p>\n\n\n\n<p>Unpivoting streamlines the process of creating DAX measures on the data later. By completing this process, you have now created a simpler way of slicing the data with the&nbsp;<strong>Year<\/strong>&nbsp;and&nbsp;<strong>Month<\/strong>&nbsp;columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"pivotcolumns\">Pivot&nbsp;columns&nbsp;<\/h2>\n\n\n\n<p>If the data that you are shaping&nbsp;is flat (in other words, it&nbsp;has&nbsp;lot of detail but is&nbsp;not&nbsp;organized or grouped in any way), the lack of structure can complicate your ability to identify patterns in the data.<\/p>\n\n\n\n<p>You can use the&nbsp;<strong>Pivot Column<\/strong>&nbsp;feature to&nbsp;convert your flat data into a&nbsp;table that contains an aggregate value for each unique value in a column. For example, you might&nbsp;want to use this feature&nbsp;to summarize data&nbsp;by using different math functions such as&nbsp;<strong>Count<\/strong>,&nbsp;<strong>Minimum<\/strong>,&nbsp;<strong>Maximum<\/strong>,&nbsp;<strong>Median<\/strong>,&nbsp;<strong>Average<\/strong>, or&nbsp;<strong>Sum<\/strong>.&nbsp;<\/p>\n\n\n\n<p>In&nbsp;the&nbsp;SalesTarget&nbsp;example,&nbsp;you can&nbsp;pivot&nbsp;the&nbsp;columns to get the quantity of product subcategories in each&nbsp;product&nbsp;category.<\/p>\n\n\n\n<p>On the&nbsp;<strong>Transform<\/strong>&nbsp;tab, select\u202f<strong>Transform\u202f&gt;\u202fPivot Columns<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-pivot-column-ssm.png#lightbox\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-pivot-column-ssm.png\" alt=\"Pivot Column\"\/><\/a><\/figure>\n\n\n\n<p>On the&nbsp;<strong>Pivot Column<\/strong>&nbsp;window that displays, select a column from the&nbsp;<strong>Values Column<\/strong>&nbsp;list, such as&nbsp;<strong>Subcategory name<\/strong>.&nbsp;Expand\u202fthe advanced options&nbsp;and select&nbsp;an&nbsp;option from the\u202f<strong>Aggregate Value Function<\/strong>&nbsp;list, such as&nbsp;<strong>Count (All)<\/strong>, and then select&nbsp;<strong>OK<\/strong>.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-aggregate-value-function-ssm.png#lightbox\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-aggregate-value-function-ssm.png\" alt=\"Aggregate value function\"\/><\/a><\/figure>\n\n\n\n<p>The following image illustrates how the&nbsp;<strong>Pivot Column<\/strong>&nbsp;feature changes the way that the data is organized.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-pivot-column-feature-display-data-ssm.png#lightbox\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/media\/02-pivot-column-feature-display-data-ssm.png\" alt=\"pivot column feature changes how data is organized\"\/><\/a><\/figure>\n\n\n\n<p>Power Query Editor records&nbsp;all steps that you take to shape your data, and the list of steps are shown in the&nbsp;<strong>Query Settings<\/strong>&nbsp;pane.&nbsp;If you have made all the required changes, select&nbsp;<strong>Close &amp; Apply<\/strong>&nbsp;to close&nbsp;Power&nbsp;Query Editor and apply your changes to your data model. However,&nbsp;before you&nbsp;select&nbsp;<strong>Close &amp; Apply<\/strong>, you can take further steps&nbsp;to clean up&nbsp;and transform&nbsp;your data in&nbsp;Power&nbsp;Query Editor.&nbsp;These additional steps are covered later in this module.&nbsp;<\/p>\n\n\n\n<p>Ref: <a href=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/clean-data-power-bi\/2-shape-data\">Shape the initial data &#8211; Learn | Microsoft Docs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unpivot&nbsp;columns&nbsp; Unpivoting is a useful feature of Power BI. You can use this feature with data from any data source, but you would most often use it when importing data from Excel. The following example shows a sample Excel document with sales data. Though the data might initially make sense, it would be difficult to <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3846\">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],"tags":[999,998,996,997],"class_list":["post-3846","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-power-bi","tag-pivot-columns","tag-unpivot-columns","tag-unpivot-columns-and-pivot-columns","tag-unpivot-columns-and-pivot-columns-power-bi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3846","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=3846"}],"version-history":[{"count":2,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3846\/revisions"}],"predecessor-version":[{"id":3854,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3846\/revisions\/3854"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}