Powerpivot DAX sum based on row value - excel

I am trying to create a running total based on 4 criteria but can't seem to get it to work - sorry very new to both powerpivot and DAX.
I have a Query that has a total of 17 columns - 13 actual data and 4 calculated.
I want to get a cumulative total based on 4 criteria.
I haven't even been able to get 1 criteria working so far:
CALCULATE(
SUM(Tonnes),
Filter(Query,[Deliveryid]=[Deliveryid])
)
Basically want to sum all the tonnes for each deliveryid - this is simple in a pivot, but i want to eventually do this for all dates before the date value in the row and for each commodity and delivery address etc.
Any help would be appreciated.

Regarding your specific requirement to sum all the tonnes for each Deliveryid, you're close. Try this: =CALCULATE(SUMX(Table1,[Tonnes]),FILTER(Table1,[Deliveryid]=EARLIER([Deliveryid]))). It will sum the Tonnes for each Deliveryid.
Regarding your follow-on desire to sum all the tonnes for each Deliveryid for the dates preceding the current date, try this: =CALCULATE(SUMX(Table1,[Tonnes]),FILTER(FILTER(Table1,[Deliveryid]=EARLIER([Deliveryid])),[Date]<EARLIER([Date]))).
The table below might help show what these do (I used very long column titles to hopefully make this more understandable):
TotalTonnesForDeliveryid (made with the first code item above) simply totals tonnes by Deliveryid. In the table below, you see the TotalTonnesForDeliveryid for Deliveryid 1 is 91 tonnes ... 38 (from 1/1/2017) + 23 (from 1/3/2017) + 30 (from 1/6/2017).
TotalTonnesForDeliveryBeforeCurrentDate (made with the second code item above) totals tonnes by Deliveryid only for the dates preceding the current date. The reason TotalTonnesForDeliveryBeforeCurrentDate shows blanks for first occurences of Deliveryid is because there was no prior delivery. If more than one delivery has been made for a particular Deliveryid, each following occurence of the Deliveryid shows an accumulation of the previous dates' tonnes. So for Deliveryid 1: the initial delivery date (1/1/2017) shows blank TotalTonnesForDeliveryidBeforeCurrentDate (because there was no previous delivery); the second delivery date (1/3/2017) shows 38 tonnes (which is what was delivered on 1/1/2017); and the third delivery date (1/6/2017) shows 61 tonnes (which is the 38 from the first delivery + the 23 from the second delivery).

Related

Facing difficulty in Excel when creating a logic for moving average of different variables

So, the point is, in my dataset I have to create a variable "Moving Avg. Amt paid per sq. ft." and the formula or the logic I need is to calculate the last five values as per most recent transactions. i.e. most recent sales by date. but this average should only return value in case it matches the same building and same area variable.
This is what my data looks like
Area ID has three categories. Building number has 5 categories. Date is sorted in ascending order. Now my variable moving average should calculate last 5 averages w.r.t date but for the same building in the same area. e.g. there are buildings 1 and 2 in area 102. I need my Mov Avg. variable to calculate using conditions when it matches criteria of building 1 in 102 for past five sales and when it finds building 2 in the building number variable, it should calculate average of last 5 sales of that building in area 102.
So my approach to this issue was (which is flawed at the moment):
I calculate average of amount paid per sq. foot w.r.t area & building based on dates using the formula
=AVERAGEIFS($N$2:$N$6547,$D$2:$D$6547,D14,$C$2:$C$6547,C14,$B$2:$B$6547,B14)
but I cannot make this formula work, to calculate moving average whenever it meets the criteria. I tried the offset the point as well by 5 but the logic is not right and hence its not working and returning #value in the cells. The formula I used to offset the above condition is
=AVERAGEIFS((OFFSET(N13,5,,5)),$D$2:$D$6547,D13,$C$2:$C$6547,C13,$B$2:$B$6547,B13)
(These formulae are used in column Q of my data)
Need a support from the community as I am badly stuck in making this data useful and I am out of any ideas to make this work.
Edit 1: I am not sure how I can attach my excel file here so you may review the dataset. I have uploaded it on a third party site, for which the link is shared below, so you can view the file in detail.
https://file.io/hlciAHJOHzWA
Expected result is as I have mentioned the instruction said
"Create a variable called "mov. avg amt. paid per sq ft". For each row, this variable should calculate average amt paid per sq ft for the most recent past five sales (by date) for the same building in the same area."
And my approach to build a logic or formula to make this variable calculate moving average w.r.t date for same building in the same area doesn't seem to work because there might be some flaws.
In Office 365 you could use:
=LET(f,FILTER($N$1:N13,($B$1:B13=B14)*($C$1:C13=C14),""),
c,COUNTA(f),
s,SEQUENCE(5,,c-5),
IFERROR(IF(c<5,SUM(f)/c,SUM(INDEX(f,s))/5),""))
If there's less than 5 matches prior to the current sales it'll calculate the average of the count. If 5 or more matches it'll calculate the average of the last 5 prior to the current sale.

excel formula for date loop and backwords logic

