Subtracting two single-valued (non-null) Calculated Fields and getting single value of NULL in Tableau Desktop - calculated-field

Both calculated fields return a positive integer with the data I have. I'm not sure why subtracting them (in the third calculated field) is giving me a NULL value. I've tried using the SUM function in the third calculated field on each of the calculated fields it uses to calculate the difference, and I get a hugely erroneous number.
// this is in a condition on a filter on WeekID
MAX([WeekID]) = MAX([--MaxWeekID])
--1wk_one_yr_ago_ship_quantity
Role: Continuous Measure
Type: Calculated Field
Default aggregation: Sum
Status: Valid
Formula
IF [--WeekID] = [--MaxWeekID_minus_1_yr] THEN {EXCLUDE [WeekID]: SUM([ShipQuantity])} END
Domain
The single value 338,604.
--1wk_ship_quantity
Role: Continuous Measure
Type: Calculated Field
Default aggregation: Sum
Status: Valid
Formula
IF [--WeekID] = [--MaxWeekID] THEN {EXCLUDE [WeekID]: SUM([ShipQuantity])} END
Domain
The single value 154,163.
--1wk_ship_diff
Role: Continuous Measure
Type: Calculated Field
Default aggregation: Sum
Status: Valid
Formula
[--1wk_ship_quantity] - [--1wk_one_yr_ago_ship_quantity]
Domain
The single value Null.

Related

Summing the value of the aggregate function Last()

enter image description hereI am trying to design a report in ssrs that returns the last opening stock for each product in the dataset. To achieve this I used
=Last(Fields!CustProdAdj_new_openingstockValue.Value).
This works fine. But where I encountered a problem is in getting the sum of all the opening stock for each product. I tried using
=sum(Last(Fields!CustProdAdj_new_openingstockValue.Value))
but I got the error message
[Error on Preview]
Please is there another way to go about this
I have tried using aggregate(), runningValue(), to no avail
This is the dataset
This is the report layout
On previewing having used max()
This is probably easier to do directly in the dataset query but assuming you cannot change that, then this should work...
This assumes your data is ordered by the CustProdAdj_createdon column and that this is a date, datetime or some other ordered value, change this bit if required.
=SUM(
IIF(Fields!CustProdAdj_createdon.Value = Max(Fields!CustProdAdj_createdon.Value, "MyRowGroupNameHere"),
CustProdAdj_new_openingstockValue,
0)
)
Change the MyRowGroupNameHere to be the name of the rowgroup spelled exactly as it is in the rowgroup panel below the main design panel. Case sensitive and include quotes.
What this does is, for each row within the rowgroup "MyRowGroupNameHere", compare the CustProdAdj_createdon date to the max CustProdAdj_createdon across the rowgroup. . If it is the same then return the CustProdAdj_new_openingstockValue else return 0.
This will return the value required on only 1 record within the group.
For example, if you had 1 row per day then only on the last day of the month would a value be returned other than 0 because only the date of the last record would match the maximum date within the group.
Then it simply sums the results of this up.

SuiteQL cannot includes keyword 'DISTINCT'?

