Spotfire : formula for calculated column based on the other columns - spotfire

I have an requirement to create an calculated column which calculates the difference of a value from previous row based on the value of other two columns.
FOR Example. Consider 4 columns as shown below. Col B and Col C has date values , Col A as some unique value and Col D will has some numerical values which will be used to calculate the required column, such that when Col B=Col C and corresponding value of Col D has to be taken and subtracted to Col D values until next time Col B=Col C occurs.
IMAGE of Table (PS: I couldnt copy whole table in copiable way as format was going to messy)
I need Output like below : ie when the date values are equal ,[COL B]=[COL C] (ie 20-12-2016 15:40), then I have to corresponding value of COL D will be considered and subtracted with 138 ,until next similar occurrence happens.Next occurrence is for value 12-01-2017 15:40 , then it should be subtracted with 161 and this continues and its dynamic.
PLEASE NOTE : For the values before [COL B]=[COL C], those corresponding values should be same as COL D or if the Value of COL C is NULL or BLank then also its should be as COL D as highlighted in Green.
OUTPUT REQUIREMENT (As in Yellow)
I had used a code something as below in spotfire
if([COL B] is Not NULL,If([COL B]=[COL C],First[COL D] - [COL D]),[COL D])
, however since the value to be subtracted will be dynamic I am not able to achieve it.
Any help is appreciated
#Ksp585 : Please find below screenshot where it is calculating wrongly: After the green link next values should have been 751 (2638-3389) instead it has calculated as 3367 based on previous calculation (3389-22=3367) which is wrong:

#Nischey Anand- Please try the below solution.
Step 1: Add 'row_id' column
RowId()
Step 2: Insert calculation column with the below custom expression
If([Column C] is not null,Sum([Column D]) over ([row_id]) - first([Column D]) over ([Column C]),[Column D])
Here is the final output:
Hope this helps!
Answer updated based on additional details provided:
Add a calculated column with the below custom expression.
If(Sum(If([Column B]=[Column C],[Column D])) over ([Column C]) is not null,[Column D] - Sum(If([Column B]=[Column C],[Column D])) over ([Column C]),[Column D])
Here is the updated final output table:
Hope this solves your question!

Related

Power Query add Month Column in dependent on Index

I'm new in "Excel PowerQuery" and now if got a little problem with it. I've got a table with only an index (0,1,2,3...) and some values. Now I need a "NewDateColumn" with monthly interval for all index values.
Example:
Index Values NewDateColumn
0 a 10/2019
1 b 11/2019
2 c 12/2019
3 d 01/2020
... ...
n z 07/2020
Thanks for your help!
Create you new custom column like below(with your preferred date)
Create a duplicate column and transform it like below and then you can remove the original Date column

What is the simplest way to use Python to group based on a combination of columns (4 columns) and sum the amount (1 column) column?

Using Python 3.6 and Pandas 0.23.0 to automate accounting.
I want to group 4 columns based on certain combined values (63 different combinations) and then sum the 5th column. Then take the output of those 63 different values to a 2 column output: Combination, Amount.
The 63 combinations will always be the same.
For example:
There are columns A, B, C, D, E.
Column A can have 3 values:
Ebay
Amazon
Shopify
Column B can have 5 values:
Sale
Refund
etc.
Column C can have 8 values:
StorePrice
StoreFee
Tax
TaxRefund
etc.
Column D can have 30 values:
SoldAmount
TaxAmount
PromotionAmount
RefundAmount
OtherAmount
etc.
Column E can have a numerical value:
-1,000,000 - 1,000,000
NOTE: The amount of unique combined values is 63 for our purpose. Refunds can’t be Promotions, etc.
I need to find the sum of Column E for each combination.
For perspective, this is typically done with a Pivot Table in excel, except I have to do it manually, so that is 63 different sorts. So I will group by Ebay, Sale, StorePrice, SoldAmount to get the summed amount of all Sold Ebay sales over a period.
I thought about storing a list of the 63 combinations in my code and then looping through the .txt file. Sum For w, x, y, z: sort of thing. Here is where I started and then got stuck:
import pandas as pd
data = pd.read_csv('/Users/XXX/Desktop/statement.txt', sep='\t', header=0)
df = pd.DataFrame(data)
test3 = df.groupby(['Column A','Column B', 'Column A', 'Column D']).sum()
This gets me close, but I'm stuck.
What is the simplest way to solve this problem? Any help is appreciated!
Your arg list should instead be this:
df.groupby(['Column A','Column B', 'Column C', 'Column D']).sum()
If you told us the actual and expected result,
we'd be in a better position to help you.
https://stackoverflow.com/help/mcve

How to get a column name based on function