Hi I have a problem developing formula to calculate following logic in Excel. I need to identify patients visits within 30 days interval and then within the identified 30 days interval the most latest date for prescribed medication.
lets say patients first visit was on 18Dec2018, this should count as New visit. so all visits until 18Jan2019 are considired as within 30days visits. This calculation I have solved nicely with this formula.
=OM(OCH(ELLER(C3=C2;C3=C4);ANTAL.OM(C:C;C3)>1;ANTAL.OM(C$2:C3;C3)>1;DAGAR(A3;A2)<30;DAGAR(A3;A4)<30;DAGAR(A:A;A3)<30);"Within 30dgr";"New Visit")
And I get in Kolumn G.
No I however within period 18Dec to 18 Jan need to identify latest date for Recepee Registered Date which is available in Kolumn F and I got stack which formula even to start with trying.
For the first part of your problem, there is a small error in your formula - the last row should not be New Visit since the difference is less than 30.
To keep things understandable, I have created 1 extra column as described below:
Date Difference from latest new entry: this will be
a. 0 if - first entry of patient ;
b. difference between row's date - previous new entry date
c. reset when the difference exceeds 30 - for ex: in row 5 the diff > 30 thus in row 6 it gets reset and the difference is calculated between row6 and row5 instead of row6 and row1
Unique Visit: marks whether the visit is new or within 30 days
Formula for date difference(put in row 1):
=IF(D2<>D1,0,IF(I1="New",B2-B1,B2-B1+H1))
Formula for Unique Visit:
=IF(D2=D1,IF(H2<30,"Within 30","New"),"New")
Put these formulas in first row and drag down to auto-fill.
Latest Recipee Register Date: Captures Recepee register date if the entry is last in entry of 30 days.
Formula:
=IF(D2<>D3,G2,IF(I3="New",G2,""))
Note: I see that your data is sorted with respect to date and other dates are sorted in ascending order as well. These above formulas will work when the data is sorted in ascending order for the all the dates. For other sorting the formulas will need to change.

How to calculate no of days where sales were made in MS excel using sumifs and countifs?

I am working on an excel sheet where I am required to calculate average number of days the stores in a city were able to make some sales. I am attaching a sample of the table for reference. The values in the cells represent the number of units sold(not relevant to this question).
Here across NY, two stores are present, and out of the total number of days in consideration (3*2), only 4 days some sales were made, making the average 66%.
Similarly for Paris, there exists only one store which was open across all days.
To arrive at the figures, I tried using nested countifs and SUMIFS , but did not receive the expected results. Also, in some of the older posts, users had suggested to use INDEX MATCH with SUMIFS, but I was not to get accurate results using these.
Can anyone help me to get the correct figures for Total days, and Days with some sale.
SUMPRODUCT SOLUTION
=SUMPRODUCT(--(A$2:A$5=A8)*--(C$2:E$5<>""))
=SUMPRODUCT(--(A$2:A$5=A8)*--(C$2:E$5<>"NO SALE"))
=ROUND(C8/B8,4)
First, according to your grid NY made sales on 4 of the 6 days. (NY1: Mon, Wed; NY2: Tues, Wed). Thus the average is not 50% but 66%.
Second, to get your formula. Assuming "Place" is in column A. Below is for NY, you can solve for the rest.
Total number of days:
In cell "C9": =COUNTIF(A2:A4,"=NY") * 3
Days with sales:
In cell "D9": =COUNTIF(C2:E2,"<>NO SALE") + COUNTIF(C4:E4,"<>NO SALE")

Calculating days since an event in Excel

I am working with a weather data set. I am particularly interested in two columns which are cumulative precipitation and a date. My question is a simple one, though I am struggling to figure out the solution. Essentially I am wanting to determine days since precipitation. An example of the data is as follows:
WEATHER DATA
Pr Date
40 8/8/2013
40 8/8/2013
40 8/9/2013
40 8/9/2013
41 8/10/2013
41 8/10/2013
In this example, if I know the last day it rained was 8/7, then 8/8 would have a value of 1 (days since precipitation), 8/9 would be 2, and 8/10 would go back to 0. I have multiple dates because of hourly recordings (I trimmed it down for this post). I've been trying to figure it out with conditional if|then statements, but I'm thinking VBA may be more appropriate here. Any help or insight would be greatly appreciated.
Assuming cell C2 to be equal 1 (or start where you wish by adjusting the C2 value), the formula below works in the example you provided. Type in C3:
=IF(A3<>A2,0,IF(B3=B2,C2,1+C2))
Drag the formula down. Explanation:
If precipitation from time i+next is different from i it comes back to zero --> there was rain.
If time i+next is equal i, then it compares the date d+next with d.
If they are equal hold the number of days without rain from previous cell.
If they are not, add 1 day* to the value inside previous cell.
*I'm assuming you have consecutive days from the following sentence:
I have multiple dates because of hourly recordings

Excel- Average days between group of dates

I'm trying to use excel to calculate the average frequency of delivery for a set of parts. I have a data set that has two columns- part number and delivery date. I'm trying to figrue out out oftne parts get delivered, on average, in terms of days. I tried using nested ifs like averageif(a2=a2:b9999,datedif(xx)) etc, but to no avail. I'm looking for this:
Input:
Part A 8.1
Part A 8.8
Part A 8.15
Output: Part A Average Delivery - Every 7 Days
etc etc. Any ideas?
If your dates are in ColumnB:
=(MAX(B:B)-MIN(B:B)--1)/COUNT(B:B)
or:
=(MAX(B:B)+1-MIN(B:B))/COUNTA(B:B)
should serve.
Edit
If you have multiple parts (the above assumed only one) and the list is in no particular order then a PivotTable may be best (say with its top left-hand corner in D1), in Tabular form with Part for Row Labels and Delivery three times for Σ Values (the first as MAX, the second as MIN and the third as COUNT). Then =(1+E3-F3)/G3 copied down should give you the average bumber of days between deliveries. For example 5 in your example (3 deliveries in 15 days).

Resources