DAX Cumulative Total With Date Filters - excel

I am trying to calculate a running total where orders are only valid during a certain date range. Each order has a value, a start date and an end date. I want to calculate the cumulative sum of the order's values only during the dates between an order's start date and end date.
I've read over this article on cumulative totals and have an equation for the running total but I can't figure out how to filter the equation so that it filter's out an order once the date table is past the order's End Date. The current measure I have is Cumulative Value:=CALCULATE(SUM(Orders[Vaue]), FILTER(ALL('Date'), [Date] <= MAX([Date]))) and I want to add a filter that filters out any orders with an end date past the current date row, similar to this Filter('Order', 'Orders'[Order_End_Date] < 'Date'[Date]). When I try to add this filter though I get an error since 'Date'[Date] is not used in any aggregation.
Below is the data model that I am using and a link to the Excel File with the data model.
The sample Data:
+-----------+
| Date |
+-----------+
| 1/1/2015 |
| 1/2/2015 |
| 1/3/2015 |
| 1/4/2015 |
| 1/5/2015 |
| 1/6/2015 |
| 1/7/2015 |
| 1/8/2015 |
| 1/9/2015 |
| 1/10/2015 |
+-----------+
+----------+------+------------------+----------------+
| Order_Id | Vaue | Order_Start_Date | Order_End_Date |
+----------+------+------------------+----------------+
| 1 | 1 | 1/1/2015 | 1/3/2015 |
| 2 | 3 | 1/2/2015 | |
| 3 | 6 | 1/3/2015 | 1/7/2015 |
| 4 | 7 | 1/5/2015 | |
+----------+------+------------------+----------------+
And the output of the current measure I have and what the correct measure's output should be.
+-----------+-----------------+--------------------------+
| Date | Current Measure | Desired Measure's Output |
+-----------+-----------------+--------------------------+
| 1/1/2015 | 1 | 1 |
| 1/2/2015 | 4 | 4 |
| 1/3/2015 | 10 | 9 |
| 1/4/2015 | 10 | 9 |
| 1/5/2015 | 17 | 16 |
| 1/6/2015 | 17 | 16 |
| 1/7/2015 | 17 | 10 |
| 1/8/2015 | 17 | 10 |
| 1/9/2015 | 17 | 10 |
| 1/10/2015 | 17 | 10 |
+-----------+-----------------+--------------------------+

