MDX: Top 10 with filtered members - jasper-reports

I'm creating a report for JasperServer in iReport. I am creating a report with the top 10 companies with most product downloads of the selected products. Users can select in JasperReports which products with a list (parameter {$P{ProductFormat}}).
My dimension for products has the following structure: [Product].[ProductFactory].[ProductType], so input for the parameter for example can be: [Product].[ProductCategory1].[Product1], [Product].[ProductCategory2].[Product2], ... I also want those products available as rows so I can use them as fields in iReport. (product in where is not an option)
SELECT
NON EMPTY {
[Measures].[Orders]
} ON COLUMNS,
NON EMPTY
Crossjoin({TopCount({[USER_COMPANY].[Company].Members}, 10, [Measures].[Orders])}, {$P{ProductFormat}})
ON ROWS
FROM [Products]
WHERE $P{DateFilter}
This query return the top 10 companies of all products with the data filtered per products. I want a top 10 companies of the selected products, but I can't get it to work with topcount and as a row.

This query against AdvWrks via SSMS seems to work ok:
WITH
SET [ProductFormat] AS
{
[Product].[Product Categories].[Category].[Bikes]
,[Product].[Product Categories].[Category].[Clothing]
,[Product].[Product Categories].[Category].[Components]
}
SELECT
[Measures].[Reseller Sales Amount] ON 0
,Generate
(
[ProductFormat]
,
[Product].[Product Categories].CurrentMember
*
TopCount
(
[Geography].[Geography].[City].MEMBERS
,5
,[Measures].[Reseller Sales Amount]
)
) ON 1
FROM [Adventure Works];
It returns the following:
If I add a WHERE clause like the following then it seems to be context aware:
WHERE [Date].[Calendar].[Calendar Year].&[2006];

Related

Filter portal for most recently created record by group

I have a portal on my "Clients" table. The related table contains the results of surveys that are updated over time. For each combination of client and category (a field in the related table), I only want the portal to display the most recently collected row.
Here is a link to a trivial example that illustrates the issue I'm trying to address. I have two tables in this example (Related on ClientID):
Clients
Table 1 Get Summary Method
The Table 1 Get Summary Method table looks like this:
Where:
MaxDate is a summary field = Maximum of Date
MaxDateGroup is a calculated field = GetSummary ( MaxDate ;
ClientIDCategory )
ShowInPortal = If ( Date = MaxDateGroup ; 1 ; 0 )
The table is sorted on ClientIDCategory
Issue 1 that I'm stumped on: .
ShowInPortal should equal 1 in row 3 (PKTable01 = 5), row 4 (PKTable01 = 6), and row 6 (PKTable01 = 4) in the table above. I'm not sure why FM is interpreting 1Red and 1Blue as the same category, or perhaps I'm just misunderstanding what the GetSummary function does.
The Clients table looks like this:
Where:
The portal records are sorted on ClientIDCategory
Issue 2 that I'm stumped on:
I only want rows with a ShowInPortal value equal to 1 should appear in the portal. I tried creating a portal filter with the following formula: Table 1 Get Summary Method::ShowInPortal = 1. However, using that filter removes all row from the portal.
Any help is greatly appreciated.
One solution is to use ExecuteSQL to grab the Max Date. This removes the need for Summary functions and sorts, and works as expected. Propose to return it as number to avoid any issues with date formats.
GetAsTimestamp (
ExecuteSQL (
"SELECT DISTINCT COALESCE(MaxDate,'')
FROM Survey
WHERE ClientIDCategory = ? "
; "" ; "";ClientIDCategory )
)
Also, you need to change the ShowInPortal field to an unstored calc field with:
If ( GetAsNumber(Date) = MaxDateGroupSQL ; 1 ; 0 )
Then filter the portal on this field.
I can send you the sample file if you want.

MDX Query with Date Range Filter

