Division by zero error when executing query - ms-access-2013

I am using a Crosstab query to calculate the percentage from a pair of numbers and a Make query to put the result into a different table.
Source data is something like this:
ITEMS |PAIRS |VAL
Element1 |dividend |43
Element1 |divisor |33
Element2 |dividend |65
Element3 |dividend |73
Element4 |dividend |32
Element4 |divisor |23
Element3 |divisor |49
PLEASE NOTE: In a real situation, it is entirely possible that one of the two elements is missing (the divisor part of Element2, in this case).
Here is how I made the Crosstab query (named QCross) to overcome the 'Overflow' error.
TRANSFORM Sum(TRawData.Val) AS SumOfVAL
SELECT TRawData.ITEMS, IIf(IsError(Sum(IIf(TRawData.[PAIRS]='dividend', TRawData.[VAL],0))/Sum(IIf(TRawData.[PAIRS]='divisor',TRawData.[VAL],0))),0,Sum(IIf(TRawData.[PAIRS]='dividend',TRawData.[VAL],0))/Sum(IIf(TRawData.[PAIRS]='divisor',TRawData.[VAL],0))) AS MYRES
FROM TRawData
GROUP BY TRawData.ITEMS
PIVOT TRawData.PAIRS;
And this is the Make query
SELECT QCross.ITEMS, QCross.MYRES INTO TOutcome
FROM QCross;
The Crosstab query is working well, however I am facing with a 'division by zero' error when the Make query is executed (!Run); it worth to note the error doesn't comes up in 'View' mode.
Where am I doing wrong?
Crosstab query execution:
Make query (View mode):

Related

Business Objects 4.x: need to combine two queries similar to a UNION

I can't seem to figure out how to combine the result of 2 Business Objects queries.
Both queries return a set of codes and a number of hours. Query 1 can have codes that do not appear in Query 2, and Query 2 can have codes that do not appear in Query 1.
The resulting report should contain all codes from both Query 1 and Query2, a column with the sum of hours from Q1 for that code, and a column with the sum of hours from Query 2 for that code. If one of the queries doesn't have a code in it, it would return a blank or 0 total.
Example:
Q1 results:
|Code|Value|
|:---|:----|
|A|15|
|A|17|
|B|12|
|D|22|
|D|35|
|E|16|
|E|9|
|E|11|
Q2 results:
|Code|Value|
|:---|:----|
|A|5|
|A|19|
|B|33|
|C|17|
|C|24|
|E|78|
|E|12|
Report:
|Code|Value1|Value2|
|----|------|------|
|A|32|24|
|B|12|33|
|C| |41|
|D|57| |
|E|36|90|
|Total|137|188|
When I create the Business Object report table as normal, only the values of Query 1 are used, and I miss the row for value C. If I flip the queries around, I miss the row for value D.
How do I set up my report to show all the code values?
Edit: Sorry for the formatting of the tables, in the preview it looks perfect. :(

Power BI DAX measure: Count occurences of a value in a column considering the filter context of the visual

I want to count the occurrences of values in a column. In my case the value I want to count is TRUE().
Lets say my table is called Table and has two columns:
boolean value
TRUE() A
FALSE() B
TRUE() A
TRUE() B
All solutions I found so far are like this:
count_true = COUNTROWS(FILTER(Table, Table[boolean] = TRUE()))
The problem is that I still want the visual (card), that displays the measure, to consider the filters (coming from the slicers) to reduce the table. So if I have a slicer that is set to value = A, the card with the count_true measure should show 2 and not 3.
As far as I understand the FILTER function always overwrites the visuals filter context.
To further explain my intent: At an earlier point the TRUE/FALSE column had the values 1/0 and I could achieve my goal by just using the SUM function that does not specify a filter context and just acts within the visuals filter context.
I think the DAX you gave should work as long as it's a measure, not a calculated column. (Calculated columns cannot read filter context from the report.)
When evaluating the measure,
count_true = COUNTROWS ( FILTER ( Table, Table[boolean] = TRUE() ) )
the first argument inside FILTER is not necessarily the full table but that table already filtered by the local filter context (including report/page/visual filters along with slicer selections and local context from e.g. rows/column a matrix visual).
So if you select Value = "A" via slicer, then the table in FILTER is already filtered to only include "A" values.
I do not know for sure if this will fix your problem but it is more efficient dax in my opinion:
count_true = CALCULATE(COUNTROWS(Table), Table[boolean])
If you still have the issue after changing your measure to use this format, you may have an underlying issue with the model. There is also the function KEEPFILTERS that may apply here but I think using KEEPFILTERS is overcomplicating your case.

MS Access error - Division by zero when executing query

I am using a Crosstab query to calculate the percentage from a pair of numbers and a Make query to put the result into a different table.
Source data is something like this:
ITEMS |PAIRS |VAL
Element1 |dividend |43
Element1 |divisor |33
Element2 |dividend |65
Element3 |dividend |73
Element4 |dividend |32
Element4 |divisor |23
Element3 |divisor |49
PLEASE NOTE: In a real situation, it is entirely possible that one of the two elements is missing (the divisor part of Element2, in this case).
Here is how I made the Crosstab query (named QCross) to overcome the 'Overflow' error.
TRANSFORM Sum(TRawData.Val) AS SumOfVAL
SELECT TRawData.ITEMS, IIf(IsError(Sum(IIf(TRawData.[PAIRS]='dividend', TRawData.[VAL],0))/Sum(IIf(TRawData.[PAIRS]='divisor',TRawData.[VAL],0))),0,Sum(IIf(TRawData.[PAIRS]='dividend',TRawData.[VAL],0))/Sum(IIf(TRawData.[PAIRS]='divisor',TRawData.[VAL],0))) AS MYRES
FROM TRawData
GROUP BY TRawData.ITEMS
PIVOT TRawData.PAIRS;
And this is the Make query
SELECT QCross.ITEMS, QCross.MYRES INTO TOutcome
FROM QCross;
The Crosstab query is working well, however I am dealing with a 'division by zero' error when the Make query is executed (!Run).
Can anyone help me to understand where the error is?
You code is not fast to comprehend, but a generic method is to check if the divisor is zero and, if so, replace with, say, 0 and 1 for dividend and divisor respectively to obtain a valid division and a result of 0:
result = IIf(divisor = 0, 0, dividend) / IIf(divisor = 0, 1, divisor)

Propel, selecting just one column

I am trying to run a query in propel that runs an aggregate function (SUM).
My Code
$itemQuery = SomeEntity::Create();
$itemQuery->withColumn('SUM(SomeColumn)', someColumn)
->groupBy(SomeForeignKey);
Problem
It should theoretically return the sum of every group of items but the problem is propel tries to fetch all columns, and also appends a bunch of other columns to the group by clause. This results in an unexpected categorisation and therefore the sum is incorrect.
Is there anyway to make propel fetch just the column I am running the aggregation function on so that the group by statement works as well?
You need to add a select statement for the column and the foreign key:
$itemQuery = SomeEntity::Create();
$itemQuery->select(array(SomeColumn, SomeForeignKey));
$itemQuery->withColumn('SUM(SomeColumn)', someColumn);
$itemQuery->groupBy(SomeForeignKey);

Pig Script: Null values after join and GENERATE

I have a strange dilemma in my Pig Script. I am joining multiple tables and one of the last joins is as follows:
a = JOIN O_1 by ((long)OpropID, (long)OAID) LEFT, property by ((long)GPropID, (long)prop_AID);
If I filter my result by specific data points, I get the proper results for those fields from the property table (right table in the join). Even without the filter, the resultset is correct, I'm only filtering it to test the results.
b = filter a by OpropID==12 and OAID==10;
dump b;
However, if create a subsequent GENERATE statement immediately after the join, the same fields (last two in the example below) return NULL results:
c = FOREACH a GENERATE gID, p_AID, OpropID, OAID, GPropID, prop_AID;
I've tried using $16, $17 instead of the field names; I've also used property::GPropID or property::prop_AID to no avail.
Any help at this point would be appreciated.

Resources