Crystal Reports 11 - Formula. 2 Groups - crystal-reports

What is happening is that I have a formula that is in the Group Header 2. What it is doing is SUM all of Group 2 (Invoice ID)
formula = SUM({Commissions_ttx.CommissionAmount},{Commissions_ttx.arpARInvoiceID})
Group 1 is EmpID
Group 2 is InvoiceID
It is skipping over the Group of 1 instead is getting all of the InvoiceID. There can be more than 1 employee on this. So the totals are doubling. Any ideas?

in the sum function you need to do something like
SUM({Commissions_ttx.arpARInvoiceID},{your group name})
if you have multiple sum values you need to get the sum of each group separately into 2 variables the the sum of both values e.g.
variable1 = SUM({field1},{Group1})
variable2 = SUM({field2},{Group2})
variable3 = tonumber(variable1 + variable2)

Related

Delete group when its observation does not contain certain values in SAS

Refer to below table, an ID is considered as complete if at least one of its group having Day 1 to Day 3 (Duplicate allowed).
I need to remove ID which has Group not having full Day 1 to Day 3.
ID Group Day
1 A 1
1 A 1
1 A 2
1 A 3
1 B 1
1 B 3
2 A 1
2 A 3
2 B 2
Expected result
ID Group Day
1 A 1
1 A 1
1 A 2
1 A 3
1 B 1
1 B 3
With this reference, Delete the group that none of its observation contain the certain value in SAS
I have tried below code but it cannot remove ID 2.
PROC SQL;
CREATE TABLE TEMP AS SELECT
* FROM HAVE
GROUP BY ID
HAVING MIN(DAY)=1 AND MAX(DAY)=3
;QUIT;
PROC SQL;
CREATE TABLE TEMP1 AS SELECT
* FROM TEMP WHERE ID IN
(SELECT ID FROM TEMP
WHERE DAY=2)
;QUIT;
So you want to find the set of ID values where the ID has at least one GROUP that has all three DAY values? Find the list of IDs as a subquery and use it to subset the original data.
The key thing in subquery is you want there to be 3 distinct values of DAY. If your data could have other values of DAY (like missing or 4) then use a WHERE clause to only keep the values you want to count.
proc sql;
create table want as
select * from have
where id in
(select id from have
where day in (1,2,3)
group by id,group
having count(distinct day)=3
)
;
quit;
You can query the dataset with a removal list. For example:
proc sql noprint;
create table want as
select *
from have
where cats(group, id) NOT IN(select cats(group, id) from removal_list)
;
quit;
Creating the Removal List
This method will prevent you from having to do a Cartesian product on all IDs, groups, and days to create your removal list.
Assume that your data is sorted by ID, group, and day.
For each ID, the first day in the group must be 1
For each ID, all days in the group after the first day must have a difference of 1 from the previous day
Code:
data removal_list;
set have;
by ID Group Day;
retain flag_remove_group;
lag_day = lag(day);
/* Reset flag_remove_group at the start of each (ID, Group).
Check if the first day is > 1. If it is, set the removal flag.
*/
if(first.group) then do;
call missing(lag_day);
if(day > 1) then flag_remove_group = 1;
else flag_remove_group = 0;
end;
/* If it's not the first (ID, Group), check if days
are skipped between observations
*/
if(NOT first.group AND (day - lag_day) NE 1) then flag_remove_group = 1;
if(flag_remove_group) then output;
keep id group;
run;

What will be the SQL to get the results by applying group by on specific matching string of column value?

What will be the SQL to get the results by applying the group by on a specific matching string of column value?
Table: results ( this is not the physical table but the result of specific queries )
rname count1 count2
Avg-1 2 2
Avg-1 1 1
Avg-2 2 2
Avg-1 1 1
Zen-3 2 2
Zen/D 2 1
QA/C 3 1
QA/D 2 1
The expected output is:
rname count1 count2
Avg 6 6
Zen 4 3
QA 5 2
In expected output count1 is sum of all count1 of rname which match the string 'Avg', 'Zen' and 'QA' respectively. Same for count2.
What will be the SQL can you please give me some directions?
demo:db<>fiddle
SELECT
(regexp_split_to_array(rname,'[-/]'))[1],
SUM(count1) AS count1,
SUM(count2) AS count2
FROM
mytable
GROUP BY 1
regexp_split_to_array(rname,'[-/]') splits the rname value at the - or the / character. Taking the first part ([1]) gives you Avg, Zen or QA
Group hy this result (using the column index 1)
SUM up the values