Cumulative Value2:=CALCULATE(
SUM(Orders[Vaue])
,FILTER(
VALUES(Orders[Order_Start_Date])
,Orders[Order_Start_Date] <= MAX('Date'[Date])
)
,FILTER(
VALUES(Orders[Order_End_Date])
,ISBLANK(Orders[Order_End_Date])
|| Orders[Order_End_Date] >= MAX('Date'[Date])
)
)
Model Diagram (note I took out your date relation - for the limited use case you've provided, it only makes things more complicated):
Note: I will refer to function arguments positionally, with the first argument represented by (1).
So, what we're doing is similar to what you were trying. We've got two FILTER()s, each as an argument to our CALCULATE(). CALCULATE() combines its arguments (2)-(n) in a logical and.
The first FILTER() does essentially what you were already doing, except we are filtering the distinct values of the [Order_Start_Date], comparing them against the current filter context of the pivot table.
The second FILTER() loops over the distinct values of [Order_End_Date], checking two conditions combined in a logical or. We must handle the case of a BLANK [Order_End_Date]. This BLANK is normally implicitly converted to 0 == 1899-12-30, which is less than any date we're considering. In the case of a BLANK, we get a true value from ISBLANK() and the row is returned as a part of FILTER()'s resultset. The other test is simply checking that [Order_End_Date] is greater than the current filter context date in the pivot.

What you are looking for is often called the "event in progress" problem. Here are some posts that will help you to solve your problem.
a solid summary of the problem
a special case
guess this will help on first sight
if you can't get enough - read the complete white paper
I hope this helps.
-Tom

Related

DAX - Cumulative total without date-formatted source data

I am trying to implement a YtD measure for my report in Excel with Power Pivot. My source looks roughly like this:
Table 1
| Month | Store | Branch | Article | Value |
|----------|-------|--------|---------|-------|
| January | 1 | A | Sales | 200 |
| January | 1 | A | Costs | 100 |
| January | 1 | A | Rent | 10 |
| February | 1 | A | Costs | 20 |
| February | 1 | A | Sales | 80 |
| March | 1 | A | Costs | 30 |
| March | 1 | A | Sales | 80 |
| February | 2 | B | Sales | 100 |
| February | 2 | B | Costs | 40 |
| February | 2 | B | Rent | 20 |
Linked to it, are a table Table 2 of months (name - number from 1 to 12), a table Table 3 of unique articles and a table Table 4 of unique stores with their branches.
I want to be able to display YtD for every article depending on the chosen month.
I have measures:
Val. := sum(table1[Value])
YtD1:= calculate(Val., all('Table 2'[Name]))
The former sums across all the values, which are filtered by article in my pivot report. The latter calculates a YtD across all months. It works, but I have to rewrite it so that it responds to filtering the last month and sums from the first month to the selected month.
I have tried to format month numbers to process them as dates (e.g. first day of the month), but couldn't appropriately handle the FORMAT function.
I have also tried to do a sum of months, i.e.:
YtD2= calculate(Val., filter(Table2;Table2[Number]<=2))
which, I hoped, would count months from January to February. It doesn't seem to do any good, resulting in numbers I cannot explain.
My desired output should look like this:
| Store | Sales | | Costs | |
|-------|-------|-----|-------|-----|
| | Val. | YtD | Val. | YtD |
| 1 | 80 | 280 | 20 | 120 |
| 2 | 100 | 100 | 40 | 40 |
if data is filtered by February.
Or
| Store | Sales | | Costs | |
|-------|-------|-----|-------|-----|
| | Val. | YtD | Val. | YtD |
| 1 | 160 | 360 | 50 | 150 |
| 2 | 100 | 100 | 40 | 40 |
if February and March are selected (Val. is displayed for February and March, but YtD from January to March).
Is there a way to implement this in DAX? Can this be done without conversion from month names (or numbers) to some date&
If not, can I get it to work for a month filter instead of a month slicer? That is, if only one month can be selected.
I cannot use variables and similar Power BI features.
Try:
YTD :=
VAR MaxSelectedMonth =
MAX( Table2[Number] )
RETURN
CALCULATE(
[Val.],
FILTER(
ALL( Table2 ),
Table2[Number] <= MaxSelectedMonth
)
)

How to create a column showing if a value is in the bottom 10 values?

I have a large data set that contains details about objects that are currently on an extension. The extensions are given a specific due date. Some of the extensions are past their due date.
I'm struggling to work out how to create a column in PowerPivot for O365 Excel that will return a yes/no value depending on if the object is one of the 5 most overdue extensions. So far nothing I've tried has worked at all.
Example with fake data:
+-----------+---------+--------------------+------------+
| ID | Urgency | Bus Days Remaining | Due Date |
+-----------+---------+--------------------+------------+
| 118017544 | Overdue | -487 | 1/04/2017 |
| 34960939 | Overdue | -97 | 30/09/2018 |
| 10695082 | Overdue | -364 | 20/09/2017 |
| 166236826 | Overdue | -86 | 15/10/2018 |
| 166236826 | Overdue | -86 | 15/10/2018 |
| 34944450 | Overdue | -437 | 9/06/2017 |
| 69427293 | Overdue | -446 | 29/05/2017 |
| 56280961 | Overdue | -437 | 9/06/2017 |
| 12535364 | Overdue | -176 | 11/06/2018 |
| 46296100 | Overdue | -163 | 28/06/2018 |
| 171666963 | Overdue | -122 | 24/08/2018 |
+-----------+---------+--------------------+------------+
The calculated column should be able to put a "Yes" next to 5 rows in this data that are the oldest.
Factors that might be important:
Multiple extensions can share a due date but be separate extensions. This makes me think that the formula needs to be based off of the "Bus Days Remaining" column value
Excel has a function in Pivot Tables where you only show the Top 10 values. This isn't an option for me because using that filter means you cannot drill into the Pivot Tables data.
Any help you could provide would be great :)
Thanks in advance
Please try this formula.
=C2<=SMALL(C$2:C$12,5)
If the 5th and 6th smallest are equal the formula will return TRUE for more than 5 items.

Formula to get the month of the last value