I am new to the MDX queries. I am writing a MDX query to select a Measure value across months and I am putting date Range as filter here just to restrict no of Months returned. For eg I want Sales Revenue for each month in Date Range of 01-Jan-2014 to 30-Jun-2014. Ideally, it should give me sales value for six months i.e Jan, Feb, Mar, Apr, May and June. However when i write below query, I get error. PFB the below enter code here`ow query.
Select NON EMPTY {[Measures].[Target Plan Value]} ON COLUMNS,
NON EMPTY {[Realization Date].[Hierarchy].[Month Year].Members} ON ROWS
From [Cube_BCG_OLAP]
( { [Realization Date].[Hierarchy].[Date].&[20140101] :
[Realization Date].[Hierarchy].[Date].&[20141231] })
The error I get is The Hierarchy hierarchy already appears in the Axis1 axis. Here Date and Month Year belong to same dimension table named as Realization Date. Please help me. Thanks in advance.
You were missing the WHERE clause but I guess that was a typo. As your error message tells, you can't have members of the same hierarchy on two or more axes. In situations like this, you can use something like below which in MDX terminology is called Subselect.
Select NON EMPTY {[Measures].[Target Plan Value]} ON COLUMNS,
NON EMPTY {[Realization Date].[Hierarchy].[Month Year].Members} ON ROWS
From (
SELECT
[Realization Date].[Hierarchy].[Date].&[20140101] :
[Realization Date].[Hierarchy].[Date].&[20141231] ON COLUMNS
FROM [Cube_BCG_OLAP]
)
I like the exists function in this situation:
SELECT
NON EMPTY {[Measures].[Target Plan Value]}
ON COLUMNS,
NON EMPTY
EXISTS(
[Realization Date].[Hierarchy].[Month Year].Members
, {
[Realization Date].[Hierarchy].[Date].&[20140101] :
[Realization Date].[Hierarchy].[Date].&[20141231]
}
)
ON ROWS
FROM [Cube_BCG_OLAP]
Select
[Measures].[Target Plan Value]} On Columns
{
[Realization Date].[Hierarchy].[Date].&[20140101].Parent :
[Realization Date].[Hierarchy].[Date].&[20140631].Parent
}
On Rows
From [Cube_BCG_OLAP]
You need to create this same dimension only for filter in the cube, for example, dimension_filter -> hierarchy_filter -> level_filter

Reshaping data in a postgres query?

I have two tables in CartoDB, one of community district polygons, and one of sites that are in those community districts.
I know the district (borocd) of each site, so I can get a list of counts of sites of each type with:
SELECT borocd, type, count(*) FROM sites GROUP BY borocd, type
But I'm having a hard time wrapping my head around how I'd update my "districts" table with columns for count of type1 and count of type2 in a single query. I wound up doing this:
UPDATE districts
SET type1_sites = (
SELECT count(*) FROM sites
WHERE type='type1' AND districts.borocd = sites.borocd
GROUP BY borocd
)
And repeating that for type 2. But could I have done that more cleanly?
UPDATE districts
SET type1_sites = (
SELECT count(*) FROM sites WHERE type='type1' AND districts.borocd = sites.borocd
),
type2_sites = (
SELECT count(*) FROM sites WHERE type='type2' AND districts.borocd = sites.borocd
);
Assuming you have separate columns for type1 and type2.

Adding Columns heading to report if no data in a given period

I have a dataset for each record it has a CompanyID, RevenueMonth, RevenueYear, Revenue
When I create the report, I am grouping each CompanyID and showing their monthly revenue for a given year.
But in a given year, not all companies have any revenues for a particular month.
Example:
A sample record would look like:
CompanyID, RevenueMonth, RevenueYear, Revenue
1,05,2013,5.00
1,08,2013,6.00
1,03,2013,3.00
End Result, I would like my report to look like this with CompanyID 1.
Company ID|01|02|03|04|05|06|07|08|09|10|11|12
1 0.00|0.00|3.00|0.00|5.00|0.00|0.00|6.00|0.00|0.00|0.00|0.00
In my current Report, it will only fill column headings with March (03), May (05) and August (08).
Company ID|03|05|08
1 3.00|5.00|6.00
How do I get my Report to add the missing months for the year?
I hope my questions is clear.
Database level
Since you're only returning a year at a time, you can create a calendar table and add this to your result set:
Keeping it as simple as possible, with the date table coming from a CTE:
with months as -- get required year/months
(
select RevenueYear = 2013
, RevenueMonth = 1
union all
select RevenueYear = 2013
, RevenueMonth = RevenueMonth + 1
from months
where RevenueMonth < 12
)
select CompanyID = coalesce(r.CompanyID, c.companyID)
, RevenueMonth = coalesce(r.RevenueMonth, m.RevenueMonth)
, RevenueYear = coalesce(r.RevenueYear, m.RevenueYear)
, Revenue = isnull(r.Revenue, 0.0)
from months m
cross join (select distinct CompanyID from records) c -- make sure all companies included
left join records r on m.RevenueYear = r.RevenueYear
and m.RevenueMonth = r.RevenueMonth
SQL Fiddle with demo.
This will return a year/month for each company in the result set.
In the long run it would be better to move from a CTE to a permanent calendar table in the database.
You can then implement this in the report using a matrix style tablix.
Report level
If you'd prefer to do this at the report level, you can set up a table-style tablix with 12 permanent columns, one for each month, then populate the month revenue cells with expressions like:
=Sum(IIf(Fields!RevenueMonth.Value = 2, Fields!Revenue.Value, Nothing)
For the February column.
This would work with your existing dataset without any database code changes.

Crystal Report - Conditional selection of record

I'm using Crystal Reports XI.
My data is coming from a dataset.
I have 2 tables which are related as follows:
Product(ProdID(PK), ProdName)
ProdPC(ProdPCID(PK), ProdID(FK), PCType, ProdCode)
The 'PCType' field determins the type of barcode format the 'ProdCode' represents. (E.g. EAN13, EAN8).
In crystal reports one 'Product' can have more than one kind of barcode which means my tables end up looking like this:
Product: ("1", "Example Product 1")
ProdPC: ("0", "1", "EAN8", "01234567"), ("1", "1", "EAN13", "012345678910")
In crystal reports I only want to print 1 barcode label for per product. However because they're are 2 records in the 'ProdPC' table, I will get 2 labels being printed for this 1 product.
What I want to do is place a condition in crystal reports which states, "If EAN13 is NULL then display EAN8, ELSE display EAN13"
I do not have access to the dataset and cannot prevent the application which calls Crystal Reports to create the barcode labels from sending more than 1 record for the 'ProdPC' table.
How can I create my conditional statement, purely in 'Crystal Reports 2008'?
What I have tried so far is:
IF {PartPC.PCType} = "EAN-13" AND {PartPC.ProdCode} <> "" THEN
{PartPC.ProdCode}
ELSE
/* DISPLAY PartPC.ProdCode containing EAN8 value */
;
But I am unsure how to then tell Crystal Reports to display the 'ProdCode' value where 'PCType' is equal to 'EAN8'
Group your report by Product ID.
Order your report by PC Type descending (within Product ID).
Include your report details in the Product ID footer (not the details section).
Option I: create two columns: one for each bar code. Do this in Crystal Reports.
Remove the PartPC table, replacing it with two SQL Expression fields:
// {%EAN-8}
(
SELECT ProdCode
FROM PartPC
WHERE ProdID=Product.ProdID
AND PCType='EAN-8'
)
// {%EAN-13}
(
SELECT ProdCode
FROM PartPC
WHERE ProdID=Product.ProdID
AND PCType='EAN-13'
)
Then create a formula field to display the appropriate one:
// {#barcode}
If Not(Isnull({%EAN-13})) Then
{%EAN-13}
Else
{%EAN-8}
Option II: alter the SQL to create to scalar-valued fields. Do this in your dataset or in a Command object (Crystal Reports).
SELECT Product.*,
(
SELECT ProdCode
FROM PartPC
WHERE ProdID=Product.ProdID
AND PCType='EAN-8'
) EAN_8,
(
SELECT ProdCode
FROM PartPC
WHERE ProdID=Product.ProdID
AND PCType='EAN-13'
) EAN_13
FROM Product
...