Growth formula with a constant base year - statistics

I have two variables year as well as price and I want to calculate the growth of former variable. However, the base year is constant in each calculation.
Consider the following table:
+------+-------+--------+
| year | price | growth |
+------+-------+--------+
| 2010 | 7 | -0.3 |
+------+-------+--------+
| 2011 | 9 | -0.1 |
+------+-------+--------+
| 2012 | 10 | 0 |
+------+-------+--------+
| 2013 | 12 | 0.2 |
+------+-------+--------+
| 2014 | 13 | 0.3 |
+------+-------+--------+
| 2015 | 17 | 0.7 |
+------+-------+--------+
The growth formula is:
(price of second year - price of first year) / price of first year
In my formula, the first year is always 2012:
growth = (price - price (for year=2012) ) / price (for year=2012)
How can I generate this formula in Stata?

The following works for me:
clear
input year price growth
2010 7 -0.3
2011 9 -0.1
2012 10 0
2013 12 0.2
2014 13 0.3
2015 17 0.7
end
generate wanted = (price - price[3]) / price[3]
or
generate obs = _n
summarize obs if year == 2012, meanonly
generate wanted = (price - price[`=obs[r(min)]']) / price[`=obs[r(min)]']
Results:
list, separator(0)
+--------------------------------+
| year price growth wanted |
|--------------------------------|
1. | 2010 7 -.3 -.3 |
2. | 2011 9 -.1 -.1 |
3. | 2012 10 0 0 |
4. | 2013 12 .2 .2 |
5. | 2014 13 .3 .3 |
6. | 2015 17 .7 .7 |
+--------------------------------+

Related

Create a new column that take previous year's rate if current month is January

I want to create a new column that takes previous year's rates if the current month is January else use current rates.
| Date | Sales | Rates | Month | Year |
|:-----------|---------:|:-----:|-------:|:-------:|
| 1/1/2017 | 10000 | 8.0 | 1 | 2017 |
| 1/1/2018 | 20000 | 8.2 | 1 | 2018 |
| 2/1/2018 | 15000 | 8.2 | 2 | 2018 |
| 1/1/2019 | 11000 | 8.5 | 1 | 2019 |
| 3/1/2019 | 18000 | 8.5 | 3 | 2019 |
| 1/3/2020 | 22000 | 9.0 | 1 | 2020 |
Here the new column should have previous years rates if the month is January.
I tried this but failed to get results.
if [Year] > 2017 and [Month] = 1 then [Rates] = [Rates] and [Year] = 2018 else [Rates]
Try this as a calculated column:
New Rates =
VAR prevYear = 'Table'[Year] - 1
VAR ratePreviousYear =
MAXX ( FILTER ( 'Table', 'Table'[Year] = prevYear ), 'Table'[Rates] )
RETURN
IF (
'Table'[Month] > 1
|| ISBLANK ( ratePreviousYear ),
'Table'[Rates],
ratePreviousYear
)

How to calculate running total + total remaining in Excel?

I have this set of data in Excel:
Date | Year | Spend | Budget | Product
Jan | 2017 | 300 | 350 | Pencils
Feb | 2017 | 450 | 450 | Pencils
March | 2017 | 510 | 520 | Pencils
... | ... | ... |
Dec | 2017 | 234 | 240 | Pencils
Jan | 2018 | 222 | 222 | Pencils
Feb | 2018 | 458 | 500 | Pencils
March | 2018 | 345 | 400 | Pencils
... | ... | ... |
Dec | 2018 | 600 | 600 | Pencils
I'm trying to build a pivot table that shows:
RT stands for "running total"
Av stands for "available"
Year | 2017
| Jan | RT | Av | Feb | RT | Av | March | RT | Av
Pencils| 300 | 300| 50 | 450 | 750| 50| 510 |1260| 60
In brief, "available" = running total + budget for remaining months. Any ideas?
Thanks!
If I understand correctly:
available = sum(budget) - sum(spent)
running total = sum(spent)
You should add a column for running total, that will sum the spent column from the beginning up to current row.
And add a column for available, that will sum the budget column from the beginning up to current row, and will reduce the value of the "running total" column (of the same row) from it.

In Tableau, how to calculate weighted average on a daily basis?

My data looks like below:
unit price quantity salesperson date
$10 5 A 1/1
$10 6 B 1/1
$30 9 A 1/1
$30 10 B 1/1
$10 3 A 1/2
$10 5 B 1/2
$20 7 A 1/2
$20 8 B 1/2
In Tableau, how to calculate the average daily unit sales price each salesperson made? Suppose unit price and quantity are measures, salesperson and date are dimensions.
Thanks a lot!
I'll go ahead and give you the LOD expression, so that you'll have something that will work regardless of your table layout.
{ FIXED [salesperson], [date] : SUM([unit price] * [quantity]) / SUM([quantity]) }
That will give you a table that looks like this:
+------------+----------+-------------+------+-----------------+
| unit price | quantity | salesperson | date | Avg Daily Sales |
+------------+----------+-------------+------+-----------------+
| 10 | 5 | A | 1/1 | 22.86 |
| 10 | 6 | B | 1/1 | 22.5 |
| 30 | 9 | A | 1/1 | 22.86 |
| 30 | 10 | B | 1/1 | 22.5 |
| 10 | 3 | A | 1/2 | 17 |
| 10 | 5 | B | 1/2 | 16.15 |
| 20 | 7 | A | 1/2 | 17 |
| 20 | 8 | B | 1/2 | 16.15 |
+------------+----------+-------------+------+-----------------+
That follows the formula you gave in a comment on Nicarus's answer.
Here's a quick calculation, just to confirm that it works.
On 1/2, Salesman A sold:
( (10 * 3) + (20 * 7) ) / (3 + 7)
= (30 + 140) / 10
= 170 / 10
= 17
You want to determine the total amount (in dollars) sold on average, per day.
You can create a calculated field, like so:
SUM([Quantity] * [Unit Price]) / COUNTD([Date])
Then display the data by salesperson:
I think I figured it out:
sum(unit price*quantity)/sum(quantity)

Excel Formula - Get Value From Another Excel Sheet Based on Column in Current Sheet

I have two sheets in excel, Summary and Data. The data sheet is connecting through SQL Server and Summary sheet is summarising the data in a table.
Summary sheet
+------+--------------+
| ID | Month - Year |
+------+--------------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
+------+--------------+
Note: I am using a formula to get the distinct IDs from the data sheet. ID column is column A, Month - Year column is column B. ID 1 is in A2 square.
Data sheet
+------+--------------+
| ID | Month - Year |
+------+--------------+
| 1 | Oct 2014 |
| 2 | Dec 2014 |
| 3 | Oct 2016 |
| 4 | Jan 2016 |
| 5 | Nov 2015 |
| 6 | Jul 2015 |
| 7 | Jan 2016 |
| 8 | Nov 2015 |
| 7 | Jan 2016 |
| 8 | Nov 2015 |
+------+--------------+
Note: ID 1 is in A2 square, Month - Year is in B2 square.
How do I write a formula for the Summary Month - Year column to get each month - year (from data sheet) based on the ID column (in the summary sheet)?
Expected Result:
+------+--------------+
| ID | Month - Year |
+------+--------------+
| 1 | Oct 2014 |
| 2 | Dec 2014 |
| 3 | Oct 2016 |
| 4 | Jan 2016 |
| 5 | Nov 2015 |
| 6 | Jul 2015 |
| 7 | Jan 2016 |
| 8 | Nov 2015 |
+------+--------------+
Equivalent Expected Result in Pivot Table (with rows Month-Year and ID):
-Nov 2015
--5
--8
-Jan 2016
--4
--7
-Jul 2015
--6
-Oct 2014
--1
-Dec 2014
--2
-Oct 2016
--3
Effort:
=VLOOKUP(A2,Data!$A$2:$A$500,2,FALSE)
=VLOOKUP(A2,Data!$A$2:$B$500,2,FALSE)
The formula you tried needs to include both the columns in the table array variable.
Additionally the array must contain the lookup value in the first column of the table array.
I prefer however to use the INDEX MATCH combination, it removes the constraint of the search column being the most right column of the table array. Using the values you gave for your ranges it would be:
=INDEX(Data!$B$2:$B$500,MATCH(A2,Data!$A$2:$A$500,0))
For more information about VLOOKUP you can get info at:
https://support.office.com/en-us/article/VLOOKUP-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1

DAX TOTALYTD on two different cadandars

I am looking to create a YTD total, however, the year end date needs to change depending on the value in another column. In other words, the fiscal year for group 1 would be from 11-1 (Nov-1) to 10-31 (Oct-31), while the fiscal year for group 2 would be from 7-1 (Jul-1) to 6-30 (Jul-30). What I need to do is when calculating the fiscal year, I need the calculation for that year to be different depending on what group the line item is in. So 2015 for group 1 would be 2014-11-01 to 2015-10-31 while 2015 for group 2 would be 2014-07-01 to 2014-06-30. Please see an example table here (Please note that I do have a date table related to this one in order to create date functions):
**Table 1**
-------------------------
Group | Date | Qty
1 | 2014-10-01 | 1
1 | 2014-11-01 | 1
1 | 2015-01-01 | 2
1 | 2015-05-01 | 1
1 | 2015-10-31 | 2
1 | 2015-11-01 | 1
2 | 2014-06-01 | 1
2 | 2014-07-01 | 1
2 | 2014-12-25 | 2
2 | 2015-01-01 | 1
2 | 2015-06-30 | 2
2 | 2015-07-01 | 1
With this information in mind, I need to create a YTDTOTAL function that will dynamically change the , parameter depending on what group the line item is in. I thought of using an if statement, but realized that it wouldn't work on a measure. Something like this:
Total $ Sold YTD = TOTALYTD([TOTAL $ Sold],directSQLDateTable[date],ALL(directSQLDateTable[date]), IIF([GROUP = "A","10/31","6/30"))
In the end, I would like to create an output similar to this (The "Group A YTD" and "Group B YTD" columns really are not needed, just wanted to add to demonstrate my example):
Year-Month | Total_Qty | Group A YTD | Group B YTD
--------------------------------------------------
2014-07 | 1 | 0 | 1
2014-08 | 1 | 0 | 1
2014-09 | 1 | 0 | 1
2014-10 | 2 | 1 | 1
2014-11 | 2 | 1 | 1
2014-12 | 4 | 1 | 3
2015-01 | 7 | 3 | 4
2015-02 | 7 | 3 | 4
2015-03 | 7 | 3 | 4
2015-04 | 7 | 3 | 4
2015-05 | 8 | 4 | 4
2015-06 | 10 | 4 | 6
2015-07 | 5 | 4 | 1
2015-08 | 5 | 4 | 1
2015-09 | 5 | 4 | 1
2015-10 | 7 | 6 | 1
2015-11 | 2 | 1 | 1
2015-12 | 2 | 1 | 1
Please let me know if you have any questions. My apologies ahead of time if I didn't do that great of job explaining this or if I have left out a piece of info.
Thanks for any advice/help in advance! You guys on here are the best!
TOTALYTD() includes everything you need for this.
TotalQty:= SUM(Table1[Qty])
QtyYTDGroup1:=
TOTALYTD(
[TotalQty]
,DimDate[Date]
,Table1[Group] = 1
,"10/31"
)
QtyYTDGroup2:=
TOTALYTD(
[TotalQty]
,DimDate[Date]
,Table1[Group] = 2
,"6/30"
)
TotalQtyYTD:= [QtyYTDGroup1] + [QtyYTDGroup2]
I can provide a detailed explanation if you want, but I think the function definition pretty much covers it.

Resources