I have sales data by customer as follows:
| - | A | B | C | D | E | F | G |
|---|---------------|--------|--------|--------|--------|--------|--------|
| 1 | Customer Name | Jan-18 | Feb-18 | Mar-18 | Apr-18 | May-18 | Jun-18 |
| 2 | Mr.A | 1000 | 500 | 0 | 200 | 0 | 0 |
| 3 | Mr.B | 0 | 300 | 200 | 0 | 0 | 100 |
I need the formula to know the last sales of the respective customer booked (the name of the month)
in this case, Mr. A last order is in Apr-18 while Mr.B is in Jun-18.
I have 2,000 plus customer and sales data since Apr 2016 up to last month, it will be a huge time saving to have a formula to help.
Assuming your 'months' are dates, not Text. Courtesy #barry houdini:
=LOOKUP(2,1/(B2:G2<>0),B$1:G$1)
in Row2 and copied down to suit, formatted mmm-yy.
Ref
An alternative to using LOOKUP() as in this answer, not sure what impact it has performance-wise as both need to create an array but I would take a stab in the dark that this is less performant:
=INDEX($B$1:$G$1,,MAX((B2:G2<>0)*COLUMN(B2:G2)-1)) - Ctrl+Shift+Enter
Ofcourse this could be edited to a dual lookup on the customer too:
=INDEX($B$1:$G$1,,MAX(INDEX(($B$2:$G$3<>0)*COLUMN($B$2:$G$3)-1,MATCH("Mr.B",$A$2:$A$3,0),0)))
This doesn't require the CSE as INDEX() handles the array manipulation

VBA Date cleanup / reformatting