Power BI average rating per product

I have a Ratings table with some product ratings:
productKey rating
product-1 4
product-1 5
product-2 3
I want to calculate the average rating per product:
Incorrect average (default): (4+5+3)/3 = 12/3 = 4
Average per product : ( (4+5)/2 + 3 ) / 2 = (4.5 + 3) / 2 = 3.75
I managed to do this with an intermediate table
1) Create table which averages ratings per product:
RatingsPerProd = SUMMARIZE(Ratings,Ratings[productKey],"averageRating",AVERAGE(Ratings[rating]))
which creates the following table:
productKey averageRating
product-1 4.5
product-2 3
2) Then I simply do an AVERAGE of averageRating
However I'd like to do this with 1 single measure on the original Ratings table, but whatever I try to do with the SUMMARIZE formula I get the following error:
The expression refers to multiple columns... cannot be converted to a
scalar value
How can I achieve average per product in 1 single measure on the original Ratings table?
You can try the following formula:
AveragePerProduct =
AVERAGEX (
VALUES ( Ratings[productKey] ),
CALCULATE ( AVERAGE ( Ratings[rating] ) )
)
Result:

Distinct Count after Sum

So I am looking to do a count after aggregation. Basically I want to be able to total up the Inventory count with a sum and then count how many times each employee has a non zero inventory count.
So for this data Jack/Jimmy would have a count of 1, Sam would have a count of 2 and Steve would have a count of 0. I could easily do this in SQL on the back end but I also want them to be able to use a date parameter. So if they shifted the date to only 1/1/17 Sam would have a count of 1 and everyone else would have a 0. Any help would be much appreciated!
Data
Emp Item Inventory Date
Sam Crackers 1 1/1/2017
Jack Crackers 1 1/1/2017
Jack Crackers -1 2/1/2017
Jimmy Crackers -2 1/1/2017
Sam Apples 1 1/1/2017
Steve Apples -1 1/1/2017
Sam Cheese 1 1/1/2017
With Date>= '1/1/17':
Emp NonZeroCount
Sam 2
Jack 1
Jimmy 1
Steve 0
With Date = '1/1/17':
Emp NonZeroCount
Sam 1
Jack 0
Jimmy 0
Steve 0
SQL I envision it replacing
Create Table #Test(
Empl varchar(50),
Item Varchar (50),
Inventory int,
Date Date
)
Declare #DateParam Date
Set #DateParam = '1/1/17'
Insert into #Test (Empl,Item,Inventory,Date)
Values
('Sam','Crackers',1,'1/1/2017'),
('Jack','Crackers',1,'1/1/2017'),
('Jack','Crackers',-1,'2/1/2017'),
('Jimmy','Crackers',-2,'1/1/2017'),
('Sam','Apples',1,'1/1/2017'),
('Steve','Apples',-1,'1/1/2017'),
('Sam','Cheese',1,'1/1/2017');
Select
Item,Sum(Inventory) as Total
into #badItems
from #Test
Where Date >= #DateParam
group by Item
having Sum(Inventory) <> 0
Select
T.Empl,Count(Distinct BI.Item)
From #Test T
Inner Join #badItems BI on BI.Item = T.Item
group by T.Empl
This is a good case for creating a set in Tableau.
Select the Item field in the data pane on the left, and right click to create a set based on that field. Name it Bad Items, and define it using the following formula on the Condition tab, which assumes you've defined a parameter named [DateParam] of type Date.
sum(if [Date] >= [DateParam] then [Inventory] end) <> 0
You can then use the set on the filter shelf, row shelf, in calculations or combine with other sets as desired.
P.S. I used an alias to display the text "Bad Items" instead of "In" in the table, set a manual default sort order for the Emp field (in case you are trying to reproduce this exactly)

Crystal Report: Sum Up a Column With Respect to value in other column

I Want to Sum Up A Column say (column1) in crystal report with respect to the value in column2.
EG:
Column1 Column2
10 SR
5 SR
20 ZR
I want to sum up column1 When Column2 value is 'SR'.
Out put should be '15' and not '35'.
How Do i Achieve it?
assuming column 1 and 2 are fields from your table.
if {tablefield1} = 'SR' then {tablefield2} else 0
Summary Sum to the report or group foot