Possible to use a running total row in powerpivot for calculation in a calculated field? - excel

If I have a pivot table and I set the row to be a running total according to date and right now i will like to use this row to create a calculated field. Is it possible?
If not then is there a formula for cumulative calculations for calculated field?
Will supply more examples if need more clarification.
I want to do something like this
week 1 2 3 4 5 6 7 8 9 10
count 20 20 21 25 26 27 28 29 21 21
cumulative count 20 40 61 86 112 139 167 196 217 238
If the week is the base field then can I create a calculated field that does something like the cumulative count? I am doing this as i need to use the cumulative count for further calculations and if i use the show values as running total it seems to me that I cant use that variable for further calculations.
Hope this helps to clarify.

There are time intelligence functions built into DAX. You could use TOTALYTD(), TOTALQTD(), and TOTALMTD() if you have a proper date dimension with contiguous, non-repeating dates ranging from January 1 in the first year you have data through December 31 in the last year you have data.
If you have a non-standard fiscal calendar you can get the same effect so long as you have index fields for each time granularity of interest which are increasing over time.
CustomTotalYTD:=
CALCULATE(
[<some measure>]
,FILTER(
ALL( 'DimDate' )
,'DimDate'[FiscalYear] = MAX( 'DimDate'[FiscalYear] )
&& 'DimDate'[Date] <= MAX( 'DimDate'[Date] )
)
)

Related

Excel Average Calculation of a Moving Criteria Range

I need to create a logic excel formula that calculates the average of 3 months prior to a specified start date. There are multiple start dates. For instance:
For ID 1, The Start Date is May-2021. I would need for the logic to calculate the average between Feb-2021 to Apr-2021 to get 91.67. For ID 2, the Start Date is Jun-2021, so I would need to calculate the average of Mar-2021 to May 2021 to get 108.33. I also would need to calculate the average of 6 months prior in a separate column.
ID
Start Date
Calculation Result
Jan-2021
Feb-2021
Mar-2021
Apr-2021
May-2021
Jun-2021
1
May-2021
91.67
50
100
75
100
25
0
2
Jun-2021
108.33
0
25
100
175
50
125
3
Apr-2021
83.33
100
150
0
75
0
200
Any help is greatly appreciated! (Not opposed to VBA suggestions either)
use INDEX to create the range.
=AVERAGE(INDEX(D2:I2,MATCH(B2,$D$1:$I$1,0)-3):INDEX(D2:I2,MATCH(B2,$D$1:$I$1,0)-1))
Or if they are true dates we can use AverageIfs:
=AVERAGEIFS(D2:I2,$D$1:$I$1,">"&EOMONTH(B2,-4),$D$1:$I$1,"<="&EOMONTH(B2,-1))

How to calculate no of days spent per month from the data in MS Excel?

