Merge 2 facts in cube? - schema

Is it possible to merge 2 facts tables to create a cube in a Mondrian schema
example the case of sales and cost ?

It is usual to have both sales and cost measures in one fact table and add them both as measures to one cube.
If you cannot have them both in the same fact table, they need to have common dimension(s), so they can be joined together in Virtual Cube (up to Mondrian 3.8). Time dimension is usual:
DHW
Table fact_sales: date_key, ..., sales
Table fact_cost: date_key, ..., cost
Table dim_date: date_key, day_of_month, month, year, ...
Mondrian OLAP schema
Dimension [Date]: table dim_date, primary key column date_key, level year year, level month ...
Cube [Sales]: table fact_sales, dimension [Date] usage date_key, measure sales
Cube [Cost]: table fact_sales, dimension [Date] usage date_key, measure cost
Virtual Cube [Sales and Cost]: virtual cube dimension [Date], virtual cube measure [Sales.sales], virtual cube measure [Cost.cost]

If you can use Mondrian 4 you can specify the exact SQL statement that should be executed to retrieve data for fact table. Therefore you can do the join on the database level, like below:
<PhysicalSchema>
<Query alias="FACT">
<ExpressionView>
<SQL dialect="generic">
select f.*, f2.measure_2 from FACT f INNER JOIN FACT2 f2 ON f.id = f2.fact_id
</SQL>
</ExpressionView>
</Query>
</PhysicalSchema>
See the full example here:
http://thejavatar.com/mondrian-4-in-pentaho-bi-server/

Related

Creating relationship between tables of different aggregation

I have a retail store data with 50000 records. One of the columns in that file is Segment (Home office, Corporate and Consumer). This data i have in one table. In another table i just have two columns - Segment, Forecast Sales. What query needs to be written to get the actual segment sales and Forecast Sales in single table or how should the relationship be created in SQL.
Data can be found here : https://community.tableau.com/docs/DOC-1236
I will assume table name [Orders] as per your excel and [Segment Forecast] for the table with columns [Segment], [Forecast Sales].
select sf.Segment,sum(o.sales) [Actual Sales],sf.[Forecast Sales]
from [Orders] o
inner join [Segment Forecast] sf on o.Segment=sf.Segment
group by sf.Segment,sf.[Forecast Sales]

Populating fact table

I've a data warehouse for sales, it has 3 dimensions [product,time,store] and a fact table [sales_fact].
Primary key of 'sales_fact' table is made up of all primary keys of dimensions table, dimension tables are all filled up manually now I want to fill 'sales_fact' table with SUM of prices of products stored in a city for a specific month or 3 month period.
How should I sum up prices from product table which are related to a specific month and add it to fact table?
Considering that sum up prices from product table which are related to a specific month
is a measure, your query can be like below :
SELECT DS.City, DT.[Month], SUM(DP.Price)FROM
SalesFact AS S
LEFT JOIN DimProduct AS DP ON DP.ProductSK=S.ProductSK
LEFT JOIN DimTime AS DT ON DT.DateSK=S.DateSK
LEFT JOIN DimStore AS DS ON DS.StoreSK=S.StoreSK
WHERE [Date condition] --Add your date conditoon
GROUP BY DS.City, DT.[Month]
You can use a view for this measure.

Create dynamic Metric, which will be based on various dimensions/ filters applied

I have a table "Trans" which contains the acccountNumbers and other dimensions like Facility , Status etc.
I need to create a calculated member in SSAS where the logic would be
Count of Accounts in a group / Total accounts.
Count of Accounts in a group would be based on the Dimension filter I provide.
For e.g. If I provide the facility then I need the Count of accounts (In numerator) group be different facilities.
Likewise If I provide the Status I would need the numerator to be grouped as per the data in Status table.
Table Name
Trans (AccountNumber, facility,Status) -- This is fact table
Dimension tables
Facility( Id, Facility_name)
Status (Id, Status)
Not sure how to go about it.
EXISTING is a useful function, so maybe something like:
COUNT(
EXISTING [AccountNumber].[AccountNumber].MEMBERS
)

SSAS best way to count Orders by number of Products ordered

I'm looking for the best way to implement measure that counts Orders by the number of products ordered.
There is a fact table of ProductsOrdered, like this
CREATE TABLE ProductsOrdered (
IdOrder UNIQUEIDENTIFIER,
IdProduct UNIQUEIDENTIFIER,
OrderDate DATE,
Quantity INT,
Price DECIMAL(18,2)
)
for every order there are rows in the table for every kind of product ordered.
I would like to have a measure that counts how many ordered a single product, how many 2, and so on.
Just like this SQL query does.
SELECT OrderedProductNumber, COUNT(IdOrder) AS NumberOfOrders
FROM (
SELECT IdOrder, COUNT(IdProduct) AS OrderedProductNumber
FROM ProductsOrdered
GROUP BY IdOrder
) t
GROUP BY OrderedProductNumber
Which MDX expression is best counterpart?
Thanks.
The problem you have is that you can only produce a count by a dimension. In this case, your dimension is the products ordered (1, 2, 3, etc.) Your option is to create a physical dimension that has the range of possibilities (tedious), or to create "dummy" members for a dimension to do the same thing as a calculation (tedious AND likely a performance problem). I would lean towards creating a dimension for products ordered, and then create a fact table using your SQL query and add the dimension and measure group to the cube. It's going to be easier to implement in the long run and by physically instantiating the counts, you will get much better query performance.
You can define dimension based on product
then measure group based on fact table, that you named ProductOrdered,
then you can define distinct count measure based on your IOrder column (e.g [IOrderdistinctcount])
after that you can use
select
{IOrderdstinccount} on 0,
[dim Product].[product].[Product].members on 1
from [Cube]

MDX equivalent to SQL's GROUP BY for excluding duplicate rows

My cube has a fact table that contains a row for every modification made to any entity. What I need is a measure that will return a count of the entities based on the selected dimensions.
So if the user selects a month from the date dimension then the measure should return the number of entities that were modified that month (rather than the number of modifications).
In SQL this would be something like:
SELECT EntityID, COUNT(*)
FROM FactTable
WHERE Date BETWEEN X AND Y
GROUP BY EntityID
How can you do this in MDX? Surely this would be an extremely common scenario with cubes.
Your t-sql query is equivalent of mdx query:
select
[Measures].[<Fact rows count measure>] on columns,
<Entity dimension members> on rows
from [<Cube Name>]
where (<month member>)
In the above query [Fact rows count measure] would be a measure with aggregation formula Count - count of rows
However, if you need to return the distinct count of entity members when you slice by another dimension, you basically have several options:
create a distinct count measure on the entityID key
create a calculated measure with expression: count(exists(existing [Entity].[Entity].[Entity].MEMBERS,,'Measure Group Name'))
HTH,
Hrvoje Piasevoli