{"id":83876,"date":"2019-04-15T18:31:40","date_gmt":"2019-04-15T18:31:40","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=83876"},"modified":"2022-04-24T21:07:02","modified_gmt":"2022-04-24T21:07:02","slug":"power-bi-and-the-matrix-a-challenge","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/bi-sql-server\/power-bi-and-the-matrix-a-challenge\/","title":{"rendered":"Power BI and The Matrix: A Challenge"},"content":{"rendered":"<p>In this article, I will show an example to demonstrate some interesting techniques using the Matrix visual. This was inspired by a friend, <a href=\"https:\/\/albertherd.com\/\">Albert Herd<\/a>, who asked for some help in our Malta user group to solve a problem.<\/p>\n<p>The data used in the example is the list of numbers drawn on the Maltese lotto. Each record is one number from a drawing, and each drawing has five records which are the five numbers from each drawing. You can <a href=\"https:\/\/1drv.ms\/u\/s!ApgSbfuN8DmZnL4qzkTidfOf1ECDZw\">download a zip file<\/a> containing a <em>csv<\/em> file with the source data, a <em>pbix<\/em> file with the data already imported to start creating the visuals, and the completed solution.<\/p>\n<p>By using the Matrix visualization, the numbers for each drawing can be displayed as a single line as shown in this figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"331\" height=\"360\" class=\"wp-image-83877\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-32.png\" \/><\/p>\n<p>The goal is to add a slicer that filters the rows based on the number or numbers chosen. For example, if you select 5 and 10, the rows that contain those numbers will be displayed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"390\" height=\"303\" class=\"wp-image-83878\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-33.png\" \/><\/p>\n<p>It\u2019s also possible to add conditional formatting so that the selected numbers light up in the colour of your choice.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"369\" height=\"287\" class=\"wp-image-83879\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-34.png\" \/><\/p>\n<p>Accomplishing this is not as straightforward as it might seem. Continue reading to learn more.<\/p>\n<h2>The Data<\/h2>\n<p>Each record in the table is one number drawn on a specific lotto drawing. Each drawing has 5 numbers, so each drawing has 5 records in the table. The table is called <em>Lotto,<\/em> and the fields most important for this example are these:<\/p>\n<p><strong>DrawNo:<\/strong> The number of the drawing<\/p>\n<p><strong>DrawOrder:<\/strong> The order of the drawn number<\/p>\n<p><strong>Number:<\/strong> The drawn number<\/p>\n<p>Starting with a new Power BI dashboard and import the csv file. As an alternative, you can also start with the <em>MatrixSimpleStart.pbix<\/em> file provided in the zip file.<\/p>\n<h2>Creating the Matrix<\/h2>\n<p>The Matrix visual has three fields to be configured: the field used for the rows, the field used for the columns, and the field used for the values. Each line of the visual should show a single drawing with the five numbers. Due to that, the field for the rows will be the <em>DrawNo<\/em>, aggregating the drawings on each row.<\/p>\n<p>Each drawing has five records, the five drawn numbers, so how do you show five records on each line? The content of the field you choose as a <em>column<\/em> field will be used as the title of the columns. The best choice is easy: <em>DrawOrder<\/em>. Each row shows the <em>DrawNo<\/em>. Each column has the <em>DrawOrder<\/em> as a title and will show the drawn <em>Number<\/em> as a value in the column.<\/p>\n<p>Once you have the file <em>MatrixSampleStart.pbix <\/em>opened or the csv file imported, follow a simple sequence of steps to configure the Matrix visual. You\u2019ll be working in <em>Report<\/em> view.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li><a id=\"post-83876-_Hlk4617579\"><\/a> Add a <em>Matrix<\/em> visual to the report from the <em>Visualizations<\/em> pane<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"228\" height=\"260\" class=\"wp-image-83880\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-35.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>In the <em>Fields Pane<\/em>, drag the three fields, <em>DrawNo<\/em>, <em>DrawOrder,<\/em> and <em>Number <\/em>to the correct slots.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"471\" height=\"450\" class=\"wp-image-83881\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-36.png\" \/><\/p>\n<p>Once you have the fields in the correct spots, the Matrix will resemble this image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"391\" height=\"427\" class=\"wp-image-83882\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-37.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>In the <em>Visualizations<\/em> <em>Pane<\/em>, with the matrix selected, click the <em>Format<\/em> button and disable the subtotals for rows and columns as they are not needed.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"394\" height=\"138\" class=\"wp-image-83883\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-38.png\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"306\" class=\"wp-image-83884\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-39.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>While still in the <em>Format<\/em> tab, under the <em>Style<\/em> option, change the style of the matrix. You can choose any available style; I suggest <em>Bold Header.<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"397\" height=\"321\" class=\"wp-image-83885\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-40.png\" \/><\/p>\n<ol>\n<li>Still in the <em>Format<\/em> tab, change the font size under these three different options: <em>Row<\/em> <em>Headers<\/em>, <em>Column<\/em> <em>Headers,<\/em> and <em>Values<\/em><strong>. <\/strong>I like to use 12 as font size.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"399\" height=\"272\" class=\"wp-image-83886\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-41.png\" \/><\/p>\n<p>The completed Matrix should look like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"331\" height=\"360\" class=\"wp-image-83887\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-42.png\" \/><\/p>\n<h2>Creating a Slicer and Filtering the Matrix<\/h2>\n<p>The matrix contains numbers from lotto drawings, so a good option for a slicer is to filter the drawn numbers, showing only the drawings with the selected drawn numbers.<\/p>\n<p>There are three possible approaches to create a slicer:<\/p>\n<ul>\n<li>Create a slicer based on the original table fields (<em>Lotto<\/em>)<\/li>\n<li>Create a slicer based on a new calculated table by using a <strong>DAX<\/strong> expression to create the new table from the original one<\/li>\n<li>Create a slicer based on a <strong>What-If<\/strong> parameter<\/li>\n<\/ul>\n<p><strong>NOTE: DAX<\/strong> is an expression language used in tabular models, such as the model in <strong>Power BI<\/strong>, to allow creating calculations over the model.<\/p>\n<p>The first two options keep a relationship with the original table (<em>Lotto<\/em>). Although this relationship is not important for the result at all, it causes a small bug. The slicer needs to be inserted in the page before the matrix. If the slicer is inserted after the matrix, some of the slicer configurations will not be available. The slicer needs to be inserted first.<\/p>\n<h2>Creating a Slicer from the Same Table<\/h2>\n<p>The easiest way to add a slicer is from a field in the table. Unfortunately, it doesn\u2019t quite provide the solution in this case. Follow these steps to see how to add a slicer based on the table:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Drop the matrix<\/li>\n<li>In the <em>Fields<\/em> <em>Pane<\/em>, select the <em>Number<\/em> field in the Lotto table<\/li>\n<li>In the <em>Visualizations<\/em> <em>Pane<\/em>, change the visual to <em>Slicer<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"186\" class=\"wp-image-83888\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-43.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>In the slicer type option, inside the slicer, change the slicer format to <em>List<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"428\" height=\"373\" class=\"wp-image-83889\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-44.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>In the <em>Visualizations Pane<\/em>, with the slicer selected, click the <em>Format<\/em> button. In the <em>Selection<\/em> <em>Control<\/em> options, disable the <em>Single<\/em> <em>Select<\/em> option, allowing multiple numbers selection<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"118\" class=\"wp-image-83890\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-45.png\" \/><\/p>\n<ol>\n<li>Repeat the steps in the \u201cCreate the Matrix\u201d section to recreate the matrix.<\/li>\n<\/ol>\n<p>To test this solution, select two numbers, such as 5 and 10, in the slicer and look at the result in the matrix. You will notice two problems:<\/p>\n<ul>\n<li>The draws are filtered to show only the selected numbers instead of all the numbers of the selected draws. That&#8217;s not the best result for this solution.<\/li>\n<li>The multiple selections act as an <strong>OR<\/strong>, not an <strong>AND<\/strong>. Draws with only one of the two selected numbers appear.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"843\" height=\"295\" class=\"wp-image-83892\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-47.png\" \/><\/p>\n<p><strong>Fixing the Selection<\/strong><\/p>\n<p>In order to fix the selection, a different approach for this problem is needed. The filter is automatically made by the model and visual engine in Power BI, showing only the selected numbers. In order to show all the numbers of the selected draws, you will need to break the automatic filter and create a <strong>DAX<\/strong> formula that will control which draws need to appear.<\/p>\n<p>This leads back to the decision about how to build the slicer: Building the slicer directly from the draws table (<em>Lotto<\/em>) creates a relationship that can&#8217;t be broken. That&#8217;s the only slicer option that will not work. If you choose to build the slicer from a calculated table or <em>What-If<\/em> parameter, the relationship to the source table (<em>Lotto<\/em>) can be controlled, avoiding the filtering.<\/p>\n<p>Once the matrix is not directly filtered by the slicer, you can create a <strong>DAX<\/strong> formula to filter the drawings. The expression will need to compare the numbers on the current drawing row to the selected numbers on the slicer, identifying if the row should be displayed or not.<\/p>\n<p>First, you\u2019ll see the two additional ways to create the slicer, both using a helper table. You can choose either method.<\/p>\n<h2>Creating the Slicer &#8211; Calculated Table<\/h2>\n<p>You can create this table using a very simple DAX expression. On the top menu, <em>Modeling<\/em> tab, you\u2019ll find a button called <em>New<\/em> <em>Table<\/em>. After clicking this button, a space expands where you can introduce the <strong>DAX<\/strong> expression for this table.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"339\" height=\"164\" class=\"wp-image-83893\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-48.png\" \/><\/p>\n<p>Call the table <em>Selector<\/em>. The expression is very simple:<\/p>\n<pre class=\"lang:c# theme:vs2012\">Selector\u00a0= VALUES\u00a0(Lotto[Number]\u00a0)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"504\" height=\"44\" class=\"wp-image-83895\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-50.png\" \/><\/p>\n<p>The newly created table, <em>Selector<\/em>, will have no relationship with the original one. Although it was created from the <em>Lotto<\/em> rows, the only effect will be the strange visual behaviour that requires the slicer to be created before the matrix.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"288\" height=\"303\" class=\"wp-image-83896\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-51.png\" \/><\/p>\n<p>Choosing this option for the <em>Selector<\/em> table, you will need to execute the following steps:<\/p>\n<ol>\n<li>Drop the slicer<\/li>\n<li>Drop the matrix<\/li>\n<li>Create the slicer again using the steps from the \u201cCreating a Slicer from the Same Table\u201d section but use the <em>Number<\/em> field from the <em>Selector<\/em> table<\/li>\n<li>Create the matrix again<\/li>\n<\/ol>\n<p>Note that at this point, the slicer will not filter the data. Continue reading to learn how to get it to work filtering the matrix.<\/p>\n<h2>Creating the Slicer from the What-if Parameter<\/h2>\n<p>Instead of creating the <em>Selector<\/em> table based on the <em>Lotto<\/em> table, you can create a <em>What-if<\/em> parameter. This is a new table with no relationship to the <em>Lotto<\/em> table.<\/p>\n<p>If you created the Selector based on the table in the previous section, delete it before following these steps.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Delete the matrix<\/li>\n<li>On the top menu, <em>Modeling<\/em> tab, click the <em>Create<\/em> <em>Parameter<\/em> button to bring up the <em>What-if Parameter<\/em> window.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"166\" class=\"wp-image-83897\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-52.png\" \/><\/p>\n<ol>\n<li>Change the <em>Name<\/em> to <em>Selector<\/em> and specify a table of values from 1 to 99.<\/li>\n<li>Click <em>OK<\/em> once the properties have been filled in<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"562\" height=\"540\" class=\"wp-image-83898\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-53.png\" \/><\/p>\n<p>\nThe DAX formula generated behind the scenes is:<\/p>\n<pre class=\"lang:c# theme:vs2012\">Selector\u00a0= GENERATESERIES\u00a0(\u00a01,\u00a099,\u00a01\u00a0)<\/pre>\n<p>You\u2019ll see the new measure in the <em>Fields<\/em> pane:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"285\" height=\"372\" class=\"wp-image-83899\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-54.png\" \/> <\/p>\n<p>As you will notice on the image above, there is one small difference on this option: the field created inside the <em>Selector<\/em> table is also called <em>Selector<\/em> instead of <em>Number<\/em> as on the previous option.<\/p>\n<p>In order to make both options the same, you can rename the <em>Selector<\/em> field to <em>Number<\/em>. It\u2019s optional, but if you don\u2019t, the following expressions will need to use <code>Selector[Selector]<\/code> to refer to this field instead of <code>Selector[Number].<\/code> <\/p>\n<p>The steps to rename this field:<\/p>\n<ol>\n<li>In the <em>Fields<\/em> pane, under the <em>Selector<\/em> table, next to the <em>Selector<\/em> field, click the \u2018\u2026\u2019 (<em>More Options<\/em>) button<\/li>\n<li>Click the <em>Rename<\/em> menu item in the context menu that will appear<\/li>\n<li>Change the field name to <em>Number<\/em><\/li>\n<\/ol>\n<p>Follow these steps to complete the slicer<\/p>\n<ol>\n<li>Repeat the formatting (steps 4 and 5 in the \u201cCreating a Slicer from the Same Table\u201d) to format the new slicer that will be automatically added to the report.<\/li>\n<li>Recreate the matrix as shown in the \u201cCreating the Matrix\u201d section<\/li>\n<\/ol>\n<p>If you chose this method for the slicer, continue to learn how to get it to work filtering the matrix.<\/p>\n<h2>Measures vs. Calculated Columns<\/h2>\n<p>Before going forward, it\u2019s interesting to understand why to create measures and not calculated columns. Both measures and calculated columns accept <strong>DAX<\/strong> expressions. However, they have some differences. While the calculated column expression is evaluated in the row context, row by row, measures are used on aggregations.<\/p>\n<p>Another significant difference, usually the easiest one to help with the decision, is when the calculation is made. The calculated column expressions are evaluated when the table is processed, and the result is stored within the <strong>Power BI<\/strong> file. This means they can\u2019t rely on any interaction with the visuals, because they are calculated before.<\/p>\n<p>This makes the decision easy: you need measures that will react to the selection on the slicer as the user make it. The fact these measures will be calculated on each line of the matrix, which in fact is an aggregation of five records, is just an additional reason.<\/p>\n<h2>Creating the Measures for Filtering<\/h2>\n<p>To filter the rows according to the selected numbers, you will need to create one measure to identify if each row has the selected numbers on the slicer. It&#8217;s a <code>boolean<\/code> measure which should result in <code>true<\/code> or <code>false<\/code>, but here comes the first trick: Power BI doesn&#8217;t deal very well with <code>boolean<\/code> measures used for filtering, so you need to create it as a numeric measure resulting in 1 or 0.<\/p>\n<p>Another concern about this formula is to display all the rows when there is no selection in the slicer. In this case, the measure should return 1 for all the rows, showing everything.<\/p>\n<p>This measure will be calculated for each row of the matrix, and each row of the matrix has a set of five numbers. The slicer, on the other hand, also will have a set of numbers selected and you don\u2019t know how many. If the drawing numbers in the row contain all the numbers of the slicer, the result should be 1 (show the line), otherwise 0.<\/p>\n<p>A <strong>DAX<\/strong> expression allows you to create variables inside the expression, and you can put this to good use to solve this problem. Here is the beginning of the expression:<\/p>\n<pre class=\"lang:c# theme:vs2012\">LineFilter\u00a0=\r\nVAR\u00a0tab\u00a0=\u00a0\u00a0\u00a0VALUES\u00a0(Selector[Number]\u00a0)\r\nVAR\u00a0tab2\u00a0=\u00a0\u00a0VALUES\u00a0(\u00a0Lotto[Number]\u00a0)\r\nVAR\u00a0common\u00a0=\u00a0\u00a0INTERSECT\u00a0(\u00a0tab,\u00a0tab2\u00a0)\r\nVAR\u00a0rowsCommon\u00a0=\u00a0\u00a0COUNTROWS\u00a0(\u00a0common\u00a0)\r\nVAR\u00a0rowsSelected\u00a0=\u00a0\u00a0COUNTROWS\u00a0(\u00a0tab\u00a0)<\/pre>\n<p>\nIt&#8217;s essential to consider the context used to process this expression. The <code>Values<\/code> function over the <code>Selector<\/code> table will return only the numbers selected on the slicer or all the numbers, while the <code>Values<\/code> function over the <code>Lotto<\/code> table will return only the numbers for the current drawing line, since the expression will be analysed for each line of the matrix.<\/p>\n<p>On the final part of the expression, if the <code>rowsCommon<\/code> variable is equal to the <code>rowsSelected<\/code> variable, it means all numbers selected on the slicer are on this drawing, and the result will be 1. Otherwise, it will be 0. However, you need also to consider if the slicer is not filtered at all. For this, you have the <code>ISFILTERED<\/code><strong> DAX<\/strong> function.<\/p>\n<p>The full <strong>DAX<\/strong> expression is:<\/p>\n<pre class=\"lang:c# theme:vs2012 \">LineFilter\u00a0=\r\nVAR\u00a0tab\u00a0=\u00a0\u00a0\u00a0VALUES\u00a0(\u00a0Selector[Number]\u00a0)\r\nVAR\u00a0tab2\u00a0=\u00a0\u00a0VALUES\u00a0(\u00a0Lotto[Number]\u00a0)\r\nVAR\u00a0common\u00a0=\u00a0\u00a0INTERSECT\u00a0(\u00a0tab,\u00a0tab2\u00a0)\r\nVAR\u00a0rowsCommon\u00a0=\u00a0\u00a0COUNTROWS\u00a0(\u00a0common\u00a0)\r\nVAR\u00a0rowsSelected\u00a0=\u00a0\u00a0COUNTROWS\u00a0(\u00a0tab\u00a0)\r\nRETURN\r\n\u00a0\u00a0\u00a0\u00a0IF\u00a0(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0OR\u00a0(\u00a0rowsCommon\u00a0=\u00a0rowsSelected,\u00a0\r\n          NOT\u00a0(\u00a0ISFILTERED\u00a0(\u00a0Selector[Number]\u00a0)\u00a0)\u00a0);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00\r\n\u00a0\u00a0\u00a0\u00a0)<\/pre>\n<p>The steps to use this expression are the following:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>In the <em>Fields<\/em> pane, Click the <em>&#8216;&#8230;&#8217; (More Options)<\/em> button close to the <em>Lotto<\/em> table<\/li>\n<li>Click the <em>New Measure<\/em> menu option in the context menu that will appear<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"287\" height=\"602\" class=\"wp-image-83900\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-55.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Paste the entire expression, including the measure name, in the formula bar<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"299\" class=\"wp-image-83901\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-56.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Drag the newly created measure to the filter area of the matrix configuration<\/li>\n<li>Change the comparison expression <em>Show items when the value<\/em> to <em>is<\/em><\/li>\n<li>Fill the value expression with 1<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"394\" height=\"559\" class=\"wp-image-83902\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-57.png\" \/><\/p>\n<ol>\n<li>Click <code>Apply<\/code> <code>Filter<\/code><\/li>\n<\/ol>\n<p>After completing these steps, the filter will be working. When you select multiple numbers on the slicer, you will see only the draws that contain all the selected numbers.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"390\" height=\"303\" class=\"wp-image-83903\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-58.png\" \/><\/p>\n<h2>Conditional Formatting<\/h2>\n<p>The conditional format is the \u201ccherry on top\u201d of this solution. You can not only filter the drawings, but you can also highlight the numbers selected on the slicer within each line with a different colour.<\/p>\n<p>The numbers selected in the slicer should appear in red or whatever colour you select. This is too complex for the conditional formatting. Due to that, you need a new measure that tells you, for each number in the drawing, if it&#8217;s selected or not.<\/p>\n<p>Since this measure will be used only for conditional filtering, it will be processed for each number and not sets of numbers. However, since it&#8217;s a measure, you still need to apply an aggregation function to the <em>Number<\/em> field, a simple <code>SUM<\/code> will do the job.<\/p>\n<p>The final measure will look like this:<\/p>\n<pre class=\"lang:c# theme:vs2012\">NumFilter\u00a0=\r\nVAR\u00a0tab\u00a0= VALUES\u00a0(Selector[Number]\u00a0)\r\nRETURN\r\n\u00a0\u00a0\u00a0\u00a0IF\u00a0(\u00a0SUM\u00a0(\u00a0Lotto[Number]\u00a0)\u00a0IN\u00a0tab,\u00a01,\u00a00\u00a0)<\/pre>\n<p>The steps to complete the conditional formatting are:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>In the <em>Fields<\/em> pane, Click the <em>&#8216;&#8230;&#8217; (More Options)<\/em> button close to the <em>Lotto<\/em> table<\/li>\n<li>Click the <em>New Measure<\/em> menu option in the context menu that will appear<\/li>\n<li>Paste the entire expression, including the measure name, in the formula bar<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"130\" class=\"wp-image-83904\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-59.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Select the matrix visual on the main pane<\/li>\n<li>In the <em>Visualizations<\/em> pane, with the matrix selected, click the <em>Format<\/em> button<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"394\" height=\"138\" class=\"wp-image-83905\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-60.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>In the <em>Visualizations<\/em> pane, open <em>Conditional<\/em> <em>formatting<\/em><\/li>\n<li>Under the <em>Conditional<\/em> <em>Formatting<\/em> element, enable <em>Font<\/em> <em>color<\/em> option<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"396\" height=\"445\" class=\"wp-image-83906\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-61.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Click on the <em>Advanced<\/em> <em>Controls<\/em> link that will appear below the <em>Font<\/em> <em>color<\/em> option<\/li>\n<li>In the <em>Font<\/em> <em>color<\/em> window, on the <em>Format<\/em> <em>by<\/em> dropdown box, select <em>Rules<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"373\" height=\"116\" class=\"wp-image-83907\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-62.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>On the <em>Based on field<\/em> dropdown box, select the measure, <em>NumFilter<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"591\" height=\"544\" class=\"wp-image-83908\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-63.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>On the <em>If value<\/em> dropdown box select the <em>is<\/em> option<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"280\" height=\"216\" class=\"wp-image-83909\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-64.png\" \/><\/p>\n<ol>\n<li>Type 1 in the textbox besides the previous dropdown<\/li>\n<li>Select <em>Red<\/em> in the color picker, if not selected already<\/li>\n<li>Click <em>Ok<\/em><\/li>\n<\/ol>\n<p>Once you have followed the steps, you should see the selected numbers light up in red or the colour that you selected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"369\" height=\"287\" class=\"wp-image-83910\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/04\/word-image-65.png\" \/><\/p>\n<h2>Summary<\/h2>\n<p>Using some interesting <strong>DAX<\/strong> expressions, each line of a matrix could be filtered according to a slicer. In addition, the numbers selected could be highlighted. Of course, this is a very specific example, but I\u2019m sure you can adapt the expressions shown here to your challenges using the Matrix visual.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Matrix is one of the many built-in visualizations found in Power BI. While the Matrix is easy to populate, filtering it is not so straightforward. In this article, Dennes Torres explains how to add a slicer that does the job.&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143528],"tags":[],"coauthors":[6810],"class_list":["post-83876","post","type-post","status-publish","format-standard","hentry","category-bi-sql-server"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/83876","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\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=83876"}],"version-history":[{"count":9,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/83876\/revisions"}],"predecessor-version":[{"id":94016,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/83876\/revisions\/94016"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=83876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=83876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=83876"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=83876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}