I have a data which has a hotel check in date and check out date. I would be calculating the number of days spent at the hotel, I would simply subtract both of them and I have the days difference.
I now have to pick up the customers who stayed for more than 31 days and find out how many days they had spent per month. For example, some one checking in on 18th July 2015 and checked out on 18th September 2015, there should be multiple columns, corresponding to that cust which reads 13 31 18, for days spent per month. (July, Aug, Sept)
Example data and sample output :
Client Check In Check Out
Majestica Inn 22-May-15 22-Jun-15 31 9 22
Twin Tree 29-May-15 30-Jun-15 32 2 31
Connaught Mews 1-Jun-15 31-Jul-15 60 29 31
Majestica Inn 18-Jul-15 18-Sep-15 62 13 31 18
Majestica Inn 18-Jul-15 18-Sep-15 62 13 31 18
I am new to Excel, please help.
if you can be flexible about the number of columns then you can use the below formulas to get the desired output.
Formula To Get The Days:
Remark: paste this formula in cell e2 and then drag & copy.
=IFERROR(IF(AND(MONTH(E$2&"-"&YEAR($B3))>=MONTH($B3),MONTH(E$2&"-"&YEAR($C3))<=MONTH($C3)),IF(MONTH(E$2&"-"&YEAR($B3))=MONTH($B3),DATEDIF($B3,EOMONTH($B3,0),"D"),IF(MONTH(E$2&"-"&YEAR($B3))=MONTH($C3),DATEDIF(DATE(YEAR($C3),MONTH($C3),1),$C3,"D")+1,DATEDIF(DATE(YEAR($C3),MONTH(E$2&"-"&YEAR($B3)),1),EOMONTH(DATE(YEAR($C3),MONTH(E$2&"-"&YEAR($B3)),1),0),"D")+1)),""),"Err")
Formula For Concatenating:
=IFERROR(DATEDIF($B2,$C2,"D")&IF(E2<>"",",","")&CONCATENATE(E2,IF(F2<>"",",",""),F2,IF(G2<>"",",",""),G2,IF(H2<>"",",",""),H2,IF(I2<>"",",",""),I2,IF(J2<>"",",",""),J2,IF(K2<>"",",",""),K2,IF(L2<>"",",",""),L2,IF(M2<>"",",",""),M2,IF(N2<>"",",",""),N2,IF(O2<>"",",",""),O2,IF(P2<>"",",",""),P2),"")
And If You Don't Want To Add any Columns Then Try This One......
Paste in Cell D2......
=IFERROR(SUBSTITUTE(DATEDIF($B2,$C2,"D")&","&IFERROR(IF(AND(MONTH("Jan"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Jan"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Jan"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Jan"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Jan"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Jan"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Feb"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Feb"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Feb"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Feb"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Feb"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Feb"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Mar"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Mar"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Mar"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Mar"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Mar"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Mar"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Apr"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Apr"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Apr"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Apr"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Apr"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Apr"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("May"&"-"&YEAR($B2))>=MONTH($B2),MONTH("May"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("May"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("May"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("May"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("May"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Jun"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Jun"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Jun"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Jun"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Jun"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Jun"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Jul"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Jul"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Jul"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Jul"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Jul"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Jul"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Aug"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Aug"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Aug"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Aug"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Aug"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Aug"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Sep"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Sep"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Sep"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Sep"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Sep"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Sep"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Oct"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Oct"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Oct"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Oct"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Oct"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Oct"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Nov"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Nov"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Nov"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Nov"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Nov"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Nov"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&IFERROR(IF(AND(MONTH("Dec"&"-"&YEAR($B2))>=MONTH($B2),MONTH("Dec"&"-"&YEAR($C2))<=MONTH($C2)),IF(MONTH("Dec"&"-"&YEAR($B2))=MONTH($B2),DATEDIF($B2,EOMONTH($B2,0),"D"),IF(MONTH("Dec"&"-"&YEAR($B2))=MONTH($C2),DATEDIF(DATE(YEAR($C2),MONTH($C2),1),$C2,"D")+1,DATEDIF(DATE(YEAR($C2),MONTH("Dec"&"-"&YEAR($B2)),1),EOMONTH(DATE(YEAR($C2),MONTH("Dec"&"-"&YEAR($B2)),1),0),"D")+1))&",",""),"Err")&"0|",",0|",""),"")
Good Luck .............

Averaging aggregated(SUM) values in Spotfire

I'm trying to Average aggregated(SUM) values, but my expression keeps doing weighted averages over the whole data set.
Table Structure
REGION SITE_ID MONTH QUANTITY
A 1 01 5
A 1 02 6
A 2 01 4
B 3 01 10
B 3 02 12
Expression
Avg(
Sum([quantity]) over (All([region]))/
UniqueCount([site_id]) over (All([region]))/
UniqueCount([month]) over (All([region]))
) over (All([region]))
To Clarify, I want to average A and B's Monthly Qty per Site
But I keep getting total qty divided by total no of site_ids divided by months
This really depends on where you are going to use it and what the REAL data looks like. This should get you started. Insert this calculated column.
SUM([QUANTITY]) OVER (Intersect([REGION],[MONTH])) / UniqueCount([REGION]) AS [AvgOverRegionByMonth]
This could be inaccurate depending on how the rest of your data looks like. You can also accomplish this in a cross table. The expressions for the Sum and Avg on the example below are as follows:
Sum([QUANTITY]) as [Sum], Sum([QUANTITY]) / Count([REGION]) as [Average]
EDIT
In order to ONLY get the average over the months, use this forumla:
AVG([QUANTITY]) OVER ([MONTH]) as [AvgOverMonth]
Here is what your data will look like:

Percentage Greater than/Less than in a table

I have a table that I want to find the percentage greater than and percentage less than compared to a baseline, for the total group based on the weights of each group.
Here is my example table:
Benchmark GRP 1 GRP 2 GRP 3 GRP 4
10 10 11 10 12
14 12 15 11 15
17 11 17 13 16
18 14 15 14 17
Poulation 40 45 30 80
What I want to do is find out for each level of the benchmark what % of the total population of all four groups is above or below the bench mark value.
I have tried various sumproducts and sumifs but can't seem to get it work.
Let me know your thoughts!
Thanks as always!
Assuming your sample data is in A1:E7 put the following formula into B9 and use Ctrl+Shift+Enter to record it as an array formula:
=SUM(IF(B$2:B$5>$A$2:$A$5,1,0))/COUNTA($A$2:$A$5)
This can then be copied across under the other groups. Below is showing how it works for me.
Note: The array formula will display with braces ({...}) around it but you do not type these.

PowerPivot: How to identify Max Value per Group in a Calculated Column

I am building a data model within Power Pivot for Excel 2013 and need to be able to identify the max value within a column for a particular group. Unfortunately what I thought would work and what I have searched for previously gave me an error or wasn't applicable (there was a similar question that dealt with calculated measures rather than columns and wasn't replicable in Power Pivot data view to the best of my knowledge)
I have included an indication of what I am trying to achieve below, in this case I am trying to calculate the Max % uptake column.
Group | % uptake | Max % uptake
A 40 45
A 22 45
A 45 45
B 12 33
B 18 33
B 33 33
C 3 16
C 16 16
C 9 16
Many thanks
Use
=CALCULATE(MAX([UPTAKE]),FILTER(Table1,[GROUP]=EARLIER([GROUP])))
use this formula in cell ("C2"):
=MAX(INDIRECT(CONCATENATE("B",MATCH(A2,$A$1:$A$10,0),":B",SUMPRODUCT(MAX(($A$1:$A$10=A2)*(ROW($A$1:$A$10)))))))

Resources