I keep on getting an error when I try to use the distinct keyword with SuiteQL.
This is my code:
var bannedItemIdArr = [];
var querySQL = "SELECT DISTINCT item.ID AS idRAW /*{id#RAW}*/ FROM item, (SELECT itemMember.parentitem AS parentitem, itemMember.parentitem AS parentitem_join, itemMember.ID AS ID, item_0.itemtype AS itemtype, itemMember.item AS item, item_0.itemtype AS itemtype_crit, item_0.isinactive AS isinactive_crit FROM itemMember, item item_0 WHERE itemMember.item = item_0.ID(+)) itemMember_SUB WHERE item.ID = itemMember_SUB.parentitem(+) AND ((UPPER(item.itemtype) IN ('ASSEMBLY') AND UPPER(itemMember_SUB.itemtype_crit) IN ('INVTPART', 'ASSEMBLY') AND itemMember_SUB.isinactive_crit = 'T' AND NVL(item.isinactive, 'F') = 'F'))";
var myPagedResults = query.runSuiteQLPaged({ query: querySQL, pageSize: 1000, });
The error:
Search error occurred: Invalid or unsupported search
How can I solve this error?
So the trick here is to use group by and not distinct.
e.g.,
select item.id
from item
group by item.id
That said your query looks like it is using the old oracle-specific syntax for joins (using (+)) instead of the suite-ql supported ANSI92 syntax of left join and right join. Also, you will have to change nvl to use the ANSI92 coalesce.
Have you looked at query.Aggregate?
Enum Description
Module
Holds the string values for aggregate functions supported with the N/query Module. An aggregate function performs a calculation on the column or condition values and returns a single value. Each value in this enum (except MEDIAN) has two variants: distinct (using the _DISTINCT suffix) and nondistinct (using no suffix). The variant determines whether the aggregate function operates on all instances of duplicate values or on just a single instance of the value. For example, consider a situation in which the MAXIMUM aggregate function is used to determine the maximum of a set of values. When using the distinct variant (MAXIMUM_DISTINCT), the aggregate function considers each instance of duplicate values. So if the set of values includes three distinct values that are all equal and all represent the maximum value in the set, the aggregate function lists all three instances. When using the nondistinct variant (MAXIMUM), only one instance of the maximum value is listed, regardless of the number of instances of that maximum value in the set. This enum is used to pass the aggregate function argument to Component.createColumn(options), Component.createCondition(options), Query.createColumn(options), and Query.createCondition(options).
N/query Module
Values
Value
Description
AVERAGE
Calculates the average value.
AVERAGE_DISTINCT
Calculates the average distinct value.
COUNT
Counts the number of results.
COUNT_DISTINCT
Counts the number of distinct results.
MAXIMUM
Determines the maximum value. If the values are dates, the most recent date is determined.
MAXIMUM_DISTINCT
Determines the maximum distinct value. If the values are dates, the most recent date is determined.
MEDIAN
Calculates the median value
MINIMUM
Determines the minimum value. If the values are dates, the earliest date is determined.
MINIMUM_DISTINCT
Determines the minimum distinct value. If the values are dates, the earliest date is determined.
SUM
Adds all values.
SUM_DISTINCT
Adds all distinct values.
Syntax
// Add additional code
...
var myTransactionQuery = query.create({
type: query.Type.TRANSACTION
});
var myAggColumn = myTransactionQuery.createColumn({
fieldId: 'amount',
aggregate: query.Aggregate.AVERAGE
});
myTransactionQuery.columns = [myAggColumn];
...
// Add additional code

QlikView Calculated Dimension

I have the following table:
enter image description here
I need to add a calculated dimension ProductEntryDate based on the following condition:
If the DrugSubstanceFlag = 'Y', then the ProductEntryDate is the Maximum of the 'Patent Expiry Date'. I will also need to add a calculated dimension 'RestrainingPatent' which is the Patent Number matching the ProductEntryDate. So in this case the ProductEntryDate will be '24/11/2016' and the RestrainingPatent will be '7326708'. How can this be accomplished in QlikView? Thanking you in advance

Powerpivot using If function

I have a table that contains text values in several columns. One column has a text value of either true or false. The data type of this column is Text.
Now I want to add a measure that calculates the rows where the column has a value of true. I am using an IF function:
=IF(Table[column a] = "true", COUNTROWS(DISTINCT(columnid)),0)
When doing this, I am getting an error saying that the value cannot be determined in this context. What am I doing wrong?
You are creating a measure that has a context of several rows rather than 1 row, so it can't determine the true/false test in your if statement. Try this DAX for your calculated measure:
MyMeasure:= Calculate(DistinctCount(Table[columnid]), Table[column a] = "True")
Here's a good article on the CALCULATE function in DAX: http://sqlblog.com/blogs/marco_russo/archive/2010/01/03/how-calculate-works-in-dax.aspx

How to check for NULL values when using the SUM function in SSRS

By default, the SUM function in SSRS excludes NULLs. I want to be able to check for ANY NULL values in the details group and throw an error in the summary group. In the details view I use this to check for NULLS:
=IIF(IsNothing(Fields!EquityPrice.Value)) ,"#Error", Fields!EquityPrice.Value*Fields!EquityShares.Value)
This works as desired.
When I use this in my summary section, it ignores the NULLS and returns the SUM of the non-null values. I want to return "#Error" instead:
=IIF(IsNothing(SUM(Fields!EquityPrice.Value))) ,"#Error", SUM(Fields!EquityPrice.Value*Fields!EquityShares.Value))
I have tried eliminating the SUM in the "IsNothing" expression but to no avail. Any help would be appreciated. Thanks in advance!
So just to confirm, if there is at least one NULL value in a group, #Error should be displayed?
You can use the following for the Summary expression:
=IIf(Sum(IIf(IsNothing(Fields!EquityPrice.Value),1,0)) > 0
, "#Error"
, Sum(Fields!EquityPrice.Value * Fields!EquityShares.Value))
This creates a count of NULL values - if that count is greater than zero, return #Error.
I made a simple report to test:
This uses your expression at the detail level and mine at the summary. Errors for the group with one NULL value as required: