<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>DAX on Hat Full of Data</title><link>https://hatfullofdata.blog/categories/dax/</link><description>Recent content in DAX on Hat Full of Data</description><generator>Hugo -- gohugo.io</generator><language>en-gb</language><lastBuildDate>Mon, 22 Sep 2025 09:07:43 +0000</lastBuildDate><atom:link href="https://hatfullofdata.blog/categories/dax/index.xml" rel="self" type="application/rss+xml"/><item><title>Power BI – Use ISINSCOPE to improve a Matrix</title><link>https://hatfullofdata.blog/power-bi-use-isinscope/</link><pubDate>Mon, 22 Sep 2025 09:07:40 +0000</pubDate><guid>https://hatfullofdata.blog/power-bi-use-isinscope/</guid><description>&lt;img src="https://hatfullofdata.blog/power-bi-use-isinscope/cover.png" alt="Featured image of post Power BI – Use ISINSCOPE to improve a Matrix" /&gt;&lt;p&gt;I recently had a request for totals in a Matrix to be shown for some columns and not for other columns. My solution was to use ISINSCOPE in a Matrix to work out which level of the matrix to choose what to return.&lt;/p&gt;
&lt;h2 id="scenario"&gt;Scenario
&lt;/h2&gt;&lt;p&gt;At an event you sell tickets. So for the Cheese and Wine evening we can see 62 tickets are sold so we expect 62 people. BUT some tickets are for multiple people such as Family Ticket is 2 adults and 3 children so the 55 tickets sold for the Christmas Fayre means very little as a Family ticket is 5 people and a Group ticket is 10. So total tickets for more than one ticket type does not make sense and is misleading.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Matric of Event, Ticket Type, Tickets Sold and Total Sales including a total for Total Tickets." class="gallery-image" data-flex-basis="316px" data-flex-grow="131" height="516" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-use-isinscope/image.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;So I’d like the Tickets Sold measure to not show a value on rows combining the ticket types, ie the total rows on this matrix.&lt;/p&gt;
&lt;h2 id="determining-the-ticket-type-rows"&gt;Determining the Ticket Type Rows
&lt;/h2&gt;&lt;p&gt;The first step is to determine which rows are the ticket type rows and which rows aren’t. For this we can use the DAX function ISINSCOPE.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="353px" data-flex-grow="147" height="462" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-use-isinscope/image-1.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;For demo purposes I created a measure that returns a true or false using the function ISINSCOPE. We can see it returns true on the ticket type lines but not the event or total lines. If we want to show all the layers we could look at all the fields in the matrix in a measure. Make sure you start with the lowest level first, so in my case Ticket Type.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="383px" data-flex-grow="159" height="426" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-use-isinscope/image-2.png" width="680"&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Scope =
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;SWITCH (
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; TRUE (),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ISINSCOPE ( Tickets[TicketType] ), &amp;#34;Ticket Type&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ISINSCOPE ( Event[Event Name] ), &amp;#34;Event&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;Total&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;As you can see, using the above measure we can tell which level of the matrix a value is on. So we only need to return total tickets if the ticket type is in scope and we can return nothing on other levels.&lt;/p&gt;
&lt;h2 id="final-measure"&gt;Final Measure
&lt;/h2&gt;&lt;p&gt;We can use a simple if statement based on the ISINSCOPE for Ticket Type. That works really well and the customer were partly happy.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Tickets Sold =
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;IF (
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ISINSCOPE ( Tickets[TicketType] ),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; SUM ( Bookings[Qty] )
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="Matrix with no total showing for the Total Tickets sold" class="gallery-image" data-flex-basis="361px" data-flex-grow="150" height="451" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-use-isinscope/image-3.png" width="680"&gt;&lt;/p&gt;
&lt;h2 id="next-request"&gt;Next Request
&lt;/h2&gt;&lt;p&gt;Once we showed that could be done they wanted to combine the 2 columns of numbers. The request was to not have a separate Total Sales column but to show the Total Sales value as in the total row for each event and the grand total at the bottom. For this we wrote a new measure called Event Sales.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Event Sales =
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;IF (
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ISINSCOPE ( Tickets[TicketType] ),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; SUM ( Bookings[Qty] ),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; SUMX ( Bookings, Bookings[Qty] * RELATED ( Tickets[Price] ) )
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="Matrix showing total sales value in the total row and numbers of tickets on the ticket type rows. All the numbers are formatted 0.00" class="gallery-image" data-flex-basis="275px" data-flex-grow="114" height="538" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-use-isinscope/image-4.png" width="618"&gt;&lt;/p&gt;
&lt;p&gt;We can see in the matrix above the money value appears in the total rows and the number of tickets in the ticket type rows. But they are all formatted the same so it is not clear what the numbers mean.&lt;/p&gt;
&lt;h2 id="adding-dynamic-formatting"&gt;Adding Dynamic Formatting
&lt;/h2&gt;&lt;p&gt;This is not going to be a full description of dynamic formatting, that needs its own post. For the Event sales measure we select Dynamic formatting. In the formula we use the same pattern as the measure for the two different formats.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Using ISINSCOPE for the dynamic format of the measure" class="gallery-image" data-flex-basis="766px" data-flex-grow="319" height="213" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-use-isinscope/image-5.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Final matric showing totals with a currency format and ticket totals with a whole number" class="gallery-image" data-flex-basis="282px" data-flex-grow="117" height="542" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-use-isinscope/image-6.png" width="639"&gt;&lt;/p&gt;
&lt;h2 id="resources"&gt;Resources
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Microsoft Learn ISINSCOPE – &lt;a class="link" href="https://learn.microsoft.com/en-us/dax/isinscope-function-dax?wt.mc_id=DX-MVP-5003563" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/en-us/dax/isinscope-function-dax&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;DAX Guide ISINSCOPE – &lt;a class="link" href="https://dax.guide/isinscope/" target="_blank" rel="noopener"
 &gt;https://dax.guide/isinscope/&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Hat Full of Data Dynamic Formatting – Its coming!&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Microsoft Learn Dynamic Formatting – &lt;a class="link" href="https://learn.microsoft.com/en-us/power-bi/create-reports/desktop-dynamic-format-strings?wt.mc_id=DX-MVP-5003563" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/en-us/power-bi/create-reports/desktop-dynamic-format-strings&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;SQLBI – &lt;a class="link" href="https://www.sqlbi.com/articles/introducing-dynamic-format-strings-for-dax-measures/" target="_blank" rel="noopener"
 &gt;https://www.sqlbi.com/articles/introducing-dynamic-format-strings-for-dax-measures/&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;Adding ISINSCOPE into your repertoire of functions opens up lots of possibilities. For the start of this post it removed a confusing total, in the development of that Matrix it added an extra dimension to a measure. Be careful to not make it too confusing, I know some will dislike the one column having two values in it.&lt;/p&gt;</description></item><item><title>Power BI – Counting Active records easily</title><link>https://hatfullofdata.blog/power-bi-counting-active-records-easily/</link><pubDate>Mon, 15 Sep 2025 16:58:48 +0000</pubDate><guid>https://hatfullofdata.blog/power-bi-counting-active-records-easily/</guid><description>&lt;img src="https://hatfullofdata.blog/power-bi-counting-active-records-easily/cover.png" alt="Featured image of post Power BI – Counting Active records easily" /&gt;&lt;p&gt;Recently in a few Power BI projects and training courses we’ve needed to create a measure for counting active records based on start and end dates for a time period. This has varied from active projects, active employees to active contracts. All of them have had the common feature of start and end dates.&lt;/p&gt;
&lt;h2 id="scenario"&gt;Scenario
&lt;/h2&gt;&lt;p&gt;&lt;img alt="diagram showing tasks with the active tasks highlighted in blue" class="gallery-image" data-flex-basis="297px" data-flex-grow="124" height="548" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-counting-active-records-easily/image-15.png" width="680"&gt;&lt;/p&gt;
&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th&gt;Task&lt;/th&gt;
					&lt;th&gt;Start&lt;/th&gt;
					&lt;th&gt;End&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td&gt;A&lt;/td&gt;
					&lt;td&gt;01-May&lt;/td&gt;
					&lt;td&gt;20-May&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;B&lt;/td&gt;
					&lt;td&gt;15-May&lt;/td&gt;
					&lt;td&gt;15-Jun&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;C&lt;/td&gt;
					&lt;td&gt;5-Jun&lt;/td&gt;
					&lt;td&gt;25-Jun&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;D&lt;/td&gt;
					&lt;td&gt;20-Jun&lt;/td&gt;
					&lt;td&gt;20-Jul&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;E&lt;/td&gt;
					&lt;td&gt;10-Jul&lt;/td&gt;
					&lt;td&gt;25-Jul&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;F&lt;/td&gt;
					&lt;td&gt;25-Jun&lt;/td&gt;
					&lt;td&gt;15-Jul&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;We have 6 tasks A-F. We want to know how many tasks are active in June. In the list in table above and shown in the image the 2 that don’t count are task A, that finishes before June starts and task E that starts after June finishes. I have a separate calendar table called Calendar.&lt;/p&gt;
&lt;p&gt;Understanding the logic of the filters needed is important when writing a measure. For a task to be active in a period, the task must finish after the start of the period and the task must start before the period ends.&lt;/p&gt;
&lt;h2 id="dax-code-for-counting-active-records"&gt;DAX Code for counting Active Records
&lt;/h2&gt;&lt;p&gt;In DAX we are going to use CALCULATE function to apply the above filters. _MinDate and _MaxDate give us the start and end of the period.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Active Tasks = 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;VAR MinDate = MIN(&amp;#39;Calendar&amp;#39;[Date])
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;VAR MaxDate = MAX(&amp;#39;Calendar&amp;#39;[Date])
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;VAR Result = 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; CALCULATE(
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; COUNTROWS(Tasks),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Tasks[Finish] &amp;gt;= MinDate,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Tasks[Start] &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;= MaxDate
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; )
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;RETURN Result
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;This code does not rely on any relationships between the tasks and calendar tables. If there is a relationship that is altering the calculation a CROSSFILTER function can be used to set the relationship to None.&lt;/p&gt;
&lt;p&gt;It can be used in a chart to show 3 active tasks in May, 4 in June and back to 3 in July.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Chart showing counting Active records by Month" class="gallery-image" data-flex-basis="387px" data-flex-grow="161" height="421" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://hatfullofdata.blog/power-bi-counting-active-records-easily/image-16.png" width="680"&gt;&lt;/p&gt;
&lt;p&gt;This is a simple example of using CALCULATE and creating a useful measure. One I’ve taught a few times and promised to document.&lt;/p&gt;
&lt;h2 id="resources"&gt;Resources
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://learn.microsoft.com/en-us/dax/calculate-function-dax?wt.mc_id=DX-MVP-5003563" target="_blank" rel="noopener"
 &gt;Calculate Function on Microsoft Learn&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://learn.microsoft.com/en-us/dax/crossfilter-function-dax?wt.mc_id=DX-MVP-5003563" target="_blank" rel="noopener"
 &gt;Crossfilter Function on Microsoft Learn&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://hatfullofdata.blog/power-bi-dax-crossfilter-function/" target="_blank" rel="noopener"
 &gt;Crossfilter on this blog&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="conclusion-on-counting-active-records"&gt;Conclusion on Counting Active Records
&lt;/h2&gt;&lt;p&gt;Yes this is another post on me being lazy to answer a question I end up answering lots. Understanding how you can use CALCULATE and how to work out the right filters for your logic is the crux of understanding quite a bit of DAX. Something I am still learning every day still working on.&lt;/p&gt;</description></item></channel></rss>