Spotfire Running Balance (Cumulative Sum) - sum

I am trying to create a running balance column in Spotfire that's supposed to look like the picture attached below. In essence,I want to calculate the cumulative total of the "amount" column row by row, and that I want it to start from 0 as the date changes.
I have tried several OVER functions:
Sum([AMOUNT]) OVER AllPrevious([Date])
Sum([AMOUNT]) OVER Intersect([CURRENCY],AllPrevious([SETTLEDATE]))
Sum([AMOUNT]) OVER Intersect([Calculation Date],AllPrevious([SETTLEDATE]))
Any help is greatly appreciated.

You were very close with your first over statement. The problem is, when you use over (AllPrevious([Date])) and you don't have 1 row for each date, then you will skip rows. So, the last row of your data would only sum it over the rows where 6/1/2017 is in the Date column. Instead, we need to apply a RowID to your data set and then sum over it. This will ensure we sum over all previous rows.
Assuming your data set is in the order you want it to be in when you bring it into SpotFire, do the following:
Insert a calculated column RowID() and name it RowID
Use this calculation: Sum([amount]) over (Intersect([Date],AllPrevious([RowID])))
This will give you the running sum you are looking for.

#scsimon- I modified your custom expression slightly to include date as requested in the question.
Modified expression:
Sum([Amt]) over (intersect(Allprevious([rowID]),[Date]))
Final output table:
#LeoL - Hope this answers your question.

Related

Max Date Last Update Date SQL

I am asking for help with this item. I am a novice to SQL and not very sure how to handle this problem I appreciate any help from the forum.
I have a table that is updated multiple times a day. I would like to create a view that only displays the last update that was made for a given day.
Here is a sample of the data
enter image description here
This is the desired result of the SQL Query when the data set provided has been queries
enter image description here
As I understood you want to get the last record of every day.
Just group it by day
You will have to use EXTRACT to do it
Example: EXTRACT(DAY FROM DATE)
Then select the max time from the column where you have the time of the day. If you dont have the time in a different column you will also need to extract it.
SELECT MAX(TIME_COLUMN) FROM `TABLE_NAME`
GROUP BY EXTRACT(DAY FROM DATE)
SELECT MAX (your_date) AS "Max Date"
FROM your_table

how to find the last data in tableau?

i want to find last data in tableau .
i used this code, but it doesn’t satisfy the results i want.
[datatime]={FIXED:MAX([datatime])}
and results just this
this is my data table
and i want the result
You're missing some steps.
first of all you need to calculate the max date (Max Date) for each line and this something you can achieve twisting your Fixed formula, like this:
{ FIXED [line] : MAX([datetime])}
Then you need to create another calculated field (Check Max Date) to check this condition:
if [datetime] = [Max Date] then 'max' else 'other' end
Then you can use Check Max Date as a filter keeping just the 'max' values.
This is a quick example based on Sub-Categories from the Superstore sample dataset.

Adding a Simple Subtraction Formula to a Case When Statement

I'm writing a sql script where I'd like to add a subtraction formula to the script. My problem is that when I add this CASE STATEMENT my script will not run. I read that you need to add paranetheses around the formula, which I did, and when I do this without the CASE WHEN it will work great. Can you just not use formulas within a case statement?
In my statement below, I have a column, TotalWeightLoss, where its a cumulative total weight lost by a person. So what I am trying to do is see the monthly weight lost instead of a cumulative total.
SELECT *
,case when rownmbr=1 then TotalWeightLoss else (TotalWeightLoss - LAG(TotalWeightLoss) OVER (PARTITION BY AccountNumber ORDER BY ProcessDate, ProcessDate)) AS AmountLost
from cte;"))
Thanks!
Besides the missing END: Assuming that rownmbr is based on a ROW_NUMBER calculation this can be simplified to
TotalWeightLoss
- LAG(TotalWeightLoss,1,0) -- LAG supports a default for a missing value
OVER (PARTITION BY AccountNumber
ORDER BY ProcessDate, ProcessDate) AS AmountLost

How can I get the Total field to display only once instead of on change of every group?

I have grouped my formula by my date field per month but why does it show in all the months (via lookup table)?
Perhaps there's something missing or wrong with my formula? This is its code:
(Sum ({tblDates.TotalFuelSales}) + Sum({tblDates.TotalMotorOilSales})) -
(Sum ({tblDates.TotalOperationalExpenses}) + Sum({tblDates.TotalMotorOilExpenses}) + Sum({tblDates.TotalFuelExpenses}))
Thanks!
Issue here is Sum ({tblDates.TotalFuelSales} will give you the grand total so for every date group you the same value... if you wish to get according to date grouping then your syntax shoukd be something like below.
Sum ({tblDates.TotalFuelSales}, {your date grouping})
Above is just an example check syntax when you apply... similarly apply to all fields in formula
If you dont you want to see the Total for each month then you should place #Profit formula on the Report footer. It is currently on the Group Footer section so it will show on change of every group.
I would do it this way:
1) Right click every field on the Group Header and Insert Summary (Sum) on the group footer for each field. So you have five Summary fields on the Group Footer section.
2) Put my Profit formula in the Report footer section, use those summaries in the formula to get my totals.
This is assuming you want Profit to appear only once.
You can skip doing the Insert Summaries part, and directly include the Sum({Fields}, {tbl.Date_group}) in your Profit formula.

How to calculate the percentage using date fields?

I want to calculate the percentages using date fields in my SSRS report.
I have two date fields :
eg I have 3 columns in my matrix
monthly target,
monthly completed and
percentage
Due date field count 10
completed date count 5
percentage 50%
for example
=Sum(Fields!Completeddate.Value)/Sum(Fields!duedate.Value)*100
however, this will not work for date fields.
Can anyone help?
Thanks in advance
The most straight-forward way to do this would be to add this field to your stored procedure (since it represents a column in your matrix) instead of trying to calculate it in SSRS.
Your SQL statement may look like this:
SELECT #CompletedDate, #DueDate, CAST(DateDiff(day, GetDate(), #CompletedDate) AS FLOAT)/CAST(DateDiff(day, GetDate(), #DueDate) AS FLOAT) * 100 AS INT) AS [Percentage]
The exact implementation will depend on how exactly you're using this value, but the point is that you can use the DateDiff() function to determine how many days apart two dates are, and with that information, you can find a percent difference. Once you've calculated this, you can assign it to a matrix column like you would any other value.