Aggregating results for all versions of a slowly changing dimension - sql-server-2005

I am looking to aggregate values for all versions of a slowly changing dimension. Here is a simplified explanation of the problem.
I have a Product dimension and this dimension has an attribute called ProductGroup.
Products from time to time change ProductGroups.
The Product dimension is a slowly changing dimension, and the change being when it moves between ProductGroups. The Product dimension has a ProductKey and a ProductBusinessKey.
The ProductKey is the unique key within the dimension and ProductBusinessKey is the key from main booking system.
The ProductBusinessKey(PBK) is unique to a product and will never change.
A simplified structure of the dimension and fact tables are
I've used the following abbreviations
PK - ProductKey
PBK - ProductBusinessKey
PG - ProductGroup
Dimension Table
PK PBK Name PG
1 1 Prod1 ProductGroup1
2 1 Prod1 ProductGroup2
3 2 Prod2 ProductGroup1
Fact Table
PK Revenue
1 100
2 100
3 100
I want to generate a report that will display total revenue for all version of the product dimension
i.e.
Prod1 Prod2
200 100
At the the moment he mdx I am using is
SELECT
[ProductDimension].[Product].Children On Columns
FROM
TheCube
WHERE
[Measures].[Revenue]
And this is generating the following results
Prod1 Prod1 Prod2
100 100 100
I was wondering how I could structure a query to return the results as
Prod1 Prod2
200 100
Any help is much appreciated.

The easiest way would be to create an attribute that has a key of either the ProductBusinessKey, then use that in your query.

If you use the SUM(..) on the [Revenue] measure over time from your time dimension it will show the desired results.

Related

SQL - Managing wide and long format

I am trying to achieve the following challenge but I am not sure how to tackle it.
I have a table called "salesman_data" with 2 columns. Each columns represents a salesman (e.g. salesman_id_1 and salesman_id_2). Each column contains data about products which have been sold by the corresponding salesman. The data stored is the product ID. For instance, the salesman salesman_id_1 sold products whose ID are 123, 456, 789, etc. The same applies for salesman_id_2. The table is the following:
Table salesman_data
salesman_id_1
salesman_id_2
123
321
456
654
789
987
...
...
I have to calculate the sales ratio. The ratio is defined as (nb of product sold by the salesman) / (total nb product sold by everyone). The required output for the answer is each salesman with their sales ratio. Then ordering data by salesman id. In my understanding, the output should look like this:
salesman_id
ratio
1
30.1
2
69.9
Since the original table does not allow me to sort data by salesman id, I would need to create an entire new table beforehand. I was thinking about doing this:
Create a new table
Calculate the ratio
Insert the ratio into the newly created table
Query the data from the new table
I do not know if my process is the most efficient. What do you guys think ?

sum not calculating correct no. of units in SQL command

I have the following SQL script(of which the result is displayed under the script). The issue I am having is that I need to add up the quantity on the invoice. The quantity works fine when all the products on the invoice are different. When there is a product that appears twice on the invoice, the result is incorrect. Any help appreciated.
The DISTINCT keyword acts on all columns you select.
A new product introduces a difference which makes it no longer distinct. Hence the extra row(s).
Where you had:
Order Product Total
1 Toaster $10
2 Chair $20
And another item is added to order 1:
Order Product Total
1 Toaster $99
1 Balloon $99 -- Yes that's a $89 balloon!
2 Chair $20
The new row (balloon) is distinct and isn't reduced into the previous row (toaster).
To make is distinct again, don't select the product name:
Order Total
1 $99
2 $20
Uniqueness kicks in and everyone's happy!
If you can remove the column from the select list that's "different", you should get the results you need.

powerpivot average of an aggregate

I may be missing an obvious solution but I am looking for a way to take the average of a summed value. For example I have profit at an item# level where each item is on a bill as well and I want average of the bills profit.
Item# | Bill# | Profit
1 1 100
2 1 200
1 2 100
2 2 200
If I just take the avg of profit I get 150 but I want the avg of the bill total which would be 300. Is it possible to do this? I was thinking something like Calculate(Average(Profit),Bill# = Bill#) but that is always true?
Thanks in advance!
It's not totally clear how you intend to use your measure but there are some powerful iterative functions in PowerPivot that do this kind of thing. This formula iterates over each bill# and averages the sum of the profit:
= AVERAGEX(VALUES(tbl[bill#]), SUM(tbl[profit]))
The first argument simply creates a 'column' of the unique bill#s and the second is the summing the profit per bill#.
assuming your table is called tbl

Create an Access expression using multi-value Count() data

The data I have is in a few tables. Table 1 has:
[invoice] (key),
[invoice total], and
[associated assets] (multi-value linked to [assets] in table 2).
Table two includes:
[Assets] (key), and
other asset data fields.
What I need to do is take the invoice total and divide it by the number of assets that go into that price to determine the cost of each asset. I have used the count() function in a query to get the number of assets for each invoice, but I can't seem to use the data anywhere else.
I have created another query to use the expression
= [invoice total]/count([associated assets]
In queries, I keep getting an aggregate error and can't seem to make this work. I would like for the expression result to populate a field in table 1. I need help either setting up a working query to pull the data from, or a form item that will pull the data and auto-populate the field.
For a table named [Invoices]
invoice invoice total associated assets
------- ------------- -----------------
1 $3.00 bicycles
2 $5.00 bicycles, ham
3 $1.00
where [associated assets] is a multi-value lookup field against the table [Assets]
AssetID AssetName
------- ---------
1 bicycles
2 ham
The following query
SELECT
i.invoice,
i.inv_tot AS [invoice total],
i.assetCount,
IIf(i.assetCount=0, NULL, i.[invoice total]/i.assetCount) AS avgCostPerAsset
FROM
(
SELECT
Invoices.invoice,
Min(Invoices.[invoice total]) AS inv_tot,
Count(Invoices.[associated assets].Value) AS assetCount
FROM Invoices
GROUP BY Invoices.invoice
) i
produces the following result
invoice invoice total assetCount avgCostPerAsset
------- ------------- ---------- ---------------
1 $3.00 1 3
2 $5.00 2 2.5
3 $1.00 0

Grouping dimension members using mdx query on the fly with aggregation

I am new to MDX and I just want to ask if it is possible in MDX query to make aggregations and groupings on the fly.
Here is the scenario, I have a dimension called "Department". And it has department code values e.g.
1234
1257
1346
1390
I also had a measure called "Sales".
What I need to do here is to make a Calculated Member that will get the Maximum "Sales" grouped per department based on the its first two digits. For example, consider the following output when browsing the cube using the Department dimension and Sales Measure
Department | Sales
1234 | 100
1257 | 200
1346 | 100
1390 | 400
Then I need to make an MDX query to produce an output something like below,
Department | Sales
12xx | 200
13xx | 400
You will notice that Maximum Sales based on the two digits of each department concatenated with "xx" string were the expected output.
Well determining the maximum is not a problem. with <name> as max(<something>) but you should reconsider the approach with the on the fly grouping.
I'm sure, that it is achievable, although I cannot provide a solution, but it will perform poorly. (I'm assuming that the digits of the department are not implemented as measure)
If you need this grouping more often you should add an additional dimension, or better a hierarchy to the department dimension.