DAX - Cumulative total without date-formatted source data - excel

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
)
)

Related

Dax measure for monthly running total on weekly granularity

I have to visualize an accumulated monthly budget on a daily level.
Means a running total for the individual month on a daily basis.
The budget targets are provided in a table like this:
+------+------------+--------+
| Area | Year-Week | Budget |
+------+------------+--------+
| A | 2020-01 | 50 |
| A | 2020-02 | 25 |
| A | 2020-03 | 50 |
| A | 2020-04 | 25 |
| B | 2020-01 | 50 |
| B | 2020-02 | 50 |
| B | 2020-03 | 50 |
| B | 2020-04 | 75 |
+------+------------+--------+
As you can see, the monthly budget for January is the sum of 2020-01 - 2020-04
The structure is provided in the calendar table. So I have the information on which week belongs to which month.
What I need to do is to create a dax measure that divides the weekly budget by the working days.
THe calendar has also the information of calendar days. For this purpose I have add this measure:
Working Days =
VAR vSelectedMonth =
SELECTEDVALUE ( 'Calendar'[Fiscal Month Number])
RETURN
CALCULATE (
COUNTROWS ( 'Calendar' ),
FILTER (
'Calendar',
'Calendar'[Fiscal Month Number] = vSelectedMonth
&& 'Calendar'[IsWorkingDay] = 1
)
)
Now, how can I divide the weekly budget by the working days for the corresponding week and than to accumulate this information for the whole month?
Based on the provided information the result should be something like that:
+------+------------+----------------+
| Area | Date | Running Budget |
+------+------------+----------------+
| A | 01.01.2020 | 10 |
| A | 02.01.2020 | 20 |
| A | 03.01.2020 | 30 |
| A | 04.01.2020 | 40 |
| A | 05.01.2020 | 50 |
| A | 06.01.2020 | 50 |
| A | 07.01.2020 | 50 |
| A | 08.01.2020 | 55 |
+------+------------+----------------+
Of course, if the corresponding date is not a working day, that value from the day before should be shown.
I am grateful for every help!
Please let me know if you need further information.
Best Regards

How to add a point to a cell when the difference of a date is < 45 days in a range of cells

I have an excel spreadsheet where there is a start date and an end date. When a person closes a ticket in less than 45 days (the difference between the start date and the end date is <45days) they are awarded 0.5 points. They also need to be sorted by month. So dates in 11 would be allocated to the points in the November column. Here is what I have so far but is not working.
=SUM(IF(AND(MONTH(I2:I1000)=11)DATEDIF(I2:I1000,J2:J1000,"D")<=45,0.5,0)
I have tried a normal IF function as well but it doesnt seem to add properly when the ranges of cells are added.
| Start Date | End Date | November Points | December Points | |
|------------|------------|-----------------|-----------------|---|
| 11/12/2019 | 11/28/2019 | 1.0 | | |
| 12/1/2019 | 1/05/2019 | | 0.5 | |
| 11/12/2019 | 11/30/2019 | | | |
So here you can see when the start date is 11/12/2019 and the end date is 11/28/2019 0.5 points is added to the November Points cell(the total in that cell is 1.0 since there are 2 date ranges within 45 days for November). When the start date is 12/1/2019 to end date 1/05/2019 there is now 0.5 points in the December cell.
This might work:
| - | B | C | D | E | F | G |
|---|------------|------------|------|-----------------|-----------------|----------------|
| 2 | Start Date | End Date | Days | November Points | December Points | January Points |
| 3 | 12/11/2019 | 28/11/2019 | 16 | | | |
| 4 | 01/12/2019 | 05/01/2020 | 35 | | | |
| 5 | 12/11/2019 | 30/11/2019 | 18 | | | |
| 6 | | | | 1 | 0.5 | 0 |
Formula and formatting used:
Cell E2: 01/11/19 (or 11/01/19 in USA) and give the cell a custom number format of mmmm "Points" - do this for F2 & G2.
Column D: =DATEDIF(B3,C3,"d")
Totals in row 6: =COUNTIFS($D$3:$D$5,"<=45",$B$3:$B$5,">=" & E$2,$B$3:$B$5,"<" & F$2)/2
So the COUNTIFS will count how many days are <=45, will check that the dates in column B are >= 1st of the month, will check that the dates in column B are < 1st of the following month.

How to determine the order in summarize and calculate values in power pivot

i am very new to power pivot and there is this one thing I haven't been able to understand fully. I have this table consisting of Week, value 1 and Value 2.
I want to first summarize all the values for week 1,2,3 and so forth and then divide the sum of value 1 with the sum of value 2. However, when i do a measure power pivot first divide value 1 with value 2 on each row and then summarize them.
This probably is a very basic question but if someone could shed some light on this for me I would be more than happy.
It is not clear what the resulting table you would to see is and this is important to understand in order to determine the correct DAX for a measure.
However given the following input data in table "tablename"
| Week | Value 1 | Value 2 |
| 2018 w1 | 200 | 4 |
| 2018 w2 | 300 | 5 |
| 2018 w3 | 250 | 3 |
| 2018 w4 | 100 | 4 |
The most obvious measure would be
Value1 by Value2 =
divide
( calculate(sum('tablename'[Value 1]))
, calculate(sum('tablename'[Value 2]))
)
This would mean that if you brought this into a table with Week in the context then you would get the following
| Week | Value 1 | Value 2 | Value1 by Value2 |
| 2018 w1 | 200 | 4 | 50 |
| 2018 w2 | 300 | 5 | 60 |
| 2018 w3 | 250 | 3 | 83.33 |
| 2018 w4 | 100 | 4 | 25 |
or if you used this for all weeks your table would be
| Value1 by Value2 |
| 53.125 |

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.

DAX Cumulative Total With Date Filters

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

Resources