Power BI - Calculating a price/cost until a variable end date - date

Let's say I have a table of projects/programs/subscriptions/etc, it doesn't really matter as long as there is a price or cost per some amount of time. My table includes at least the following columns:
[ProjectName]
[StartDate]
[EndDate]
[CostPerDay]
I'm trying to allow the user to choose another date (slicer I assume?) and display the cost of each and all projects up to that date. Is this possible?
Edit: After the first responses I realize the original question was very poorly worded. Sorry about that. I've reworded it and I'll explain more here:
I am not trying to filter the programs by end date. I'm trying to sum a cost up until the end date OR slicer date, whichever is earlier.
Here's a short example table:
So we can also think of it as a Gantt chart like this:
Now imagine sliding a vertical line along that chart. I want to see the total cost up to that date.
I'm sure it will have to do with counting days between start date and the slicer date, then multiplying by cost. But how do we not include days after the end date of each project? Or it may be easier to do a range slicer with a min and max date, but again not counting days before or after each project.
To word it differently: can I input a date range, count the days that each project has in common with that range, and (the simple part) multiply days by cost?

It looks like what you need is a table visualization with a slicer for End date. Once the table and slicer are created, you can click on the small down facing arrow in the slicer and choose "Before". If you want both start date and end date in the equation, then you would have to add one more slicer for "Start date". If this is not what you are looking for kindly provide additional details so that we can help you out.

Related

Formula to add days to a Gregorian date

I was looking at Tomohiko Sakamoto's weekday calculator. It's a formula to calculate the day-of-week directly given year, month, day. That made me wonder what other neat date calculation shortcuts exist.
In particular, given an input date as (in_year, in_month, in_day) and a number of days N to add, what's a formula for returning the output (out_year, out_month, out_day)? Is there a well-known trick like the algorithm above?
One way would be to convert the input to a Julian day (a count of days since 4713 BC), add N to it, and then convert back. There are formulas for conversion in both directions. But the combined formula would be quite unwieldy. Is there a simplified version?
Perhaps there is even a formula to move forward or back by a certain number of weekdays.
This question isn't "how do I do date arithmetic in my favourite programming language?" I know how to call the date library to perform these operations. It's more curiosity and the hope of starting a collection of cool date algorithms.
Some of the answers in Algorithm to add or subtract days from a date? will be relevant to this. In particular http://howardhinnant.github.io/date_algorithms.html gives code to convert y,m,d to a count of days and back again. Those two routines run back to back would be pretty fast.

Aggregating data from the US stock market in Tableau, using different time frames

I am a very basic user of tableau and I have not found an answer to my question.
I have a txt file that has historical daily data for 98% of all the stocks in the US, with their daily capitalization. Each stocks has its TICKER, Daily Market Value for every trading day of the year, and its SECTOR.
I did a simple time series that display SUM([Mktval]) (sum of all individual market values) across all stocks, on a daily daily, and where I can see that the total value as of 2016 is about 24 Trillion USD, as in the image below.
When I change the view column from DAY to YEAR, I don't see the right values, but something a lot larger. So I realized that I need to do SUM([Mktval])/252 to get the right value for a year (there are 252 trading days in a year).
If I change the view to MONTH, as in the chart below, the numbers are again wrong because 252 is not the right value to use in the division.
Is there any way that Tableau can adjust the values automatically to reflect the AVG MktVal across different time intervals?
Thanks
Replace SUM(Mktval) on the Rows shelf with the following calculated field
avg({ fixed day(Date1) : sum(Mktval) })
That solution is all in one step. It is perhaps a bit more clear to use 2 steps. First, create a calculated field called total_daily_market_value defined as
{ fixed day(Date1) : sum(Mktval) }
Then make sure that calculated field is a measure. It is an LOD calculation that you can think of as a separate table with one value for each day showing the total market value for that day.
Drag that measure to a shelf, and then change the aggregation function to AVG(), MEDIAN(), MIN(), MAX() or STDEV() as desired. Tableau will aggregate the total_daily_market_value using your chosen aggregation function for whatever values of Date1 are in your view.

Tableau : How to get the Dashboard always display the latest 10 days worth of group statistics

