{"id":3568,"date":"2021-09-10T10:06:51","date_gmt":"2021-09-10T17:06:51","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3568"},"modified":"2021-09-10T10:06:53","modified_gmt":"2021-09-10T17:06:53","slug":"power-bi-calculatetable-function-dax","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3568","title":{"rendered":"Power BI: CALCULATETABLE function DAX"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">CALCULATETABLE function is a power bi filter function in DAX that evaluates a table expression in a context modified by the given filters. It returns a table of values.<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">CALCULATETABLE(&lt;expression&gt;,&lt;filter1&gt;,&lt;filter2&gt;,\u2026)<\/pre>\n\n\n\n<p>&lt;expression&gt; is a table expression to be evaluated. A measure can not be used as expression.<\/p>\n\n\n\n<p>&lt;filter1&gt;,&lt;filter2&gt;.. is a Boolean expression or a table expression that defines a filter.<\/p>\n\n\n\n<p>Lets look at an example of&nbsp; DAX using CALCULATETABLE&nbsp; function.<\/p>\n\n\n\n<p>Here we have sample table named&nbsp;<strong>TransactionHistory<\/strong>&nbsp;which contains product details as given below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/sqlskull.com\/wp-content\/uploads\/2020\/06\/ds.png?resize=299%2C313&amp;ssl=1\" alt=\"\" class=\"wp-image-5481\"\/><\/figure>\n\n\n\n<p>Lets see the behaviour of CALCULATETABLE function.<\/p>\n\n\n\n<p><strong>Basic use of CALCULATETABLE function<\/strong><\/p>\n\n\n\n<p>Lets create a table named&nbsp;<strong>calctable<\/strong>&nbsp;to filter a table records for quantity &gt;1 using following DAX.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CalCtable = CALCULATETABLE(TransactionHistory,TransactionHistory[Quantity] &gt;1)<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/sqlskull.com\/wp-content\/uploads\/2020\/06\/Dax_Calculate.png?resize=680%2C69&amp;ssl=1\" alt=\"\" class=\"wp-image-5478\"\/><\/figure>\n\n\n\n<p>Once you commit DAX function, a table named as&nbsp;<strong>calctable<\/strong>&nbsp;is created in data model.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/sqlskull.com\/wp-content\/uploads\/2020\/06\/calctable.png?resize=226%2C194&amp;ssl=1\" alt=\"\" class=\"wp-image-5479\"\/><\/figure>\n\n\n\n<p>This table is just a copy of actual table&nbsp;<strong>TransactionHistory<\/strong>&nbsp;but contains only those products for which a quantity value is greater than1.<\/p>\n\n\n\n<p>Lets drag a table fields into a table visual, and you can see all the products having a quantity greater than 1.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/sqlskull.com\/wp-content\/uploads\/2020\/06\/calulatetbl.png?resize=421%2C237&amp;ssl=1\" alt=\"\" class=\"wp-image-5483\"\/><\/figure>\n\n\n\n<p>You can also compare both tables result sets, you can clearly see that&nbsp;<strong>calctable<\/strong>&nbsp;contains only those products for which quantity value is greater than 1.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/sqlskull.com\/wp-content\/uploads\/2020\/06\/compare.png?resize=577%2C312&amp;ssl=1\" alt=\"\" class=\"wp-image-5484\"\/><\/figure>\n\n\n\n<p>So, using CALCULATETABLE function you can sort table records based on any specific condition.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">CALULATETABLE function with Summarize function<\/h4>\n\n\n\n<p>Lets modify the above DAX calculation and returns only specific columns&nbsp;<strong>ProductID<\/strong>,&nbsp;<strong>Quantity<\/strong>, and&nbsp;<strong>TransactionDate<\/strong>&nbsp;only.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CalCtable =\nCALCULATETABLE (\nSUMMARIZE (\nTransactionHistory,\nTransactionHistory[ProductId],\nTransactionHistory[Quantity],\nTransactionHistory[TransactionDate]\n),\nTransactionHistory[Quantity] &gt; 1\n)<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/sqlskull.com\/wp-content\/uploads\/2020\/06\/cal.png?resize=598%2C248&amp;ssl=1\" alt=\"\" class=\"wp-image-5486\"\/><\/figure>\n\n\n\n<p>Once you commit the DAX, you will get only those columns that you want to see as shown in below screenshot.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/sqlskull.com\/wp-content\/uploads\/2020\/06\/table.png?resize=187%2C376&amp;ssl=1\" alt=\"\" class=\"wp-image-5488\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Using CALULATETABLE function within measures<\/h4>\n\n\n\n<p>You can also use CALCULATETABLE function within measure based on requirement so rather than creating a separate filtered table it can be used inside any measure.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SumofQuantity&gt;1 =\n\nSUMX(\nCALCULATETABLE (\nTransactionHistory,\nTransactionHistory[Quantity] &gt; 1\n),TransactionHistory[Quantity])<\/pre>\n\n\n\n<p>Now once you commit the DAX, and drag the measure into card visual.<\/p>\n\n\n\n<p>As you can see, it returns a total quantity sum of products for which quantity is greater than1 that is 21 as shown in below screenshot.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/sqlskull.com\/wp-content\/uploads\/2020\/06\/calulate5.png?resize=469%2C439&amp;ssl=1\" alt=\"\" class=\"wp-image-5492\"\/><figcaption>Ref: https:\/\/sqlskull.com\/2020\/06\/09\/calculatetable\/<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>CALCULATETABLE function is a power bi filter function in DAX that evaluates a table expression in a context modified by the given filters. It returns a table of values. CALCULATETABLE(&lt;expression&gt;,&lt;filter1&gt;,&lt;filter2&gt;,\u2026) &lt;expression&gt; is a table expression to be evaluated. A measure can not be used as expression. &lt;filter1&gt;,&lt;filter2&gt;.. is a Boolean expression or a table expression <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3568\">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":[847],"class_list":["post-3568","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-power-bi","tag-calculatetable"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3568","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=3568"}],"version-history":[{"count":1,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3568\/revisions"}],"predecessor-version":[{"id":3569,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3568\/revisions\/3569"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}