I'm relatively new to VBA coding and wanted to get some ideas as to how I could do some data cleanup/reformatting. I have a excel data export from a system that has very little business logic/validation.
As a result I have a Date column that has data integrity issues that I have examples of below. Dates are not formatted the same consistently, there is dates combined with text strings, in some cases only text (in the date field)
Here are examples of the data I have in the Date column:
2/2/2018
8/3/2018
1996
1990-1991
02/29/95
1992-93
05/08/200
DECLINED
5/1418
8/14/2018
06/09/200
1/12/94, DECLINED CONTRACT 12/01/00
EXP CAT I
06/14/23018
1996
5-1-1207/07/92
8/3/2018
3-10-
1996
02/27/187
1-29-14
2/2/2018
1-4-11
3.8.99
2-17-12
10-6-16
I would like to convert the dates into the MM/DD/YYYY format. I realize where I just have pure text (e.g. 'DECLINED') that there is no way to extract a date, however I'm hoping for the other examples it may be possible to format the date to the above.
Some of the dates are plain no good (e.g. '5/1418' can't determine how to translate this), but I'm hoping for at least the dates formatted with MM-DD-YYYY and MM.DD.YYYY and similar combinations there is a way to convert their formatting, as well as where I just have 1 digit Month and Day (e.g. 2/2/2018 should be 02/02/2018). If just a 4 digit year is provided I want to convert to '01/01/(year)' Any ideas are appreciated.
This is one of those problems that you could spend a year trying to solve to get a 100% solution. The good news is you can get super lazy and have a nice 60% solution by using the VBA CDATE() function which makes a good guess for whatever you feed it. Tossing Split() at it to peel off extra words and whatnot (that may follow the date with a space or a comma) you can get most of the actual dates covered here. The remaining records are either dates that are so badly formatted that you will have to write code for the edge case, or it's just garbage non-date stuff you can ignore.
Create a new module in VBA and pop this in:
Public Function dateguesser(inDate As String) As Date
dateguesser = CDate(Split(Split(inDate, " ")(0), ",")(0))
End Function
Then in your sheet you can use this as a new function
=dateguesser(A1)
And copy down. For your list, you get the following:
+----+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | A | B |
+----+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | 2/2/2018 | 2/2/2018 |
| 2 | 8/3/2018 | 8/3/2018 |
| 3 | 1996 | 6/18/1905 |
| 4 | 1990-1991 | #VALUE! |
| 5 | 02/29/95 | #VALUE! |
| 6 | 1992-93 | #VALUE! |
| 7 | 05/08/200 | ############################################################################################################################################################################################################################################################### |
| 8 | DECLINED | #VALUE! |
| 9 | 5/1418 | ############################################################################################################################################################################################################################################################### |
| 10 | 8/14/2018 | 8/14/2018 |
| 11 | 06/09/200 | ############################################################################################################################################################################################################################################################### |
| 12 | 1/12/94, DECLINED CONTRACT 12/01/00 | 1/12/1994 |
| 13 | EXP CAT I | #VALUE! |
| 14 | 06/14/23018 | #VALUE! |
| 15 | 1996 | 6/18/1905 |
| 16 | 5-1-1207/07/92 | #VALUE! |
| 17 | 8/3/2018 | 8/3/2018 |
| 18 | 3-10- | #VALUE! |
| 19 | 1996 | 6/18/1905 |
| 20 | 02/27/187 | ############################################################################################################################################################################################################################################################### |
| 21 | 1-29-14 | 1/29/2014 |
| 22 | 2/2/2018 | 2/2/2018 |
| 23 | 1-4-11 | 1/4/2011 |
| 24 | 3.8.99 | #VALUE! |
| 25 | 2-17-12 | 2/17/2012 |
| 26 | 10-6-16 | 10/6/2016 |
+----+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Clearly this is just a starting point, but I think it's a good solid starting point. The remaining crap you can start writing edge cases for in your VBA, but the closer you get to 100% the longer it's going to take to get any further and before you know it you'll be a month into this and wondering what's happened to your life.

Distinct Count of weeks where sum is greater than 0 PowerPivot

I have a table with the following criteria.There are multiple week endings for multiple projects. The hour/cost column can be positive or negative and are correlated (positive hours/positive costs). I'm able to get a distinct count of week ending for the project if there are any costs to it(+or-), but I want to get a distinct count only if the sum of hours or costs are positive.
Since charges can be + or -, and potentially cancel out for a week ending, this would alter my average formula if it were to count a week with sum of 0.
I'm trying to build a calculated field that I could add to my Pivot table that lists the actual charged weeks when I filter by Project. In the sample, there are 4 unique dates with sum greater than 0, but my current formula gives me 7 unique dates, disregarding positive sum.
WeekEndDate| Project | Hours | Cost
Sample Data
+---------------+---------+--------+-------------+
| WeekEndDate | Project | Hours | Cost |
+---------------+---------+--------+-------------+
| 10/7/16 | C7119A | 2.00 | $122.00 |
| 10/7/16 | C7119A | 32.00 | $1,952.00 |
| 10/7/16 | C7119A | 1.50 | $91.50 |
| 10/7/16 | C7119A | -32.00 | ($1,952.00) |
| 10/14/16 | C7119A | 10.00 | $610.00 |
| 10/14/16 | C7119A | -10.00 | ($610.00) |
| 10/21/16 | C7119A | 19.50 | $1,189.50 |
| 10/21/16 | C7119A | -19.50 | ($1,189.50) |
| 10/28/16 | C7119A | 2.00 | $122.00 |
| 10/28/16 | C7119A | 3.00 | $183.00 |
| 10/28/16 | C7119A | -3.00 | ($183.00) |
| 10/28/16 | C7119A | -2.00 | ($122.00) |
| 11/4/16 | C7119A | 1.00 | $61.00 |
| 11/11/16 | C7119A | 3.50 | $213.50 |
| 1/13/17 | C7119A | 3.00 | $183.00 |
+---------------+---------+--------+-------------+
You can do this using SUMMARIZE().
First create a calculated column to get a distinct ID on the project/week combo:
ProjectWeek =CONCATENATE([Project],[Week End Date])
Then create a calculated measure to count the weeks with positive sum of hours:
Positive Project Weeks:= COUNTROWS(Filter(ADDCOLUMNS(
SUMMARIZE('Project Costs','Project Costs'[Week End Date]),
"WeekSum", MAXX(Values('Project Costs'[ProjectWeek]),
CALCULATE(Sum([Hours]),ALL('Project Costs'[Hours])))),
[WeekSum] >0))
SUMMARIZE creates a table that contains the distinct WeekEndDate values.
ADDCOLUMNS creates a column in that table that shows the sum of hours for that WeekEndDate.
Filter removes the rows in our virtual table that have a sum of hours less than or equal to 0.
Countrows then counts the rows left in the table, which is equal to the distinct count of end dates.
If you don't create the calculated column for ProjectWeek and instead use WeekEndDate in the MAXX(VALUES()) functions, you will get a total of 7 instead of 4.

Resources