My input text source always contains last 12 months worth of data. e.g: Current month is October. So My input source contains data starting from last Oct 1st to till date. But I want the aggregate statistics to be displayed on a daily basis for last 10 days of sales , 30 days of sales, 45 days of sale per product across various regions
I am trying to use window_avg fuction with something like window_avg(sum(sales), first() + datediff('day', window_min(min([date]))-1, dateadd('month',1,window_min(min([Date]))-1)) * 13,13) something like that. But I am not able to crack the exact logic.
Could you please suggest me some better way to achieve this, rather than using these kind of calculations. Also I am afraid if this goes wrong if there is data missing in the middle one or two days.
Any help is appreciated.
A very simple thing is to use a relative date filter. There's a UI for you to select they last N days.
Put the date on the columns shelf and set it to the date truncation of year-month-days. Put your measure row shelf. Put the date pill on the filter shelf too and use a relative date filter.
If you are doing simple aggregate like the sum of sales for a day it's easy and you'll not need to do anything else. You can can also fairly easily create a table calculation by right clicking on the measure and choosing one of the quick table calculations. Even when I'm doing a more sophisticated calculation, I start with a quick table calculation and then start editing.
If you are doing something like a moving average, the filter and the moving average can interact. For example, if I'm showing a 5 day trailing moving average over 30 day period, the first few days do not get averaged in the same way -- you don't have days over 30 days ago. If that's not really an issue for you, that's cool and you are done.
If it is an issue, it's going to be trickier. I'd suggest creating a second filter based on a table calc. The reason is the order of operations in Tableau. The raw data is filtered then aggregated by the database, then the table calcs are performed. If there are any filters on table calculations, then they are filtered after that. So basically, in my example, you want create a filter for 35 days on the date, then create a table calc on the date -- like using the INDEX() function. Filter the index function to show 30 days worth, then you've got a moving average that uses 35 days to compute the average, but only shows 30.

Tableau Draw Financial Data Series

I am new to tableau. I am just trying the simplest task: I want to draw a stock price time trend. I have data and I have price. But no matter how I tried, tableau seems no option for this raw data drawing-- you have to select a measure, whether sum or average or count but no option for just raw data. Can someone tell me how to do this? Thx!
Sum should be fine and as you add dimensions the granularity will be determined. For example, Take one of your stocks and add the date to columns and the symbol to rows. Most likely you want to see daily close price so right click and change to exact date. At this point the sum shouldn't matter because you should only have one price for each day in your record. If you have hourly price data, I would suggest using close price or change the date to the hour level.
In the Analysis menu uncheck aggregate measures, then no aggregations are performed on the measures and you see all the values.

Calculate interest on postgresql with trigger/function

I'm currently working on a simple banking application.
I have built a postgresql database, with the right tables and functions.
My problem is, that I am not sure how to calculate the interest rate on the accounts. I have a function that will tell me the balance, on a time.
If we say that we have a 1 month period, where I want to calculate the interest on the account. The balance looks like this:
February Balance
1. $1000
3. $300
10. $700
27. $500
Balance on end of month: $500
My initial thoughts are to make a for loop, looping from the 1st in the month, to the last day in month, and adding the interest earned for that particular day in a row.
The function I want to use at end of month should be something like addInterest(startDate,endDate,accountNumber), which should insert one row into the table, adding the earned rate.
Could someone bring me on the right track, or show me some good learning resources on PL/PGSQL?
Edit
I have been reading a bit on cursors. Should I use a cursor to walk through the table?
I find it a bit confusing to use cursors, anyone here with some well explained examples?
There are various ways of interest calculation in banking system.
Interest = Balance x Rate x Days / Year
Types of Balances
Periodical Aggregate Balance
Daily Aggregate Balance
Types of Rates
Fixed Rate Dynamic Rate (according to balance)
Dynamic Rate (according to term)
Dynamic Rate (according to schedule)
Types of Days/Schedules
End of Day Processing (One day)
End of Month Processing (One month)
End of Quarter Processing (Three months)
End of Half Processing (Six months)
End of Year Processing (One year)
Year Formula
A year could consist of 365 or 366 days.
Your user might want to override number of days in a year, maintain a separate year variable property in your application.
Conclusion
Interest should be calculated as a routine task. Best approach would be that would run on a schedule depending upon the frequency setup of individual accounts.
The manual has a section about loops and looping through query results. There are also examples of trigger functions written in pl/pgsql. The manual is very complete, it's the best source I know of.