{"id":3244,"date":"2025-04-22T07:02:40","date_gmt":"2025-04-22T07:02:40","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/04\/22\/601823-2\/"},"modified":"2025-04-22T07:02:40","modified_gmt":"2025-04-22T07:02:40","slug":"601823-2","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/04\/22\/601823-2\/","title":{"rendered":"How to Write Queries for Tabular Models with DAX"},"content":{"rendered":"<p>    How to Write Queries for Tabular Models with DAX<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<h2 class=\"wp-block-heading\"><mdspan datatext=\"el1745276188880\" class=\"mdspan-comment\">Introduction<\/mdspan><\/h2>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/dax.guide\/st\/evaluate\/\"><code>EVALUATE<\/code><\/a> is the statement to query tabular models.<\/p>\n<p class=\"wp-block-paragraph\">Unfortunately, knowing SQL or any other query language doesn\u2019t help as <code>EVALUATE<\/code> follows a different concept.<\/p>\n<p class=\"wp-block-paragraph\"><code>EVALUATE<\/code> has only two \u201cParameters\u201d:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">A table to show<\/li>\n<li class=\"wp-block-list-item\">A sort order (<a href=\"https:\/\/dax.guide\/st\/order-by\/\"><code>ORDER BY<\/code><\/a>)<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">You can pass a third parameter (<a href=\"https:\/\/dax.guide\/st\/start-at\/\"><code>START AT<\/code><\/a>), but this one is rarely used.<\/p>\n<p class=\"wp-block-paragraph\">However, a DAX query can have additional components. Those are defined in the <code>DEFINE<\/code> section of the query.<br \/>In the <code>DEFINE<\/code> section, you can define Variables and local Measures.<br \/>You can use the <code>COLUMN<\/code> and <code>TABLE<\/code> keywords in <code>EVALUATE<\/code>, which I have never used until now.<\/p>\n<p class=\"wp-block-paragraph\">Let\u2019s start with some simple Queries and add some additional logic step by step.<\/p>\n<p class=\"wp-block-paragraph\">However, first, let\u2019s discuss the Tools.<\/p>\n<h2 class=\"wp-block-heading\">Querying tools<\/h2>\n<p class=\"wp-block-paragraph\">There are two possibilities for querying a tabular model:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Using the <a href=\"https:\/\/learn.microsoft.com\/en-us\/power-bi\/transform-model\/dax-query-view\">DAX query view<\/a> in Power BI Desktop.<\/li>\n<li class=\"wp-block-list-item\">Using <a href=\"https:\/\/www.sqlbi.com\/tools\/dax-studio\/\">DAX Studio<\/a>.<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">Of course, the syntax is the same.<\/p>\n<p class=\"wp-block-paragraph\">I prefer DAX Studio over DAX query view. It offers advanced features not available in Power BI Desktop, such as performance statistics with Server Timing and displaying the model\u2019s metrics.<\/p>\n<p class=\"wp-block-paragraph\">On the other hand, the DAX query view in Power BI Desktop provides the option to apply changes in a Measure back to the model directly after I have modified them in the query.<\/p>\n<p class=\"wp-block-paragraph\">I will discuss this later when I explain more about the possibility of defining local measures. You can read the MS documentation on modifying Measures directly from the DAX query view.<\/p>\n<p class=\"wp-block-paragraph\">You can find a link to the documentation in the References section below.<\/p>\n<p class=\"wp-block-paragraph\">In this article, I will use DAX Studio only.<\/p>\n<h2 class=\"wp-block-heading\">Simple queries<\/h2>\n<p class=\"wp-block-paragraph\">The simplest query is to get all columns and all rows from a table:<\/p>\n<p class=\"has-surface-secondary-background-color has-background wp-block-paragraph\">EVALUATE<br \/>\u00a0\u00a0\u00a0\u00a0 Customer<\/p>\n<p class=\"wp-block-paragraph\">This query returns the entire Customer table:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-1.jpg?ssl=1\" alt=\"\" class=\"wp-image-601825\"><figcaption class=\"wp-element-caption\">Figure 1 \u2013 Simple query on the Customer table. The number of returned rows can be found in the bottom right corner of DAX Studio, as well as the position of the cursor in the Query (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">If I want to query the result of a single value, for example, a Measure, I must define a table, as <code>EVALUATE<\/code> requires a table as input.<\/p>\n<p class=\"wp-block-paragraph\">Curly brackets do this.<\/p>\n<p class=\"wp-block-paragraph\">Therefore, the query for a Measure looks like this:<\/p>\n<p class=\"has-surface-secondary-background-color has-background wp-block-paragraph\"><code>EVALUATE&lt;br&gt;\u00a0\u00a0\u00a0\u00a0 { [Online Customer Count]}<\/code><\/p>\n<p class=\"wp-block-paragraph\">The result is one single value:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-64.png?ssl=1\" alt=\"\" class=\"wp-image-601824\"><figcaption class=\"wp-element-caption\">Figure 2 \u2013 Querying a Measure with Curly brackets to define a table (Figure by the Author)<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\">Get only the first 10 rows<\/h2>\n<p class=\"wp-block-paragraph\">It\u2019s not unusual to have tables with thousands or even millions of rows.<\/p>\n<p class=\"wp-block-paragraph\">So, what if I want to see the first 10 rows to glimpse the data inside the table?<\/p>\n<p class=\"wp-block-paragraph\">For this, <a href=\"https:\/\/dax.guide\/topn\/\"><code>TOPN()<\/code><\/a> does the trick.<\/p>\n<p class=\"wp-block-paragraph\"><code>TOPN()<\/code> accepts a sorting order. However, it doesn\u2019t sort the data; it only looks at the values and gets the first or last rows according to the sorting criteria.<\/p>\n<p class=\"wp-block-paragraph\">For example, let\u2019s get the ten customers with the latest birthdate (Descending order):<\/p>\n<p class=\"has-surface-secondary-background-color has-background wp-block-paragraph\"><code>EVALUATE&lt;br&gt;\u00a0\u00a0\u00a0\u00a0TOPN(10&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,Customer&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,Customer[BirthDate]&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,DESC)<\/code><\/p>\n<p class=\"wp-block-paragraph\">This is the result:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-3.jpg?ssl=1\" alt=\"\" class=\"wp-image-601827\"><figcaption class=\"wp-element-caption\">Figure 3 \u2013 Here, the result of TOPN() is used to get the top 10 rows by birthdate. See, that 11 rows are returned, as there are customers with the same birthdate (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/dax.guide\/\">DAX.guide<\/a> article on <a href=\"https:\/\/dax.guide\/topn\/\"><code>TOPN()<\/code><\/a> states the following about ties in the resulting data:<\/p>\n<p class=\"wp-block-paragraph\"><em>If there is a tie in <code>OrderBy_Expression<\/code> values at the N-th row of the table, then all tied rows are returned. Then, when there are ties at the N-th row, the function might return more than n rows.<\/em><\/p>\n<p class=\"wp-block-paragraph\">This explains why we get 11 rows from the query. When sorting the output, we will see the tie for the last value, November 26, 1980.<\/p>\n<p class=\"wp-block-paragraph\">To have the result sorted by the Birthdate, you must add an <code>ORDER BY<\/code>:<\/p>\n<p class=\"has-surface-secondary-background-color has-background wp-block-paragraph\"><code>EVALUATE&lt;br&gt;\u00a0\u00a0\u00a0\u00a0TOPN(10&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,Customer&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,Customer[BirthDate]&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,DESC)&lt;br&gt;\u00a0\u00a0\u00a0\u00a0ORDER BY Customer[BirthDate] DESC<\/code><\/p>\n<p class=\"wp-block-paragraph\">And here, the result:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-4.jpg?ssl=1\" alt=\"\" class=\"wp-image-601828\"><figcaption class=\"wp-element-caption\">Figure 4 \u2013 Result of the same TOPN() query as before, but with an ORDER BY to sort the output of the query by the Birthday descending (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Now, the ties at the last two rows are clearly visible.<\/p>\n<h2 class=\"wp-block-heading\">Adding columns<\/h2>\n<p class=\"wp-block-paragraph\">Usually, I want to select only a subset of all columns in a table.<\/p>\n<p class=\"wp-block-paragraph\">If I query multiple columns, I will only get the distinct values of the existing combination of values in both columns. This differs from other query languages, like SQL, where I must explicitly define that I want to remove duplicates, for example with <code>DISTINCT<\/code>.<\/p>\n<p class=\"wp-block-paragraph\">DAX has multiple functions to get a subset of columns from a table:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\"><a href=\"https:\/\/dax.guide\/addcolumns\/\"><code>ADDCOLUMNS()<\/code><\/a><\/li>\n<li class=\"wp-block-list-item\"><a href=\"https:\/\/dax.guide\/selectcolumns\/\"><code>SELECTCOLUMNS()<\/code><\/a><\/li>\n<li class=\"wp-block-list-item\"><a href=\"https:\/\/dax.guide\/summarize\/\"><code>SUMMARIZE()<\/code><\/a><\/li>\n<li class=\"wp-block-list-item\"><a href=\"https:\/\/dax.guide\/summarizecolumns\/\"><code>SUMMARIZECOLUMNS()<\/code><\/a><\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Of these four, <a href=\"https:\/\/dax.guide\/summarizecolumns\/\"><code>SUMMARIZECOLUMNS()<\/code><\/a> is the most useful for general purposes.<\/p>\n<p class=\"wp-block-paragraph\">When trying these four functions, be cautious when using <a href=\"https:\/\/dax.guide\/addcolumns\/\"><code>ADDCOLUMNS()<\/code><\/a>, as this function can result in unexpected results.<\/p>\n<p class=\"wp-block-paragraph\">Read <a href=\"https:\/\/www.sqlbi.com\/articles\/best-practices-using-summarize-and-addcolumns\">this SQLBI article<\/a> for more details.<\/p>\n<p class=\"wp-block-paragraph\">OK, how can we use <code>SUMMARIZECOLUMNS()<\/code> in a query:<\/p>\n<p class=\"has-surface-secondary-background-color has-background wp-block-paragraph\"><code>EVALUATE&lt;br&gt;\u00a0\u00a0\u00a0\u00a0SUMMARIZECOLUMNS('Customer'[CustomerType])<\/code><\/p>\n<p class=\"wp-block-paragraph\">This is the result:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-2.jpg?ssl=1\" alt=\"\" class=\"wp-image-601826\"><figcaption class=\"wp-element-caption\">Figure 5 \u2013 Getting the Distinct values of CustomerType with SUMMARIZECOLUMNS() (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As described above, we get only the distinct values of the CustomerType column.<\/p>\n<p class=\"wp-block-paragraph\">When querying multiple columns, the result is the distinct combinations of the existing data:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-6.jpg?ssl=1\" alt=\"\" class=\"wp-image-601829\"><figcaption class=\"wp-element-caption\">Figure 6 \u2013 Getting multiple columns (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Now, I can add a Measure to the Query, to get the number of Customers per combination:<\/p>\n<p class=\"has-surface-secondary-background-color has-background wp-block-paragraph\"><code>EVALUATE&lt;br&gt;\u00a0\u00a0\u00a0\u00a0SUMMARIZECOLUMNS('Customer'[CustomerType]&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,Customer[Gender]&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,\"Number of Customers\", [Online Customer Count])<\/code><\/p>\n<p class=\"wp-block-paragraph\">As you can see, a label must be added for the Measure. This applies to all calculated columns added to a query.<\/p>\n<p class=\"wp-block-paragraph\">This is the result of the query above:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-5.jpg?ssl=1\" alt=\"\" class=\"wp-image-601830\"><figcaption class=\"wp-element-caption\">Figure 7 \u2013 Result of the query with multiple columns and a Measure (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">You can add as many columns and measures as you need.<\/p>\n<h1 class=\"wp-block-heading\">Adding filters<\/h1>\n<p class=\"wp-block-paragraph\">The function <a href=\"https:\/\/dax.guide\/calculate\/\"><code>CALCULATE()<\/code><\/a> is well-known for adding filters to a Measure.<\/p>\n<p class=\"wp-block-paragraph\">For queries, we can use the <a href=\"https:\/\/dax.guide\/calculatetable\/\"><code>CALCULATETABLE()<\/code><\/a> function, which works like <code>CALCULATE()<\/code>; only the first argument must be a table.<\/p>\n<p class=\"wp-block-paragraph\">Here, the same query as before, only that the Customer-Type is filtered to include only \u201cPersons\u201d:<\/p>\n<p class=\"has-surface-secondary-background-color has-background wp-block-paragraph\"><code>EVALUATE&lt;br&gt;CALCULATETABLE(&lt;br&gt;\u00a0\u00a0\u00a0\u00a0SUMMARIZECOLUMNS('Customer'[CustomerType]&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,Customer[Gender]&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,\"Number of Customers\", [Online Customer Count])&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,'Customer'[CustomerType] = \"Person\"&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)<\/code><\/p>\n<p class=\"wp-block-paragraph\">Here, the result:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-7.jpg?ssl=1\" alt=\"\" class=\"wp-image-601831\"><figcaption class=\"wp-element-caption\">Figure 8 \u2013 Query and result to filter the Customer-Type to \u201cPerson\u201d (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">It is possible to add filters directly to <code>SUMMARIZECOLUMNS()<\/code>. The queries generated by Power BI use this approach. But it is much more complicated than using <code>CALCULATETABLE()<\/code>.<\/p>\n<p class=\"wp-block-paragraph\">You can find examples for this approach on the <a href=\"https:\/\/dax.guide\/\">DAX.guide<\/a> page for <a href=\"https:\/\/dax.guide\/summarizecolumns\/\"><code>SUMMARIZECOLUMNS()<\/code><\/a>.<\/p>\n<p class=\"wp-block-paragraph\">Power BI uses this approach when building queries from the visualisations. You can get the queries from the Performance Analyzer in Power BI Desktop.<\/p>\n<p class=\"wp-block-paragraph\">You can read <a href=\"https:\/\/medium.com\/towards-data-science\/how-to-get-performance-data-from-power-bi-with-dax-studio-b7f11b9dd9f9\">my piece about collecting performance data<\/a> to learn how to use Performance Analyzer to get a query from a Visual.<\/p>\n<p class=\"wp-block-paragraph\">You can also read the Microsoft documentation linked below, which explains this.<\/p>\n<h2 class=\"wp-block-heading\">Defining Local Measures<\/h2>\n<p class=\"wp-block-paragraph\">From my point of view, this is one of the most powerful features of DAX queries:<\/p>\n<p class=\"wp-block-paragraph\">Adding Measures local to the query.<\/p>\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/dax.guide\/st\/define\/\"><code>DEFINE<\/code><\/a> statement exists for this purpose.<\/p>\n<p class=\"wp-block-paragraph\">For example, we have the Online Customer Count Measure.<br \/>\u00a0Now, I want to add a filter to count only customers of the type \u201cPerson\u201d.<\/p>\n<p class=\"wp-block-paragraph\">I can modify the code in the data model or test the logic in a DAX query.<\/p>\n<p class=\"wp-block-paragraph\">The first step is to get the current code from the data model in the existing query.<\/p>\n<p class=\"wp-block-paragraph\">For this, I must place the cursor on the first line of the query. Ideally, I will add an empty line to the query.<\/p>\n<p class=\"wp-block-paragraph\">Now, I can use DAX Studio to extract the code of the Measure and add it to the Query by right-clicking on the Measure and clicking on \u201cDefine Measure\u201d:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-8.jpg?ssl=1\" alt=\"\" class=\"wp-image-601833\"><figcaption class=\"wp-element-caption\">Figure 9 \u2013 Use the \u201cDefine Measure\u201d feature of DAX Studio to extract the DAX code for a Measure (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">The same feature is also available in Power BI Desktop.<\/p>\n<p class=\"wp-block-paragraph\">Next, I can change the DAX code of the Measure by adding the Filter:<\/p>\n<p class=\"has-surface-secondary-background-color has-background wp-block-paragraph\"><code>DEFINE\u00a0&lt;br&gt;---- MODEL MEASURES BEGIN ----&lt;br&gt;MEASURE 'All Measures'[Online Customer Count] =&lt;br&gt;\u00a0\u00a0\u00a0\u00a0CALCULATE(DISTINCTCOUNT('Online Sales'[CustomerKey])&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,'Customer'[CustomerType] = \"Person\"&lt;br&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)&lt;br&gt;---- MODEL MEASURES END ----<\/code><\/p>\n<p class=\"wp-block-paragraph\">When executing the query, the local definition of the Measure is used, instead of the DAX code stored in the data model:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-9.jpg?ssl=1\" alt=\"\" class=\"wp-image-601834\"><figcaption class=\"wp-element-caption\">Figure 10 \u2013 Query and results with the modified DAX code for the Measure (Figure by the Author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Once the DAX code works as expected, you can take it and modify the Measure in Power BI Desktop.<\/p>\n<p class=\"wp-block-paragraph\">The DAX query view in Power BI Desktop is advantageous because you can directly right-click the modified code and add it back to the data model. Refer to the link in the References section below for instructions on how to do this.<\/p>\n<p class=\"wp-block-paragraph\">DAX Studio doesn\u2019t support this feature.<\/p>\n<h2 class=\"wp-block-heading\">Putting the pieces together<\/h2>\n<p class=\"wp-block-paragraph\">OK, now let\u2019s put the pieces together and write the following query: I want to get the top 5 products ordered by customers.<\/p>\n<p class=\"wp-block-paragraph\">I take the query from above, change the query to list the Product names, and add a <code>TOPN()<\/code>:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\">DEFINE\u00a0\n---- MODEL MEASURES BEGIN ----\nMEASURE 'All Measures'[Online Customer Count] =\n\u00a0\u00a0\u00a0\u00a0CALCULATE(DISTINCTCOUNT('Online Sales'[CustomerKey])\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,'Customer'[CustomerType] = \"Person\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)\n---- MODEL MEASURES END ----\n\nEVALUATE\n\u00a0\u00a0\u00a0\u00a0TOPN(5\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,SUMMARIZECOLUMNS('Product'[ProductName]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,\"Number of Customers\", [Online Customer Count]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,[Number of Customers]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0,DESC)\n\u00a0\u00a0\u00a0\u00a0ORDER BY [Number of Customers]<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Notice that I pass the measure\u2019s label, \u201cNumber of Customers\u201d, instead of its name.<\/p>\n<p class=\"wp-block-paragraph\">I must do it this way, as DAX replaces the measure\u2019s name with the label. Therefore, DAX has no information about the Measure and only knows the label.<\/p>\n<p class=\"wp-block-paragraph\">This is the result of the query:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-65.png?ssl=1\" alt=\"\" class=\"wp-image-601832\"><figcaption class=\"wp-element-caption\">Figure 11 \u2013 The query result using TOPN() combined with a Measure. Notice that the label is used instead of the Measures name (Figure by the Author)<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p class=\"wp-block-paragraph\">I often use queries in DAX Studio, as it is much easier for <a href=\"https:\/\/towardsdatascience.com\/tag\/data-validation\/\" title=\"Data Validation\">Data Validation<\/a>.<\/p>\n<p class=\"wp-block-paragraph\">DAX Studio allows me to directly copy the result into the Clipboard or write it in an Excel file without explicitly exporting the data.<\/p>\n<p class=\"wp-block-paragraph\">This is extremely useful when creating a result set and sending it to my client for validation.<\/p>\n<p class=\"wp-block-paragraph\">Moreover, I can modify a Measure without changing it in <a href=\"https:\/\/towardsdatascience.com\/tag\/power-bi\/\" title=\"Power Bi\">Power Bi<\/a> Desktop and quickly validate the result in a table.<\/p>\n<p class=\"wp-block-paragraph\">I can use a Measure from the data model, temporarily create a modified version, and validate the results side-by-side.<\/p>\n<p class=\"wp-block-paragraph\">DAX queries have endless use cases and should be part of every Power BI developer\u2019s toolkit.<\/p>\n<p class=\"wp-block-paragraph\">I hope that I was able to show you something new and explain why knowing how to write DAX queries is important for a Data model developer\u2019s daily life.<\/p>\n<h2 class=\"wp-block-heading\">References<\/h2>\n<p class=\"wp-block-paragraph\">Microsoft\u2019s documentation about applying changes from the DAX <a href=\"https:\/\/towardsdatascience.com\/tag\/query\/\" title=\"Query\">Query<\/a> view on the model:<\/p>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/power-bi\/transform-model\/dax-query-view#update-model-with-changes\">Update model with changes \u2013 DAX query view \u2013 Power BI | Microsoft Learn<\/a><\/p>\n<p class=\"wp-block-paragraph\">Like in my previous articles, I use the Contoso sample dataset. You can download the ContosoRetailDW Dataset for free from Microsoft <a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=18279\">here<\/a>.<\/p>\n<p class=\"wp-block-paragraph\">The Contoso Data can be freely used under the MIT License, as described <a href=\"https:\/\/github.com\/microsoft\/Power-BI-Embedded-Contoso-Sales-Demo\">in this document<\/a>. I changed the dataset to shift the data to contemporary dates.<\/p>\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/601823-2\/\">How to Write Queries for Tabular Models with DAX<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Salvatore Cagliari<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/601823-2\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Write Queries for Tabular Models with DAX Introduction EVALUATE is the statement to query tabular models. Unfortunately, knowing SQL or any other query language doesn\u2019t help as EVALUATE follows a different concept. EVALUATE has only two \u201cParameters\u201d: A table to show A sort order (ORDER BY) You can pass a third parameter (START [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,1726,211,83,2414,1360,2415],"tags":[1514,572,246],"class_list":["post-3244","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-data-validation","category-data-analysis","category-data-science","category-power-bi-tutorials","category-power-bi","category-query","tag-dax","tag-evaluate","tag-query"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/3244"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=3244"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/3244\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=3244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=3244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=3244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}