If you are an avid report designer or user, you may have wanted to see the percent difference between one row and the previous one. Sounds easy right? Well let’s break it down. Percent difference follows a consistent formula: (current value – previous value)/previous value. Current value? Easy peasy. Previous value? That’s where this blog comes into play. It’s fairly simple to get the previous month value or previous date period value because Power BI can tell it exactly what the filter would be (September minus one month is always August ). However, there may not have been any invoices in August, so how can we compare September with the last month that had an invoice?
Let’s set the scene for this example. A business user has requested to compare each day’s orders with the previous day they had orders. To get the previous day that had orders, we can create a measure called Last Day Order Quantity (see below). Within this measure, you’ll notice we use a variable to calculate the measure Order Quantity. By pulling this into the variable, it will save the row context for Order Quantity so we can make sure that we are only looking at rows that have orders. The next variable (LastDayOrdered) filters the ship date table to grab the last date where 1. there are orders (Order Quantity is not blank) and 2. the current day (aka the row we are currently on) also has orders. The final variable does the work of calculating our order quantity for the last day that contained orders.
Last Day Order Quantity = VAR Orders = [Order Quantity] //grab the charge amount for this line VAR LastDayOrdered = MINX(FILTER(ALL('ShipDate'),'ShipDate'[DaysAgo]>MAX('ShipDate'[DaysAgo]) &&[Order Quantity] <> BLANK() && Orders<>BLANK()),'ShipDate'[DaysAgo]) VAR LastDayOrders = CALCULATE([Order Quantity],FILTER(ALL('ShipDate'),'ShipDate'[DaysAgo]=LastDayOrdered)) RETURN LastDayOrders
As you can see in the screenshot above, there is a gap in ship dates between 1/25/2014 and 1/28/2014 and the last day order quantity pulls in the amount from 1/25/2014 (1) instead of grabbing it from 1/27/2014 (0). Sweet! Now that makes finding the % difference in order quantity very simple. Below is the full code from this example and also a more parameterized version with tons of comments to help you use it as needed. Happy coding!
% Difference Order Quantity = VAR Orders = [Order Quantity] //grab the charge amount for this line VAR LastDayOrdered = MINX(FILTER(ALL('ShipDate'),'ShipDate'[DaysAgo]>MAX('ShipDate'[DaysAgo]) &&[Order Quantity] <> BLANK() && Orders<>BLANK()),'ShipDate'[DaysAgo]) VAR LastDayOrders = CALCULATE([Order Quantity],FILTER(ALL('ShipDate'),'ShipDate'[DaysAgo]=LastDayOrdered)) RETURN DIVIDE(Orders-LastDayOrders,LastDayOrders,0)
% Difference = //Make a variable to grab the value of your measure at your current line (we will use this later) VAR Desired_Measure = [Order Quantity] //Now we need to grab the time where this desired measure was not null //In my example, ship date is the column that I want to see the previous order quantity sliced by VAR Last_Time_Measure_Had_Value = // I use MINX here to ensure I get the previous day, not the next day. Feel free to flip this as desired. MINX(FILTER(ALL('ShipDate') //this makes sure that I'm only grabbing ship dates that are before my current ship date. ,'ShipDate'[DaysAgo] > MAX('ShipDate'[DaysAgo]) //this makes sure that the options for days ago only include ones that we had orders on, AKA the desired measure is not NULL for this day. //DO NOT USE Desired_Measure here because desired_measure will only look at your current row and we are trying to look at all the rows to make sure we are grabbing a non-NULL one. &&[Order Quantity] <> BLANK() //this checks that the current row is not blank. && Desired_Measure<>BLANK()) //I need this variable to return the smallest number of days ago (hence the MINX at the beginning) that meets the criteria above //For your use, you should swap daysago with whatever field you are hoping to slice and dice by ,'ShipDate'[DaysAgo]) //This final variable calulcates my desired measure (order quantity) and filters my table for the last time that measure had a value. VAR Last_Instance_Of_Calculated_Measure = CALCULATE([Order Quantity],FILTER(ALL('ShipDate'),'ShipDate'[DaysAgo]=Last_Time_Measure_Had_Value)) //Easy peasy now! We can take our current days orders - last day we had orders number of orders, divded by the last day we had orders number of orders to get the % growth day over day. Phewf! RETURN DIVIDE(Desired_Measure - Last_Instance_Of_Calculated_Measure,Last_Instance_Of_Calculated_Measure,0)