{"id":83924,"date":"2019-04-17T19:27:36","date_gmt":"2019-04-17T19:27:36","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=83924"},"modified":"2025-06-27T14:04:40","modified_gmt":"2025-06-27T14:04:40","slug":"using-the-filter-function-in-dax","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/bi-sql-server\/using-the-filter-function-in-dax\/","title":{"rendered":"Using the FILTER Function in DAX"},"content":{"rendered":"<p><strong>The series so far:<\/strong><\/p>\n<ol>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/creating-calculated-columns-using-dax\/\">Creating Calculated Columns Using DAX<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/creating-measures-using-dax\/\">Creating Measures Using DAX<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/using-the-dax-calculate-and-values-functions\/\">Using the DAX Calculate and Values Functions<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/using-the-filter-function-in-dax\/\">Using the FILTER Function in DAX<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/cracking-dax-the-earlier-and-rankx-functions\/\">Cracking DAX\u00a0\u2013 the EARLIER and RANKX Functions<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/using-calendars-and-dates-in-power-bi\/\">Using Calendars and Dates in Power BI<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/creating-time-intelligence-functions-in-dax\/\"><span>Creating Time-Intelligence Functions in DAX<\/span><\/a><\/li>\n<\/ol>\n\n<p>The <code>FILTER<\/code> function works quite differently than the <code>CALCULATE<\/code> function explained in the previous article. It returns a table of the filtered rows, and sometimes it is the better approach to take.<\/p>\n<p>I\u2019ll spend most of this article explaining how to create the following measures:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"263\" class=\"wp-image-83991\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-132.png\" \/><\/p>\n<p>The columns above show, respectively:<\/p>\n<ol>\n<li>The city name;<\/li>\n<li>Total sales for the city in question (the filter context);<\/li>\n<li>Total sales for the city in question for 2018;<\/li>\n<li>Total sales for the city in question for the USA;<\/li>\n<li>Total sales for the city in question for the USA for 2018; and<\/li>\n<li>The percentage each city\u2019s sales contributes to the total.<\/li>\n<\/ol>\n<p>First, I\u2019ll show you how to set up the example. Then I\u2019ll dive into the syntax of the <code>FILTER<\/code> function. I\u2019ll finish by highlighting the differences between the <code>FILTER<\/code> and <code>CALCULATE<\/code> functions.<\/p>\n<h2>A Quick Refresher<\/h2>\n<p>To work through the examples in this article, you\u2019ll need to create a simple Power BI report containing a single table and then create and show a series of measures. Here\u2019s a quick run-through of how to get started.<\/p>\n<p>First, create a Power BI report based on the tables used in the previous articles. You can load them either from the <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/10\/Generate-simple-toy-database.sql\">SQL Server database given<\/a> or the <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/10\/Simple-Toys.xlsx\">Excel workbook<\/a>. You should now have something like this (if your diagram looks a bit different, you may not have updated your instance of Power BI to include the March 2019 update, which included the new Model View):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"763\" height=\"255\" class=\"wp-image-83992\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-133.png\" \/><\/p>\n<p>Now create a table in <em>Report<\/em> view to list out the city names. Make sure the <em>Table<\/em> visualization is selected and click <em>CityName<\/em> in the <em>Fields<\/em> list.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"235\" height=\"340\" class=\"wp-image-83993\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-134.png\" \/><\/p>\n<p>Switch to the <em>Home<\/em> ribbon and select <em>Enter<\/em> <em>Data<\/em>. This will add a new table to your report to contain your measures. To understand why you might want to do this, see <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/creating-measures-using-dax\/\">this previous article<\/a> in this series:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"501\" height=\"236\" class=\"wp-image-83994\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-135.png\" \/><\/p>\n<p>Give this table a name. Here I\u2019ve called mine <em>All measures<\/em>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"363\" class=\"wp-image-83995\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-136.png\" \/><\/p>\n<p>Click <em>Load<\/em>. Now add the following measure to your <em>All measures<\/em> table. You can right-click on the table and choose <em>New measure<\/em> to do this:<\/p>\n<pre class=\"lang:c# theme:vs2012\">Sales = SUMX(\n    \/\/ multiply the price of each transaction by\n    \/\/ its quantity, and sum the result\n    Sales,\n    [Price]*[Quantity]\n)<\/pre>\n<p>Choose to display this measure in your table:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"228\" height=\"227\" class=\"wp-image-83996\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-137.png\" \/><\/p>\n<p>You should now be able to see the total value of sales for each city:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"179\" height=\"268\" class=\"wp-image-83997\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-138.png\" \/><\/p>\n<p>What happens if you want to show the sales for American cities only? Or sales taking place in 2018 only?<\/p>\n<h2>The FILTER Function<\/h2>\n<p>The measure for the sales column shown above, giving total sales for each city, is as follows:<\/p>\n<pre class=\"lang:c# theme:vs2012\">Sales = SUMX(\n    Sales,\n    [Price]*[Quantity]\n)<\/pre>\n<p>What this does (as readers of this series of articles will know) is to iterate down the rows in the <em>Sales<\/em> table, calculating the price multiplied by the quantity for each and summing the result for each city to get this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"175\" height=\"236\" class=\"wp-image-83998\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-139.png\" \/><\/p>\n<p>To get the sales in 2008, you could use a <code>CALCULATE<\/code> function so that this measure would work:<\/p>\n<pre class=\"lang:c# theme:vs2012\">2018 sales using CALCULATE = CALCULATE(\n    SUMX(\n        Sales,\n        [Price]*[Quantity]\n    ),\n    YEAR(Sales[SalesDate]) = 2018\n)<\/pre>\n<p>This takes the filter context for each city, and further reduces it to consider only those rows where the sales occurred in 2018 to get this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"241\" class=\"wp-image-83999\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-140.png\" \/><\/p>\n<p>Another way to solve the problem, however, is to treat the sales for the current filter context as a table and filter it accordingly. Consider the example of sales for New York. Here\u2019s the underlying data for this city:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"126\" class=\"wp-image-84000\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-141.png\" \/><\/p>\n<p>The total figure for sales for New York for 2018 is 25.98 (18.98 + 7.00). One way to get to this would be to follow these steps when compiling the data for New York. Firstly, get the data for the filter context:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"435\" height=\"125\" class=\"wp-image-84001\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-142.png\" \/><\/p>\n<p>Secondly, filter this data to include only those sales for 2018, by iterating down each row deciding whether to include it. This will include only the shaded area below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"126\" class=\"wp-image-84002\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-143.png\" \/><\/p>\n<p>This leaves this table, which is the one whose sales Power BI will sum:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"73\" class=\"wp-image-84003\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-144.png\" \/><\/p>\n<p>Here\u2019s the formula to accomplish this:<\/p>\n<pre class=\"lang:c# theme:vs2012\">2018 sales = SUMX(\n    FILTER(\n        Sales,\n        YEAR(Sales[SalesDate])=2018\n    ),\n    [Price]*[Quantity]\n)<\/pre>\n<p>This will give exactly the same results as the formula using <code>CALCULATE<\/code> above. The <code>CALCULATE<\/code> function will run more quickly because it doesn\u2019t have to iterate down each row in the table testing a condition. At this point, you may be asking yourself what the point of the <code>FILTER<\/code> function is. I\u2019ll return to this later in this article.<\/p>\n<h2>Linking tables within the Filter Function<\/h2>\n<p>Take a look at how to show total sales for the USA for each city. The <em>Sales<\/em><strong>,<\/strong> <em>City<\/em> and <em>Country<\/em> tables are related as follows:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"198\" class=\"wp-image-84004\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-145.png\" \/><\/p>\n<p>What\u2019s needed is to iterate down the rows in the sales table, calculating the sales (price times quantity) for each but only where the country name is <em>USA<\/em>. Here\u2019s the formula to do this:<\/p>\n<pre class=\"lang:c# theme:vs2012\">American sales = SUMX(\n    FILTER(\n        Sales,\n        RELATED(Country[CountryName])=\"USA\"\n    ),\n    [Price]*[Quantity]\n)<\/pre>\n<p>The expression gives these results:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"294\" height=\"239\" class=\"wp-image-84005\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-146.png\" \/><\/p>\n<p>The question is \u2013 why use the <code>RELATED<\/code> function when the DAX formulae using filter context automatically link tables together? The answer is that within this formula, row context, not filter context, is used. The shaded lines in the formula below iterate over each row in the <em>Sales<\/em> table returned for the filter context, creating a row context for each:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"145\" class=\"wp-image-84006\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-147.png\" \/><\/p>\n<p>Because within the shaded bit of the formula DAX has to create a row context for each row in the sales table, it then has to use the <code>RELATED<\/code> function to bring in the country name from the <em>Country<\/em> table.<\/p>\n<h2>Combining Filters Without Nesting<\/h2>\n<p>It\u2019s now time to look at how to combine criteria: how to show sales which happened in 2018 and which took place in the USA. I\u2019ll show in a bit how to do this by nesting one <code>FILTER<\/code> function within another, but for now, I\u2019ll show ways to combine criteria. There are two basic ways to do this in DAX \u2013 either by using <code>&amp;&amp;<\/code> or the <code>AND<\/code> function (or if either of two conditions can be true, using <code>||<\/code> or the <code>OR<\/code> function).<\/p>\n<p>Here\u2019s a version of the measure using the <code>AND<\/code> function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">2018 American sales = SUMX(\n    FILTER(\n        Sales,\n        AND(\n            RELATED(Country[CountryName])=\"USA\",          \n            YEAR(Sales[SalesDate])=2018\n        )\n    ),\n    [Price]*[Quantity]\n)<\/pre>\n<p>Here\u2019s the same measure, but using the <code>&amp;&amp;<\/code> symbols:<\/p>\n<pre class=\"lang:c# theme:vs2012\">2018 American sales using &amp;&amp; = SUMX(\n    FILTER(\n        Sales,\n        RELATED(Country[CountryName])=\"USA\" &amp;&amp;\n        YEAR(Sales[SalesDate])=2018\n    ),\n    [Price]*[Quantity]\n)<\/pre>\n<p>Personally, I\u2019d use the <code>AND<\/code> (or <code>OR<\/code>) functions any time, as they work in the same way as their Excel counterparts, and it\u2019s easier to indent and comment formulae. However, you should use whichever floats your particular boat.<\/p>\n<p>Even more sales have dropped from the figures:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"341\" height=\"240\" class=\"wp-image-84007\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-148.png\" \/><\/p>\n<h2>Combining Conditions by Nesting Functions<\/h2>\n<p>The other way to solve this would have been to nest one function within another:<\/p>\n<pre class=\"lang:c# theme:vs2012\">2018 American sales using nesting = SUMX(\n    FILTER(\n        FILTER(\n            Sales,\n            RELATED(Country[CountryName])=\"USA\"\n        ),\n        YEAR(Sales[SalesDate])=2018\n    ),\n    [Price]*[Quantity]\n)<\/pre>\n<p>Consider what this does for the New York row in the table:<\/p>\n<p>Filter context restricts the data to sales for the current city in question.<\/p>\n<p>The inner <code>FILTER<\/code> function iterates over each row in the table of data for the filter context, picking out only rows where the country is in the USA.<\/p>\n<p>The outer <code>FILTER<\/code> function then iterates over each row in the table of sales for the USA for the filter context and applies a further constraint that the sales year must be 2018.<\/p>\n<p>Depending on your data, nesting <code>FILTER<\/code> functions could speed up processing. If the vast majority of sales were outside the USA, the inner condition could eliminate nearly all rows for each city in the filter context, with the result that Power BI would only need to test the sales date for the few remaining rows.<\/p>\n<h2>Using ALL to Remove Any Filters<\/h2>\n<p>Every example shown so far has taken the set of rows for the current filter context and applied additional constraints to pick out only certain rows. However, you can use the <code>ALL<\/code> function when filtering to work with the entire table, rather than just the data for the current filter context. You could use this, for example, to show the percentage contribution of each city\u2019s sales to the grand total:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"285\" height=\"263\" class=\"wp-image-84008\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-149.png\" \/><\/p>\n<p>Here\u2019s the formula for the above measure:<\/p>\n<pre class=\"lang:c# theme:vs2012\">% of all sales = DIVIDE(\n    \/\/ calculate the total sales for the current filter context\n    SUMX(\n        Sales,\n        [Price]*[Quantity]\n    ),\n    \/\/ divide this by total sales for all cities\n    SUMX(\n        ALL(Sales),\n        [Price]*[Quantity]\n    )\n)<\/pre>\n<p>Incidentally, if you\u2019re wondering how to get the nice percentage format, just select the measure you\u2019ve created:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"220\" height=\"88\" class=\"wp-image-84009\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-150.png\" \/><\/p>\n<p>You can then set the formatting in the <strong>Modeling<\/strong> tab on the menu:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"521\" height=\"136\" class=\"wp-image-84010\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-151.png\" \/><\/p>\n<h2>The Difference Between CALCULATE and FILTER<\/h2>\n<p>To see the difference between the way in which <code>CALCULATE<\/code> and <code>FILTER<\/code> filter data, consider this example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"259\" class=\"wp-image-84011\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-152.png\" \/><\/p>\n<p>The first measure applies the filter context (so it only calculates sales for the city in question), and applies an additional constraint that the city should be New York:<\/p>\n<pre class=\"lang:c# theme:vs2012\">New York FILTERED = CALCULATE(\n\u00a0\u00a0\u00a0\u00a0\/\/ work out total sales\n\u00a0\u00a0\u00a0\u00a0\/\/ for the filter context\n\u00a0\u00a0\u00a0\u00a0SUMX(\n        Sales,\n        [Price]*[Quantity]\n    ),\n\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\/\/ but whittle the filter context\n\u00a0\u00a0\u00a0\u00a0\/\/ down to show only those cities\n\u00a0\u00a0\u00a0\u00a0\/\/ within it called New York\n\u00a0\u00a0\u00a0\u00a0FILTER(\n        City,\n        City[CityName]=\"New York\"\n    )\n)<\/pre>\n<p>The second measure replaces the filter context with a new constraint that the city should be New York, which results in the same figure appearing in every row:<\/p>\n<pre class=\"lang:c# theme:vs2012\">New York CALCULATED = \n\u00a0\u00a0\u00a0\u00a0CALCULATE(\n\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ work out total sales\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ for the filter context\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SUMX(\n            Sales,\n            [Price]*[Quantity]\n        ),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ changing the city criteria\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ so it is New York, not \n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ whatever the filter context\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ originally said\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0City[CityName]=\"New York\"\n)<\/pre>\n<h2>Debugging Using the FILTER Function (Method 1)<\/h2>\n<p>To make debugging easier, first add a couple of calculated columns to the <em>Sales<\/em> table, to give the city name and sales year. The formulae are shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"758\" height=\"133\" class=\"wp-image-84012\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-153.png\" \/><\/p>\n<p>Here\u2019s the formula for the <em>City<\/em> column. It just looks up the name of each city in which sales took place:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"114\" class=\"wp-image-84013\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-154.png\" \/><\/p>\n<p>Here\u2019s the formula for the <em>Sales<\/em> <em>year<\/em> column. It gives the year in which each sale took place:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"120\" class=\"wp-image-84014\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-155.png\" \/><\/p>\n<p>These two columns will make it easier to check what\u2019s going on when debugging.<\/p>\n<p>The <code>FILTER<\/code> function creates virtual tables which, under normal circumstances, you never see, but you can use a tool like DAX Studio to show the rows these virtual tables contain. I\u2019ve covered how to download and use DAX Studio in a <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/creating-measures-using-dax\/\">previous article<\/a> in this series, but here\u2019s a quick refresher. When you run DAX Studio, choose to connect to an open Power BI report:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"588\" height=\"311\" class=\"wp-image-84015\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-156.png\" \/><\/p>\n<p>Type in a DAX query in the top right-hand window and press the F5 key to run this. The results will appear beneath it. For the example below, I\u2019m just listing out the contents of the <strong>Sales<\/strong> table:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"923\" height=\"438\" class=\"wp-image-84016\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-157.png\" \/><\/p>\n<p>Incidentally, if you\u2019re wondering what those long date table names are, you\u2019re not the only one. I presume they are created behind the scenes to provide the built-in date hierarchy included in the March 2019 update of Power BI.<\/p>\n<p>You can evaluate any table, including one which is returned from a filter function. A good thing to ask might be: which sales were in the United States? You can do this by copying this part of the measure you created earlier:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"409\" height=\"159\" class=\"wp-image-84017\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-158.png\" \/><\/p>\n<p>Precede this with the word <code>EVALUATE<\/code> in DAX Studio, and you\u2019ll get this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"443\" height=\"87\" class=\"wp-image-84018\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-159.png\" \/><\/p>\n<p>Run this to get the following output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"623\" height=\"178\" class=\"wp-image-84019\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-160.png\" \/><\/p>\n<p>That\u2019s looking good, so now you can repeat this technique with the outer bit of the <code>FILTER<\/code> function:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"419\" height=\"230\" class=\"wp-image-84020\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-161.png\" \/><\/p>\n<p>This gives only 3 rows:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"301\" class=\"wp-image-84021\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-162.png\" \/><\/p>\n<p>From this, it\u2019s easy to see why you get the figures for this measure.<\/p>\n<h2>Debugging Using the FILTER Function (Method 2)<\/h2>\n<p>Another way to debug a DAX formula using the <code>FILTER<\/code> function (or any other DAX formula, for that matter) is to use variables. I\u2019ve already covered this for scalar variables (ones holding a single value) in the previous article in this series on <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/creating-measures-using-dax\/\">measures<\/a>, but did you know a variable can hold an entire table?<\/p>\n<p>Here\u2019s another way to write the nested <code>FILTER<\/code> function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">US sales in 2018 = \n\/\/ create a variable to hold the sales in the USA\nVAR UsaSalesTable =  FILTER(\n    Sales,\n    RELATED(Country[CountryName])=\"USA\"\n)\n\/\/ create another variable to filter this to show\n\/\/ only sales in 2008\nVAR UsaSales2018 = FILTER(\n    UsaSalesTable,\n    YEAR(Sales[SalesDate])=2018\n)\n\/\/ finally, calculates sales for these figures\nRETURN SUMX(\n    UsaSales2018,\n    [Price]*[Quantity]\n)<\/pre>\n<p>The advantage of breaking the complicated formula down into different parts is that you could then test each in isolation.<\/p>\n<h2>Why Would You Use the FILTER Function?<\/h2>\n<p>I promised I would return to this question: why would you use the <code>FILTER<\/code> function when the <code>CALCULATE<\/code> function seems to offer a better alternative? There are at least four advantages:<\/p>\n<p>I\u2019ve already shown that it\u2019s easier to debug DAX expressions that use the <code>FILTER<\/code> function.<\/p>\n<p>I think expressions using the <code>FILTER<\/code> function are easier to understand than equivalent expressions just using <code>CALCULATE<\/code>.<\/p>\n<p>Learning the <code>FILTER<\/code> function will help you to understand the <code>EARLIER<\/code> function, which will be the subject of the next article in this series.<\/p>\n<p>There are some problems which the <code>CALCULATE<\/code> function won\u2019t solve (an example follows).<\/p>\n<p>To illustrate the last point, suppose that you want to create a measure showing total sales for cities having two or more purchases. Here are the figures that this should return:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"240\" class=\"wp-image-84022\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-163.png\" \/><\/p>\n<p>There are no sales recorded for Chicago, LA and Rio in the new measure because they each only witnessed a single sale.<\/p>\n<p>Assume in all of the following that <code>[Number of purchases]<\/code> is a measure with this formula:<\/p>\n<pre class=\"lang:c# theme:vs2012\">Number of purchases = COUNTROWS(Sales)<\/pre>\n<p>Here\u2019s a measure which you could use to try to solve this problem (although it won\u2019t work):<\/p>\n<pre class=\"lang:c# theme:vs2012\">Sales for multiple purchases = CALCULATE(\n    \/\/ calculate total sales where ...\n    SUMX(\n        Sales,\n        [Price]*[Quantity]\n    ),\n    \/\/ ... the number of purchases is more than 1\n    [Number of purchases] &gt; 1\n)<\/pre>\n<p>If you type in this measure, you\u2019ll see the following error message:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"301\" class=\"wp-image-84023\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-164.png\" \/><\/p>\n<p>This isn\u2019t a brilliant description of the problem, which is that you can\u2019t use a measure in the filtering part of a <code>CALCULATE<\/code> function; you can only refer to columns. You can, however, solve this problem by rewriting it to incorporate a <code>FILTER<\/code> function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">Sales for multiple purchases = CALCULATE(\n    \/\/ calculate total sales but ...\n    SUMX(\n        Sales,\n        [Price]*[Quantity]\n    ),\n    \/\/ only where the number of \n    \/\/ purchases is more than 1\n    FILTER(\n        City,\n        [Number of purchases] &gt; 1\n    )\n)<\/pre>\n<p>This will calculate total sales, but only for those cities where the number of purchases was more than 1.<\/p>\n<h2>Summary<\/h2>\n<p>The FILTER function in DAX allows you to iterate down the rows of any table, creating a row context for each and testing whether the row should be included in your calculation. You can combine filters using keywords like AND and OR and also nest one filter within another. The FILTER function allows you to perform some tasks which the CALCULATE function can\u2019t reach, and also (in my opinion) lets you create formulae which are easier to understand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous article of this series, Andy Brown of Wise Owl Training explained how to use the oh-so-important CALCULATE function in DAX to make changes to the default filter context within a formula. This article shows how you can use the FILTER function to do something similar and explains the differences between the two approaches. &hellip;<\/p>\n","protected":false},"author":9783,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143528],"tags":[5134],"coauthors":[6782],"class_list":["post-83924","post","type-post","status-publish","format-standard","hentry","category-bi-sql-server","tag-sql-prompt"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/83924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/9783"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=83924"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/83924\/revisions"}],"predecessor-version":[{"id":107297,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/83924\/revisions\/107297"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=83924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=83924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=83924"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=83924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}