Spotfire: Calculate delta between two filtered series - calculated-columns

I must calculate a delta between two time series, based on filtering over one column.
Here's the situation, I've got a table like this:
The user chooses a reference Id with a dropdown list, and the string is stored in document property ReferenceId.
I want a calculated column that will dynamically calculate the difference between the current row value and the corresponding reference row value. For instance, if ReferenceId=Id1:
How can I do that?
Thanks in advance.

You'll need to reference year to get the correct value... This should work
100*([Value]-First(case when "${ReferenceId}"=[Id1] then [Value] end) over [Year])/First(case when "${ReferenceId}"=[Id1] then [Value] end) over [Year]

Related

Pivot table, Excel Online, show all months in time period as columns even if values are empty? (recreating Query from Google Sheets)

I have a Google Sheets spreadsheet with a query formula and I'm trying to recreate this in Excel. I thought Power Query would be the right place, but it doesn't seem to be available in 365 online.
I tried building a pivot table and got some what close, but it's missing months that have no data.
This is my Google Sheets formula
where F != 0
group by E
order by sum(F) desc
label sum(F) ''",0)
The formula spits out something like this:
(I included the headers in my screenshot, but they are not part of the query formula)
In my pivot table, this is the output
I have a helper table of all the months in the time period selected (they're dynamic based on the starting month the user selects), but I don't see a way of referencing that in my sheet.
Before I go buy a subscription to get the desktop version of Excel to work with Power Query, is there something I can do here to get the pivot table to do what I want?
Any help is appreciated.
Interesting approach... see if this is what you are looking for:
Assume you are given data set like this:
Formula:
=LAMBDA(RANGE,
LAMBDA(REVENUE,DATE,VALUE,
LAMBDA(UREVENUE,UDATE,VALUE,
LAMBDA(PIVOT,
HSTACK(
VSTACK("REVENUE",UREVENUE),
BYROW(PIVOT,LAMBDA(ROW,IF(NOT(ISNUMBER(INDEX(ROW,1))),"TOTAL",SUM(ROW)))),
PIVOT
)
)(
MAKEARRAY(COUNTA(UREVENUE)+1,COUNTA(UDATE),LAMBDA(ROW,COL,
IFS(
ROW=1,INDEX(UDATE,COL),
TRUE,SUM(FILTER(VALUE,(REVENUE=INDEX(UREVENUE,ROW-1))*(DATE=INDEX(UDATE,COL)),0))
)
))
)
)(UNIQUE(REVENUE),UNIQUE(DATE),IF(VALUE="",0,VALUE))
)(INDEX(RANGE,,1),INDEX(RANGE,,2),INDEX(RANGE,,3))
)($A$2:$C$19)
This formula mainly uses MAKEARRAY() with INDEX() to recreate the output of google QUERY().
name $A$2:$C$19 as RANGE with LAMBDA(),
name Col1,Col2,Col3 of RANGE as REVENUE,DATE,VALUE with LAMBDA(),
get unique values of REVENUE and DATE with UNIQUE(),
fill in 0 for empty values in VALUE with IF(),
name the unique values as UREVENUE and UDATE, and update the values of VALUE with LAMBDA(),
get pivot sum of REVENUE and DATE with MAKEARRAY(), which COUNTA(UREVENUE)+1 as number of ROW, and COUNTA(UDATE) as number of COL,
the reason why we need to +1 in ROW count, is because that we have to add a header row for the pivoted data,
inside MAKEARRAY(), fill in every CELL with IFS() according to some conditions,
when ROW index is 1, it should be the header row, so return the value of UDATE as header, INDEX() here determines which value of UDATE should be shown according to the COL index,
when ROW index is not 1, FILTER() the data of VALUE according to UREVENUE and UDATE, same as step 9, we use INDEX() to determines which value of the given data sets are we referencing, SUM() the result of FILTER() to form a single value for each CELL,
name the result of MAKEARRAY() as PIVOT with LAMBDA(),
get SUM() of each ROW in PIVOT with BYROW(),
stack the outputs with VSTACK() and HSTACK() to form the result.
Just noticed that you have mentioned about there is another table to select the data you would like to display according to months, so I updated the formula, and also separated the header part from MAKEARRAY() so the whole thing may hopefully looks logically easier to understand:
=LAMBDA(DATARANGE,SELECTED,HEADERS,
LAMBDA(REVENUE,DATE,VALUE,SELECTED,
LAMBDA(UREVENUE,VALUE,
LAMBDA(PIVOT,HEADERS,
LAMBDA(TOTAL,
VSTACK(HEADERS,HSTACK(UREVENUE,TOTAL,PIVOT))
)(BYROW(PIVOT,LAMBDA(ROW,SUM(ROW))))
)(
MAKEARRAY(COUNTA(UREVENUE),COUNTA(SELECTED),LAMBDA(ROW,COL,
SUM(FILTER(VALUE,(REVENUE=INDEX(UREVENUE,ROW))*(DATE=INDEX(SELECTED,COL)),0))
)),
HSTACK(HEADERS,TRANSPOSE(SELECTED))
)
)(UNIQUE(REVENUE),IF(VALUE="",0,VALUE))
)(INDEX(DATARANGE,,1),INDEX(DATARANGE,,2),INDEX(DATARANGE,,3),TRIM(TEXTSPLIT(SELECTED,,",")))
)($A$2:$C$19,$F$1,HSTACK("REVENUE","TOTAL"))
name $A$2:$C$19 as DATARANGE, $F$1 as SELECTED, HSTACK("REVENUE","TOTAL") as HEADERS with LAMBDA(), which the two texts inside HEADERS would be the titles of output column 1 and 2,
name Col1,Col2,Col3 of DATARANGE as REVENUE,DATE,VALUE, and TEXTSPLIT() SELECTED into an ARRAY, apply TRIM() on the ARRAY to get rid of extra spacing if there is any, and update it as values of SELECTED with LAMBDA(),
get unique values of REVENUE with UNIQUE(), and fill in 0 for empty values in VALUE with IF(),
name the unique values as UREVENUE, and update the values of VALUE with LAMBDA(),
update values of HEADERS, HSTACK() it with TRANSPOSE(SELECTED) to form the complete HEADERS,
get pivot sum of REVENUE and DATE with MAKEARRAY(), which COUNTA(UREVENUE) as number of ROW, and COUNTA(SELECTED) as number of COL,
inside MAKEARRAY(), fill in every CELL with FILTER(), filter the data of VALUE according to UREVENUE and SELECTED, uses INDEX() to determines which value of the given data are we referencing as criteria, SUM() the result of FILTER() to form a single value for each CELL, name the result of MAKEARRAY() as PIVOT with LAMBDA(),
get SUM() of each ROW in PIVOT with BYROW(), name the result of BYROW() as TOTAL with LAMBDA(),
stack the outputs with VSTACK() and HSTACK() to form the final result.
Some SORT() is added to help rearrange the output data if needed, controlled by the value of SORTREVENUE,SORTTOTAL,SORTPIVOT.
In the 1st LAMBDA(), pass ASC or DESC to activate related SORT(), or leave it blank to turn that SORT() off.
Be noticed, sort result of SORTTOTAL will overwrite sort result of SORTREVENUE.
=LAMBDA(DATARANGE,SELECTED,HEADERS,SORTREVENUE,SORTTOTAL,SORTPIVOT,
LAMBDA(REVENUE,DATE,VALUE,SELECTED,MONTHS,
LAMBDA(UREVENUE,SELECTED,
LAMBDA(PIVOT,HEADERS,
LAMBDA(TOTAL,
VSTACK(
HEADERS,
LAMBDA(OUTPUT,ASC,DESC,
IF(OR(ASC,DESC),SORT(OUTPUT,2,IF(DESC,-1,ASC)),OUTPUT)
)(HSTACK(UREVENUE,TOTAL,PIVOT),SORTTOTAL="ASC",SORTTOTAL="DESC")
)
)(BYROW(PIVOT,LAMBDA(ROW,SUM(ROW))))
)(
MAKEARRAY(COUNTA(UREVENUE),COUNTA(SELECTED),LAMBDA(ROW,COL,
SUM(FILTER(VALUE,(REVENUE=INDEX(UREVENUE,ROW))*(DATE=INDEX(SELECTED,COL)),0))
)),
HSTACK(HEADERS,TRANSPOSE(SELECTED))
)
)(
LAMBDA(REVENUE,ASC,DESC,
IF(OR(ASC,DESC),SORT(REVENUE,,IF(DESC,-1,ASC)),REVENUE)
)(UNIQUE(REVENUE),SORTREVENUE="ASC",SORTREVENUE="DESC"),
LAMBDA(TEXT,NUM,ASC,DESC,
IF(OR(ASC,DESC),
XLOOKUP(SORT(XLOOKUP(SELECTED,TEXT,NUM),,IF(DESC,-1,ASC)),NUM,TEXT),
SELECTED
)
)(INDEX(MONTHS,,1),INDEX(MONTHS,,2),SORTPIVOT="ASC",SORTPIVOT="DESC")
)
)(
INDEX(DATARANGE,,1),
INDEX(DATARANGE,,2),
INDEX(DATARANGE,,3),
TRIM(TEXTSPLIT(UPPER($F$1),,",")),
HSTACK({"JAN";"FEB";"MAR";"APR";"MAY";"JUN";"JUL";"AUG";"SEP";"OCT";"NOV";"DEC"},SEQUENCE(12))
)
)($A$2:$C$19,$F$1,HSTACK("REVENUE","TOTAL"),"DESC","DESC","DESC")

Calculated Fields in excel pivot

I have column ('CSAT') in a sheet that has numbers 1 and 0 in each cell. '1' represents 'Satisfied' and '0' represents 'Disatisfied'. I want to make a pivot from this sheet and have a new calculated field in it ('CSAT %') that will give me the score by dividing (Total 'Satisfied') count by (Total 'Dissatisfied + Total 'Satisfied') * 100.
I tried with COUNTIF but i dont think we can use this formula in pivot
Calculated Fields and Items in PivotTables are tricky. The main tripping point is understanding that Calculated Fields and Items operate on the totals, not on the individual values in the underlying data.
For example, if you created a new Field that was equal to Field1 * Field2 and data is being summarized by SUM, Excel doesn't multiply all of the respective values in each field and then sum the results. It first sums the fields for each category and then multiplies those results. What it's really doing is SUM(Field1) * SUM(Field2) for each category.
You can use some worksheet functions in the calculated fields, but you have to remember you're still operating on the totals. So if you created a new Field that was equal to Count(Field1) * Count(Field2), you're (almost) always just going to get an answer of 1. This is because the calculation is actually doing Count(SUM(Field1)) * Count(SUM(Field2)) for each category. The sum of each field is a single number, so the calculation is just doing 1*1 for each category.
So for this reason, you can't use aggregating functions like SUMIF or COUNTIF which need to look at each individual elements. Since you need to look at individual elements, you actually can't use a Calculated Field for your solution at all.
What you can do is use a Calculated Item!
The main catch here is you can't use any field in more than 1 location when calculated items are involved. Excel just throws an error message saying you're not allowed.
So if you have a category column as well as the CSAT column, you need to create another dummy column full of 1's to operate on.
You can then set up pivot table as follows:
Category field to Rows.
Dummy field to Data area, summarized by Sum
CSAT field to Columns
Click on the CSAT column headers in the pivot table and choose: PivotTable Tools > Fields, Items, & Sets > Calculated Item
Set Name for your new Item to CSAT%
Enter the formula: ='1'/('0'+'1')
On the CSAT field, hide items 1 and 0, so only the CSAT% field is visible
Result:
A couple of notes:
When entering fields and items in calculated fields and items, do so by placing the cursor where you want in the Formula then double clicking on the field/item name from the lists below. This will add brackets and quotes as required in the correct format.
Note that the formula doesn't need SUM around the item names, because calculated fields/items always work on the total of values. They are totalled according to how the data is summarized in the pivot table.
The dummy column was added with all values of 1 so that summing these values gives you the count, from which the percentage can be calculated using the formula specified.
Answer without using calculated fields:
Assuming you have categories in the row fields, you can put CSAT as a column field as well as a data field then choose to summarize values by Count and show values as a percentage of row totals:
After putting CSAT in column and data fields, right click on the data and select Summarize Values By > More Options...
First choose to Summarize Values By Count:
Then click Show Values As tab and select % of Row Total:
You'll then have percentage of 1's under the CSAT=1 column:

calculated field in pivot table divide one column by the other EXCEL

I have two columns in a pivot table. Count of Work orders, and Sum of the Cost.
I want to insert a calculated field that simply divides the sum of cost by count of work orders to get an average per work order.
When I put I insert a calculated field with the following formula, it yields the total cost, not the average. You'll see the fields are subtotal (cost) and WO#(work order)
And here is what the output looks like in my pivot table.
When you add a calculated field in a pivot table, you need to only add the reference, not a calculation inside of it, so you don't need to add Sum or Count in your definition. However, Excel works calculated fields in a very infuriating manner - first it adds your values and then performs the calculation - if, for example, I have a calculated field that's simply field3=field2/field1, when I want to display the SUM of these values, instead of sum(field3), it does sum(field2)/sum(field1)
I would recommend doing this calculation outside of the pivot.
For example, see my results when I have the following table as input for a pivot
I had the same issue and found the answer I needed. Like the OP, I want to calculate an average -- SUM(field 1) divided by COUNT(field 2) -- but the problem with this is that there are two functions in the same formula (SUM divided by COUNT). As we have seen, using multiple functions in the same calculation produces unintended results.
The key that worked for me was to create a new field (field 3) in the raw data with a formula that assigns a 1 to items I want to count and a 0 to items I don not want to count, so the count of this column is just the sum. In this way, I convert COUNT(field 2) in the denominator to SUM(field 3). So, the result I need is now SUM divided by SUM, same function on top and bottom, which Excel can handle. And luckily for me in this situation, Excel's "infuriating manner" of calculating is exactly what I want.
As Fernando stated, the calculated field should just refer to the field itself; it shouldn't use SUM or COUNT or anything else. The function you want will be applied when you add the field to the pivot table and you choose the function you want.
I set my calculated function to be [field 1 / field 3], with an IF statement to avoid division by 0, and I used the SUM function when I put the calculated field in the pivot table. The end result is SUM(field 1) / SUM(field 3), which equals SUM(field 1) / COUNT(field 2)
Summary:
Restate your formula so that the same function is used on all fields; for example, find a way to restate an average (SUM/COUNT) to be SUM/SUM or COUNT/COUNT, etc.
Add fields to the raw data that will aid in the restated formula; for example, if your restated formula uses a SUM instead of a COUNT, create a new field in the raw data that assigns 1's and 0's so that the sum of this new field is equal to the count of the other field.
Create the calculated pivot field that uses the fields corresponding to the restated formula, including the new field you just created; do not use SUM or COUNT at this point.
Add your calculated field to the data area of the pivot table and choose the function you want; this function will be applied to each field that is referenced in the formula of the calculated field.

the result of calculated new column is not right

There is a table of 12 columns. I would like to add an extra column, where each entry stores the average value of the corresponding row across those 12 columns. I use the feature "Calculated new column" to fulfill this task. After getting the result, I noticed that the average value was returned as zero when one of the 12 columns has zero value on that specific row. For other rows, the calculation is just OK if none of the entries in those 12 columns is zero. I attached the screenshot of resulting table and the calculation procedure in the data table properties for your reference. Would you like to let me know the possible reason?
average value was returned as zero when one of the 12 columns has zero
Actually you don't have zero values, but null values, which is different! Use SN() function to manage null values (need to use for all the columns)!

Select a subset of a table for further processing

In Microsoft Excel, how can I compute a range (portion of a column), based on the values in another column of the same table, returning the result in an Array form for further processing by other functions?
In SQL, what I mean is "SELECT field1 FROM table WHERE field2=value".
The selected results will be fed (twice) to FREQUENCY(), to compute the number of distinct entries in "field1". That is: given an existing table like this:
Box Date
1 07/01/12
13 07/01/12
13 07/01/12
27 07/18/12
13 07/18/12
55 07/18/12
I want to produce a resulting table like this:
Boxes Date
2 07/01/12
3 07/18/12
Note that "13" is only counted once in the first date ("distinct"), but it's still counted again in the second date.
I already have an expression that does the right thing for the whole of the table,
=SUM(N(FREQUENCY(Box,Box)>0))
where "Box" is a named range of the first table, consisting of the whole Box column. (Using the same range/array/list as the data and the bins for FREQUENCY is a stupefyingly subtle trick actually contained in the Excel help but -- alas! -- by no means adequately explained.)
But I want (several) subsets, one for each date. I want to expand my "SUM(N(FREQUENCY…" expression to act only on the rows of the first table whose Date column matches the Date column of the row being computed. That is, again resorting to SQL,
SELECT count(DISTINCT t1.Box), t2.Date
FROM `t1` JOIN `t2` ON (Date)
GROUP BY Date
I can even build a pivot table of the interesting values (which gets me counts in its cells), then use a parallel, date-indexed column of
=COUNTIF(…)
to reduce each row of counts down to a single count of uniques for that date. But this requires me to update the pivot table to notice new data in the base table, and then to drag-expand the column of answers to include the new date (or suffer ugly value error markers). So something more automatic, less fussily manual, would be sweet.
I guess not available when you asked, but Excel 2013 has Distinct Count as an option in a PivotTable:

Resources