Suppose I have a dataset:
A B C Final
1 2 3 C
4 5 6 C
I want final to return the column name of the max value. In the example above, 3 and 6 are the highest values, so columns C will be returned in the 'Final' column.Is there an efficient way/formula you can use to get the column names without using VBA? the real dataset containts 60 columns.
I would try something like this to get the column letter.
=SUBSTITUTE(ADDRESS(1,MATCH(MAX(A3:C3),A3:C3,0),4),1,"")
If you want the header, I think there's an easier way, but the extension from this would be
=INDIRECT(SUBSTITUTE(ADDRESS(1,MATCH(MAX(A3:C3),A3:C3,0),4),1,"")&1)
For the header, the better way
=INDEX(A1:C1,1,MATCH(MAX(A3:C3),A3:C3,0))
HLOOKUP is a way without VBA, but sadly HLOOKUP does only search in the first row of the array, so you should add the "title column" at the bottom
A B C Final
1 2 3 <formula1>
4 5 6 <formula2>
A B C
If the dataset starts at first corner, Formula1 would be :
=HLOOKUP(MAX(A2:C2);A2:C4;2;FALSE)
Formula2 will be
=HLOOKUP(MAX(A3:C3);A3:C4;1;FALSE)
etc...
=HLOOKUP(MAX($A3:$C3);$A3:$C$4;<manual change here backward>;FALSE)
You can use the vector form of the LOOKUP function.
If your first row, with A B C, are the labels, then:
=LOOKUP(2, 1/(MAX(A2:C2)=A2:C2),$A$1:$C$1)

Excel - Include Row in Sum Based on Comparison to Following Row

Paging All Excel Wizards,
I am trying to see if there is a way to have a one-line SUMIF or something similar to sum up the following criteria in an Excel spreadsheet:
Sum the values of Column C if
If Column A = "Chizzle"
AND If Column B is >= Column B of the next row
Sample Data:
A B C
Type Level Value
__________________
Chizzle 1 23
Chizzle 2 10
Bobbles 3 1.5
Bobbles 3 2.6
Chizzle 2 5.5 <- Should Be counted
Cobbles 2 1
Chizzle 1 3.3 <- Should Be counted
I have tried using something like this:
=SUMIFS(C1:C1000,A1:A1000,"Chizzle", B1:B1000, ">=" & B2:B1001 )
Unfortunately the B2:B1001 part isn't working and it is selecting all values.
If there is a way to do this with a one line calculation, without having to add an additional column? That would be awesome but I'm not sure if it is possible.
Thanks!
Try this SUMPRODUCT():
=SUMPRODUCT(($A$2:$A$8="Chizzle")*($B$2:$B$8>=$B$3:$B$9)*($C$2:$C$8))

How to align and merge matching values, from various columns, in excel

I have had some success matching data from various columns and creating a new data output. Here is what I typically start with;
COL A COL B COL C COL D
ITEM VALUE ITEM VALUE2
---- ---- ---- ----
A 1 B 100
B 2 A 200
C 3 F 300
G 4 E 400
H 5 C 500
J 6 M 600
And I can achieve this result using VLOOKUP;
COL E COL F COL G
ITEM VALUE VALUE2
---- ---- ----
A 1 200
B 2 100
C 3 500
G 4
H 5
J 6
But what I'm really after is this: both matching AND merging;
COL E COL F COL G
ITEM VALUE VALUE2
---- ---- ----
A 1 200
B 2 100
C 3 500
E 400
F 300
G 4
H 5
J 6
M 600
I'm punching a tad above my weight class with this one, so any help would be greatly appreciated.
With something like:
=COUNTIF(A:A,C2)
in a column and copied down you should get indication of which items are not already present in ColumnA. Sort with C:D on that basis and simply copy the items associated with 0 to ColumnA and their values to ColumnD, then sort and apply your VLOOKUP.
The following is based on: http://www.get-digital-help.com/2009/05/25/create-a-drop-down-list-containing-only-unique-distinct-alphabetically-sorted-text-values-using-excel-array-formula/#comment-74005
It's a great site for clever use of Excel formulas and does a good job of explaining the monster formulas below, creating dynamic named ranges, and how to work with array formulas. Look at it!
My solution is not quite as elegant as I'd like but it works. So, you'll need to create some named ranges.
One for the items in COL A (e.g. "Items1") and one for the items in COL C (e.g. "Items2")
In COL G we'll have another List "AllItems1". In COL H we'll have another List "AllItems2". You'll need to make a named range of "AllItems1" too.
The array formula in COL G is:
=IFERROR(IFERROR(INDEX(Items1,MATCH(0,IF(MAX(NOT(COUNTIF($G$1:$G1,Items1))* COUNTIF(Items1,">"&Items1)+1))=(COUNTIF(Items1,">"&Items1)+1),0,1),0)),INDEX(Items2,MATCH(0,IF(MAX(NOT(COUNTIF($G$1:$G1,Items2))*(COUNTIF(Items2,">"&Items2)+1))=(COUNTIF(Items2,">"&Items2)+1),0,1),0))),"")
Which is a mouthful. This cascades through each list (i.e. Items1 and Items2) and gives you a non-repetitive sorta alphabetized list of the items in "Items1" and "Items2".
COL G is really just an intermediate step to get us to COL H. I haven't found a way to combine two lists without doing this step. Let me know if you find a better way.
To get a non-repetitive AND alphabetized list of the items we put the following array formula in COL H:
=IFERROR(INDEX(AllItems1,MATCH(0,IF(MAX(NOT(COUNTIF($H$1:$H1,AllItems1))*(COUNTIF(AllItems1,">"&AllItems1)+1))=(COUNTIF(AllItems1,">"&AllItems1)+1),0,1),0)),"")
Then we do VLOOKUP s off of "AllItems2".
Put the following VLOOKUP in COL I :
=IFERROR(VLOOKUP($H2,$A$2:$B$7,2,0),"")
And put the following VLOOKUP in COL J:
=IFERROR(VLOOKUP($H2,$C$2:$D$7,2,0),"")
Of course, you could make "AllItems2" a named range also and use that in the VLOOKUP s.
I think that gets you what you want.

Resources