<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vishant on Medium]]></title>
        <description><![CDATA[Stories by Vishant on Medium]]></description>
        <link>https://medium.com/@vishant1203?source=rss-9d8670de5316------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GWlutFps5oZr2iL38JF6Mw.jpeg</url>
            <title>Stories by Vishant on Medium</title>
            <link>https://medium.com/@vishant1203?source=rss-9d8670de5316------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 10:23:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vishant1203/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[TOPN calculation in Power BI]]></title>
            <link>https://medium.com/@vishant1203/topn-calculation-in-power-bi-6b9aecd1a024?source=rss-9d8670de5316------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b9aecd1a024</guid>
            <dc:creator><![CDATA[Vishant]]></dc:creator>
            <pubDate>Mon, 21 Jul 2025 12:01:25 GMT</pubDate>
            <atom:updated>2025-07-21T12:16:20.090Z</atom:updated>
            <content:encoded><![CDATA[<p>Here, we will talk about various TOPN calculations in Power BI. If you are just in begining phase of learning Power BI or feeling stuck with use of TOPN DAX then this post will help you to undertstand concepts in detail.</p><p>Let’s have simple example, we have table name Sales_fact_table of three columns. column 1 → sr.no, column 2 →Product Name, column 3 → Sales and we want to filter TOP1 or TOP2 or TOP 3 products based on their total sales.</p><p>As a first step, create numeric range parameter as shown below, to create dynamic TOP N value selection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YCMHFCMRUwEyMv2tj7MNBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*BD8pfV_IVWfEcFh_ZIpcFQ.png" /></figure><p>Now, i have generated series of 3 digits only and added column with rows Top 1, Top 2, Top 3 as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-yKJ4o3vThTJ_rCzQ2Eh2g.png" /></figure><p>Now, create simple tile slicer and table of products and their sales, The table of which we want to shown up TOP N value, whenever we select respective slicer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UuZG30YUz5iynyeOMsbqeA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1uHBiNSiZ_uLGWo2C8IqDQ.png" /></figure><p>Let’s create another table of distinct values of product with additional row name as “others”(see last row in below snapshot). Why did we create “others” , it is because we want that whenever we click TOP 2 slicer tile then my table should show TOP 2 product with thier sales amount and remaining amount in last row “others” as shown next below snapshot. As last step create one to many relationship of this new table with fact_sales _table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kQg3CwNI7I_ms0y2LW7PhA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*ny80Am3CHCca0RanRfJbpQ.jpeg" /></figure><p>Lets try simple method _1 first, here we will use DAX →</p><p>CALCULATE([Total_sales], <strong>TOPN(SELECTEDVALUE(‘Parameters’[Number]),VALUES(‘Product table’[Product Name]), [Total_sales], DESC)</strong>)</p><p>Total sales is just simple measure of SUM (Sales). TOPN will take first parameter of our selected parameter. Second parameter is table column which needs to be filtered , third parameter is basis of TOP N selection, final parameter is order of selection. Here, TOP N will return table with single column of product names and it will show no. of top products as per selected parameters</p><p>But it will give correct result for total value only (shown on card right side), it will not filter respective products if select any Top 1, Top 2, Top 3(see table below at centre)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C6QaWPnMbPv2zF0t8PrGBA.png" /></figure><p>Now, <strong>let’s see method 2</strong> → instead of using TOPN for filtering , we will use FILTER function to filter specific rows.</p><p>Method 2_Top_N_Product_sales = CALCULATE([Total_sales], FILTER(‘Product table’, RANKX(ALL(‘Product table’), [Total_sales], , DESC,Dense)&lt;=SELECTEDVALUE(‘Parameters’[Number])))</p><p>Here, concept is same as used in method 1, but used Filter function and RANKX function to filter top products as per selected TOP 1 TOP2 , TOP3.</p><p>Filter function will return rows from product table as per condition.</p><p>But problems with method 1 and method 2 are that they are not giving us end result we want. I restate that we want to show remaining values after selecting Top N products in row name “others”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3G1Rv1TiF5Msl7xQ9zKKIw.png" /></figure><p><strong>Method 3 </strong>is used, and DAX is pasted below. Do not go to DAX directly, first think about logic.</p><p>Logic is, we want to have “selected top N value” first, Then we want to have Top N product rows, then we want to have TOP sales corresspond to that products.</p><p>Now for other sales we will subtract top N sales from total sales.</p><p>then i will return value like …if my selected row is other then i want to show other sales calculated above , otherwise i want to have TopN sales.</p><p>For Total to show correct value we always use HASONEVALUE() DAX function, this is widely used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-yWZSGz3Sx-KEcCoROG45Q.png" /></figure><p>May be you can have doubt for keepfilter DAX function, when and where to use it?? KEEPFILTER() helps to have filters which are already applied externally and on top of that it applied addtional filter which we mention in paranthesis.</p><p>Try using with and without KEEPFILTER, then you will able to get it easily.</p><p>Happy learning!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b9aecd1a024" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Time Intelligence DAX functions in Power BI]]></title>
            <link>https://medium.com/@vishant1203/time-intelligence-dax-functions-in-power-bi-5177479e6ddb?source=rss-9d8670de5316------2</link>
            <guid isPermaLink="false">https://medium.com/p/5177479e6ddb</guid>
            <category><![CDATA[business-intelligence]]></category>
            <category><![CDATA[data-analyst]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[power-bi]]></category>
            <dc:creator><![CDATA[Vishant]]></dc:creator>
            <pubDate>Sat, 29 Mar 2025 06:54:50 GMT</pubDate>
            <atom:updated>2025-04-07T12:05:00.148Z</atom:updated>
            <content:encoded><![CDATA[<p>Very simplified explanation of time intelligence functions. Simple and easy to implemented!</p><p>Whenever we see any value in report(whether it is Revenue, Earned value, Profits, Cost), we always want to compare it with previus months or quarters or years or week or any previous period in order to get meaning from that value and make further decision in business operations/ Projects.</p><p>At first, let us see some reports where these functions are used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*rMYmPVCk85QtKBA4fCA0_w.png" /><figcaption>Last 3 month and 6 months comparison</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*FEWh5rNNKLEn9YayAA-Tqg.png" /><figcaption>KPI Comparison with Previous year/months/quarters</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*2zgAPuk8R_lC1XAsFvcEzQ.png" /><figcaption>Revenue comparison with previous year</figcaption></figure><p>Let’s start now:</p><p>First, we will see how can we calculate YTD, MTD, QTD in most simple way.</p><p>For using any time intelligence functions effectively, we should have dimension date table marked as date table in our model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*jTvM60nZgSEHyNL7RTkgFA.png" /><figcaption>Dim_date tables marked as date table</figcaption></figure><p>Now, we will start implement time intelligence DAX functions:</p><h3><strong>Year to Date(YTD):</strong></h3><p>we have following DAX function to calculate ytd:</p><p>Option 1: Revenue_YTD_1 = <strong>TOTALYTD</strong>(Revenue[Revenue],dim_date[Date])</p><p>or</p><p>Option 2: Revenue_YTD_2 =</p><p><strong>VAR YTDDates</strong>=DATESYTD(dim_date[Date])</p><p><strong>RETURN</strong></p><p><strong>CALCULATE</strong>(Revenue[Revenue],YTDDates)</p><p>Results are as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*YCgTN4X1TxmyPI3EG6dm-A.png" /><figcaption><strong>YTD</strong></figcaption></figure><p>What should we use ?</p><p><em>Option 2 gives more flexibility over other option, so better to go for option 2. Same way we can calculate MTD, QTD etc..But remember all mark your dim_date table as date table in PowerBI desktop(as shown above).</em></p><h3><strong>Previous Year(PY):</strong></h3><p>Same thing, we have two option for calculating revenue in previous year as below:</p><p>Option 1 using <strong>SAMEPERIODLASTYEAR</strong>:</p><p>Revenue_PY_1 = CALCULATE(Revenue[Revenue], <strong>SAMEPERIODLASTYEAR</strong>(dim_date[Date]))</p><p>Option 2:</p><p>REVENUE_PY_2 = CALCULATE(Revenue[Revenue],<strong>DATEADD</strong>(dim_date[Date],-1,YEAR))</p><p>we can calculate previous any previous years, months, quarters or semester easily just by small change in DAX function. e.g</p><p>CALCULATE(Revenue[Revenue],<strong>DATEADD</strong>(dim_date[Date],-2,Year))</p><p>CALCULATE(Revenue[Revenue],<strong>DATEADD</strong>(dim_date[Date],-1,Month))</p><p>CALCULATE(Revenue[Revenue],<strong>DATEADD</strong>(dim_date[Date],-1,Quarter))</p><p>CALCULATE(Revenue[Revenue],<strong>DATEADD</strong>(dim_date[Date],-4,Quarter))</p><p><strong><em>So better to go for Option 2, more flexible and easy to remember.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*qjuXTxeY_Mcmk4IvM9FPRA.png" /></figure><h3><strong>PARALLELPERIOD</strong>:</h3><p>Let’s directly jump into examples first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*gkCXcNPM5qbi42N5zeByOg.png" /><figcaption>Parallel period</figcaption></figure><p>column 2 (calculated previous quarter revenue using Revenue PQ_1 = CALCULATE(Revenue[Revenue], DATEADD(dim_date[Date],-1,QUARTER))</p><p>Column 3 calulated using</p><p><strong>Revenue_parallelperiod(QUARTER) =CALCULATE(</strong>Revenue[Revenue], <strong>PARALLELPERIOD</strong>(dim_date[Date],-1,QUARTER)<strong>)</strong></p><p>Then what special in this parallelperiod DAX function: see below speciality in DAX function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KAhsUcxF81SaY9mS4sm0Ow.png" /></figure><p>Parallelperiod takes complete period of last quarter(from first date till last date) whereas in DATEADD function takes only previous 3 months value only, (e.g. if month is Apr, it will take consideration of Jan month , if it is May then Feb,…if Aug then May so on…but parallel period does not..)</p><h3><strong>Sliding window function:(one of the most important function)</strong></h3><p><em>Whenever we have to calulate running total, moving average including current month, moving average excluding current month, last 3 month average, last 6 month, running total then we have to use below concept to arrive at require result.</em></p><p><em>We will calculate only running total here. However, we can use same concept for moving average calulation as well.</em></p><p>Way to calculate running total</p><h3><strong>Option 1- using DATESINPERIOD dax function( including current month):</strong></h3><p>Revenue(for last 3 month incl. current month) =</p><p><strong>VAR</strong> firstseldate=<strong>MAX</strong>(dim_date[Date])</p><p>Return</p><p><strong>CALCULATE</strong>(Revenue[Revenue],<strong>DATESINPERIOD</strong>(dim_date[Date],</p><p>firstseldate,-3, MONTH))</p><h3><strong>Option 2 -same as option 1 but excluding current month:</strong></h3><p><strong>Revenue </strong>(last 3 months excl. current month) =</p><p><strong>VAR</strong> firstseldate=<strong>MIN(dim_date[Date])-1</strong></p><p>Return</p><p><strong>Calculate</strong>(Revenue[Revenue], <strong>DATESINPERIOD</strong>(dim_date[Date],firstseldate,-3,Month))</p><p><em>Above functions are simple and intutive.</em></p><h3><strong>Option 3 -Using DATESBETWEEN dax function:</strong></h3><p>Revenue(last 3 months using DATESBETWEEN) =</p><p><strong>VAR </strong>firseldate=<strong>EOMONTH</strong>(MIN(dim_date[Date]),-3)+1</p><p><strong>VAR</strong> lastseldate=<strong>EOMONTH</strong>(MAX(dim_date[Date]),0)</p><p><strong>Return</strong></p><p><strong>Calculate</strong>(Revenue[Revenue], <strong>DATESBETWEEN</strong>(dim_date[Date],firseldate,lastseldate))</p><p><em>where EOMONTH is END OF MONTH</em></p><p>Now, let’s see what results are generated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Dc8_SvY_q8JofuoyOUhUA.png" /><figcaption>Running total calculation</figcaption></figure><p>Each of option stated above are having different behaviour, so we need to understand their behaviour and apply as per requirement.</p><p>Above DAX function can be easilty modified to get running average including month or excluding month. BTW, same results can be achieved using new visual calculation in built method.However, using DAX gives us more flexibility.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EeVlH2gqzt_6kQ2FNJ5JPg.png" /><figcaption>Running sum or moving average calculation</figcaption></figure><p>some e.g. where some modifications are done in above dax functions to get desired results.</p><ol><li><strong>Last 3 months Average</strong> =</li></ol><p><strong>VAR</strong> firstseldate=min(dim_date[Date])-1</p><p><strong>RETURN</strong></p><p><strong>Calculate</strong>(<strong>Average</strong>(aggregated[Revenue]), <strong>DATESINPERIOD</strong>(dim_date[Date],firstseldate,-3,Month))</p><p>I have created another aggregated table using summarize function to calculate month wise revenue( if i do not calculate month wise revenue then it will create average revenue day wise).</p><p>aggregated = <strong>Summarize(Revenue, dim_date[Year], dim_date[Month_Name],”Revenue”,SUM(Revenue[Revenue_C]))</strong></p><p>2.<strong> Last 6 months Average</strong> =</p><p><strong>VAR</strong> firstseldate=min(dim_date[Date])-1</p><p><strong>RETURN</strong></p><p>Calculate(<strong>average</strong>(aggregated[Revenue]), <strong>DATESINPERIOD</strong>(dim_date[Date],firstseldate,-6,Month))</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*X4AQdG8pCrxdr_05p6NZLQ.png" /></figure><p><strong>Last ’N’ Days Sales:</strong></p><p>Before i finish, i want to discuss one more important aspect of DATESINPERIOD Dax function. It can be used to calculate Last N days sales as well. see example below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*b7W_pxNkeU1X8TWZM1A4Kw.png" /></figure><p><strong>Last 7 days sales </strong>= <strong>CALCULATE</strong>(Revenue[Revenue_M], <strong>DATESINPERIOD</strong>(dim_date[Date], <strong>MAX</strong>(dim_date[Date])-1, -7,DAY))</p><p><strong>Last 30 days sales </strong>= <strong>CALCULATE</strong>(Revenue[Revenue_M], <strong>DATESINPERIOD</strong>(dim_date[Date], <strong>MAX</strong>(dim_date[Date])-1, -30,DAY))</p><p><strong>Conclusion: </strong>Having good understanding of time intelligence DAX functions is very important in order to compare values with past trend.</p><p>Main agenda of this post was to understand behaviour of some important time intelligence DAX function and use them according to our requirements.</p><p>Happy Learning !</p><p>Thanks for reading.</p><p>I will be more happy to discuss more on this. Leave comment below.</p><p>Find me on <a href="https://www.linkedin.com/in/vishantsaini03/">linkedin</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5177479e6ddb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimize PowerBI Model]]></title>
            <link>https://medium.com/@vishant1203/optimize-powerbi-model-a36543acc9a3?source=rss-9d8670de5316------2</link>
            <guid isPermaLink="false">https://medium.com/p/a36543acc9a3</guid>
            <dc:creator><![CDATA[Vishant]]></dc:creator>
            <pubDate>Sat, 15 Mar 2025 06:09:12 GMT</pubDate>
            <atom:updated>2025-03-15T06:27:06.697Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s suppose, you are PowerBI developer and finished preaparing report in Power BI and sit relaxed now. All of sudden, you heard complain from one of user about report slow down and longer time to process report. It will create sudden panic for you and your team. This will be time for your competency check and build reputation among team.</p><p>Let’s discuss what we can do to address this challenge:</p><p>Let’s start with simple examples: Out of two scenarios given below what will you choose?</p><p>Scenario 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*aQK9w1nI-0bEiMBa3-fBBA.png" /><figcaption>Sales Table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/1*eoe2bsBqfNNOY2uJm9XWug.png" /><figcaption>Calculate High Discount Sales</figcaption></figure><p>Scenario 2: Create another column of discount category then apply DAX</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*GM0lGLJ5BJPMfDme4vuGug.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*7Zmh5XdAKntCMb8NfNw4ww.png" /></figure><p>I have to work in Excel then i would have definitely choose scenario 1(Very intutive) but in Power BI it is bit tricky, in scenario 1 we have used only BI formule engine and scenario 2 , we have used vertipaq storage engine and formule engine both.</p><p>Now, Let’s see another example and tell which you would go for.</p><p>Scenario 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/489/1*xv2uZzOO2SAsMpPf4B8zLA.png" /><figcaption>Scenario 1 of caculating running total</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*gjPjGNOaKUSd_4Hm63JLww.png" /><figcaption>Scenario 2 of calculating runnung total</figcaption></figure><p>I would defintely go for scenario 1 because it is so simple and intutive but reality is that in scenario 1, vertipaq storage engine retreive entire sales table though it required only two columns from that table whereas in scenario 2, vertipaq storage engine retrieve only two columns from given table.</p><p>Now, Let’s understand Vertipaq storage and formule engine both.</p><ol><li>Vetipaq storage is multi threaded ( it can use additional CPUs to increase speed).</li><li>Vertipaq storage engine store data in columnar database(storing data in columns) using value encoding, hash encoding or run length encoding etc.</li><li>Vertipaq storage engine really good at two things filtering and aggregation. filtering done on dimension table and aggregation done on transaction table.(can relate example 1 here)</li><li>Vertipaq storage engine send data to formule engine.</li><li>Formule engine handle DAX calculations, is single threaded(can use only one CPU), slow at doing things.</li><li>Let formule engine delegate as much it can to vertipaq storage engine so both can use optimally.</li></ol><p>For Performance tuning, we need to focus on three areas:</p><p>a. Data Refresh</p><p>b. Model Calulation</p><p>c. Visualization rendering</p><h4><strong>Data Refresh:</strong></h4><ol><li>Try to have query folding done for all transformation performed in query editor.</li><li>For calculated columns, we have two options either create them in Source itself or Power query or using dax. Always go for option 1 or 2 as compressibility factor reduce by 90% in PBI desktop. DAX calulated columns are stored inefficiently, lead to bigger file size and slow reports.</li></ol><h4><strong>Model Calculation:</strong></h4><ol><li>Use PowerBI analyzer, it will tell how muc time is spent in running DAX and how much time spent in rendering visual.</li><li>Remove data not needed. Remove unneccassary columns more beneficial than removing rows as vertipaq storage engine store data in columnar database(store in column rather than rows).</li><li>Try to use more and more variables, calculated columns and tables.</li><li>Select right data type/ e.g. convert Date/time datatype into date only if possible, convert text data type(12,34,45) to number data type as number’s compressibility factor much higher than text data type)</li><li>Use incremental refresh</li><li>Aggregation on data whereever possible (means fewer rows, reduced size, lower carinality and hence better performance).</li></ol><p><strong><em>Hint: Understand functionality of vertipaq storage engine and formulae engine to make things easy instead of just mugging up.</em></strong></p><h4>Visualization Rendering:</h4><ol><li>Keep maximum 20 elements on page, even simple lines can slow down rendering little bit.</li><li>Use drill through pages or report tooltips to pre-filter data instead of standard tooltip for better performance.</li></ol><p>Thanks for reading.</p><p>I will be more happy to discuss more on this. Leave comment below.</p><p>Find me on <a href="https://www.linkedin.com/in/vishantsaini03/">linkedin</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a36543acc9a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>