Calculation Script for SSAS project - ssas

I need to write a script in my SSAS project (cube calculation) that calculate Opening and closing stock value on specific date.
In this screen short first table showing my raw data and second table is showing my required BI values .
Opening stk (stock) on 2015-02-01 product A was 18 and that day price was 10 so 10*18 = 180 stk value,
on 2015-02-08 price was 9 for same product and closing stk was 10 (so 9*10=90) is a closing stock value.
I have use below code for this but this script is not showing required results.
Scope([Measures].[Closing Stock]);
This = sum((
null:[Time].[Year].CurrentMember *
null:[Time].[Quarter].CurrentMember *
null:[Time].[Month Name].CurrentMember *
null:[Time].[Month].CurrentMember *
null:[Time].[date].CurrentMember )
,[Measures].[Movement Qty]) * Price;
FORMAT_STRING ( This ) = "#,#";
End Scope;

For what I understand you want to display the remaining stock and the remaining stock value for each day. The script below will work only at day levels. I am have assumed that you have the following measurse in your cube [Meausres].[Opening],[Measures].[Qty],[Measures].[Price] and you have [date] dimension and a level named [date] in this dimension
with
member measure.ClosingCount
as
sum([date].[date].currentmember, [Meausres].[Opening]-[Measures].[Qty])
member measure.ClosingAmount
as
sum([date].[date].currentmember, [measure].[ClosingCount]*[Measures].[Price] )
Edit based on the comment
with
member measure.ClosingCount
as
sum(([date].[date].currentmember,[Product].[Product].currentmember), [Meausres].[Opening]-[Measures].[Qty])
member measure.ClosingAmount
as
sum(([date].[date].currentmember,[Product].[Product].currentmember), [measure].[ClosingCount]*[Measures].[Price] )

Related

Access SQL query - Percentage of Total calculation

In Access SQL, I am attempting what should seem like a simple task in attaining a percentage of total. There are 3 item stores (Sears, kmart & Mktpl) of which in any given week, I wish to calculate their respective percent of total based on balance of sales (all can be obtained using one table - tbl_BUChannelReporting).
For example week 5 dummy numbers - Sears 7000, kmart 2500, mktpl 2000
the following ratios would be returned: sears 61%, kmart 22%, mktpl 17%
I was originally trying to create a sub query and wasn't getting anywhere so I am essentially trying to sum sales on one of the item stores in week 5 divided by the sum of all 3 item store sales in week 5. The following is my query, which is giving me "cannot have aggregate function in expression" error:
SELECT FY, FW, Rept_Chnl, BU_NM, Order_Store, Item_Store, CDBL(
SUM(IIF([item_store]="sears", revenue, IIF([item_store]="kmart", revenue, IIF([item_store]="mktpl", revenue,0)))) /
(SUM(IIF([item_store]="sears",revenue,0)+SUM(IIF([item_store]="kmart",revenue,0)+SUM(IIF([item_store]="mktpl",revenue,0))))))
AS Ratios
FROM tbl_BUChannelReporting
WHERE FY = "2017"
AND FW = 5
GROUP BY FY, FW, Rept_Chnl, BU_NM, Order_Store, item_store
Thanks all in advance for taking the time. This is my 1st post here and I don't consider myself anything but a newbie anxious to learn from the best and see how this turns out.
Take care!
-D
Consider using two derived tables or saved aggregate queries: one that groups on Item_Store and the other that does not include Item_Store in order to sum the total stores' revenue. All other groupings (FY, FW, Rept_Chnl, BU_NM, Order_Store) remain in both and used to join the two. Then in outer query, calculate percentage ratio.
SELECT i.*, CDbl(i.Store_Revenue / a.Store_Revenue) As Ratios
FROM
(SELECT t.FY, t.FW, t.Rept_Chnl, t.BU_NM, t.Order_Store, t.Item_Store,
SUM(t.Revenue) As Store_Revenue
FROM tbl_BUChannelReporting t
WHERE t.FY = '2017' AND t.FW = 5
GROUP BY t.FY, t.FW, t.Rept_Chnl, t.BU_NM, t.Order_Store, t.Item_Store) As i
INNER JOIN
(SELECT t.FY, t.FW, t.Rept_Chnl, t.BU_NM, t.Order_Store
SUM(t.Revenue) As Store_Revenue
FROM tbl_BUChannelReporting t
WHERE t.FY = '2017' AND t.FW = 5
GROUP BY t.FY, t.FW, t.Rept_Chnl, t.BU_NM, t.Order_Store) As a
ON i.FY = a.FY AND i.FW = a.FW AND i.Rept_Chnl = a.Rept_Chnl
AND i.BU_NM = a.BU_NM AND i.Order_Store = a.Order_Store
Or save each above SELECT statement as its own query and reference both below:
SELECT i.*, (i.Store_Revenue / a.Store_Revenue) As Ratios
FROM
Indiv_Item_StoreAggQ As i
INNER JOIN
All_Item_StoreAggQ As a
ON i.FY = a.FY AND i.FW = a.FW AND i.Rept_Chnl = a.Rept_Chnl
AND i.BU_NM = a.BU_NM AND i.Order_Store = a.Order_Store

Where Clause using another Dimension Value

I'm trying to create a Calculated Member in my cube with where clause but couldn't figure how to achieve the proper result.
I created a calculated member "Outlook" using the below code to display only Forecast values.
CREATE MEMBER CURRENTCUBE.[Measures].[Outlook]
AS SUM(([Source Profile].[Source Profile Hierarchy].CurrentMember,
[Source Profile].[Profile Level01].&[Outlook]),
[Measures].[USD Amount]),
FORMAT_STRING = "#,##0;(#,##0)",
VISIBLE = 1 , DISPLAY_FOLDER = 'USD' , ASSOCIATED_MEASURE_GROUP = 'CARS';
Cube Result
Now I would like to filter the results dynamically based on another hidden dimension "Current_Month". This dimension always has current financial period value and it's corresponding outlook profile
Year_Month Outlook_Profile
2015010 10 + 2
Expected result should be "Outlook" measure showing value based on Current_Month dimension, which is '10 + 2' and rest of them should be 0
Expected result
Just to explain the requirement in SQL terms, I would like to achieve the below in MDX
Where Fact.Source_Profile=Dimension.Source_Profile
instead of
Where Fact.Source_Profile='10 + 2'
I'm not sure how to achieve this in Where Clause or by another means. I could see examples of hard coding values, like year.&[2015] but haven't seen one using dynamic values.
I found a solution myself and thought of sharing the same. Used StrToMember function to pass hidden dimension's member as a variable here.
CREATE MEMBER CURRENTCUBE.[Measures].[Outlook]
AS (
Sum
(
(
[Source Profile].[Source Profile Hierarchy].CurrentMember,
strtomember("[Source Profile].[Source Name].&[" + [Outlook Profile].[Outlook Profile].&[1].Member_Caption + "]")
)
,[Measures].[USD Amount]
)
),
FORMAT_STRING = "#,##0;(#,##0)",
VISIBLE = 1 , DISPLAY_FOLDER = 'USD' , ASSOCIATED_MEASURE_GROUP = 'CARS' ;

Firebird SQL. How to allow for BOM components in SOH when calculating production requirements

I am trying to write a query to find the purchasing/manufacturing requirements, based on our sales orders and taking into account our stock on hand and work in progress.
I am using a Firebird 2.5 server
My approach has been to begin with the sales orders, then
use a recursive query to identify all the items that are required.
Then to subtract those requirements from the work in progress and stock on hand to give available and free quantities.
The first part works well, but the second part fails to take into account the child components present in the stock on hand.
For example Item A includes 3 x Item B
I have sales for 5 x Item A and 2 x Item B
The query will correctly tell me that I need 5 X Item A and 17 x Item B
But if I have 2 x Item A in stock It will tell me I need 3 x Item A and 17 x Item B , not allowing for the 6 x Item B already present in the 2 x Item A
If I try to subtract the Stock On Hand, inside the recursive part of the query, it may subtract the same items several times.
I think I somehow need to use two recursive queries, but I'm not sure how.
WITH RECURSIVE RPL (ITEMCODE, LINECODE, ITEMNAME, PERQTY) AS
(
SELECT MAX(SALESHEADER.ORDERNUMBER), SALESLINES.LINECODE, MAX(SALESLINES.LINEDESCRIPTION), SUM(SALESLINES.ORDERQTY)
FROM SALESHEADER, SALESLINES
WHERE ORDERSTATUS = 'Open'
AND ORDERDESCRIPTION NOT SIMILAR TO '%F/C%'
AND SALESLINES.ORDERNUMBER = SALESHEADER.ORDERNUMBER
GROUP BY SALESLINES.LINECODE
UNION ALL
SELECT CHILD.ITEMCODE, CHILD.LINECODE, CHILD.LINEDESCRIPTION, CHILD.PERQTY*PARENT.PERQTY
FROM RPL PARENT, BOMLINES CHILD
WHERE PARENT.LINECODE = CHILD.ITEMCODE
AND CHILD.CODETYPE = 'Item Code'
)
SELECT RPL.LINECODE, MAX(RPL.ITEMNAME) AS DESCRIPTION , MAX(ITEMMASTER.ONHANDQTY) AS ONHAND, MAX(ITEMMASTER.SUPPLYQTY) AS SUPPLY,
SUM(RPL.PERQTY) AS DEMAND,
(MAX(ITEMMASTER.SUPPLYQTY) + MAX(ITEMMASTER.ONHANDQTY)) - SUM(RPL.PERQTY) AS AVAILABLE,
MAX(ITEMMASTER.ONHANDQTY) - SUM(RPL.PERQTY) AS FREE
FROM RPL JOIN ITEMMASTER ON ITEMMASTER.ITEMCODE = RPL.LINECODE
GROUP BY RPL.LINECODE;
Thanks in advance for any assistance.

How to using subquery values in arithmetic operations

My manager wants to see what is total values of our suppliers shipments, and what is the total values of their invoices recorded. So he can see the differencies and want from suppliers to unsended invoices.
Here is my code.
It is working on accounting table and shipment detail table.
fis_meblag0 is always little from zero because it is 320 account so I mutiply it -1 for get real value.
sth_tutar is value of shipment, sth_vergi is VAT and so the sum of them is equal to sum of total with VAT.
Now the hard part.
Manager wants to diference between them in a other column and also sort the valuez z to a.
I know I can reuse same subselect for the getting totals but I want to know that can I achieve this without using the same subquery again.
I mean in first subselect I have the total, in last column I only need just the total, can I use total without compute it again?
Regards
select
ch.cari_kod as Carikod,
ch.cari_unvan1 as Unvani,
(select (sum(mf.fis_meblag0) * -1)
from dbo.MUHASEBE_FISLERI mf
where (mf.fis_tarih > '20141231' and mf.fis_tarih < '20150201')
and mf.fis_hesap_kod = ch.cari_kod
and mf.fis_meblag0 < 0) as mtoplam,
(Select sum (sth.sth_tutar + sth.sth_vergi)
from dbo.STOK_HAREKETLERI sth
where (sth.sth_tarih > '20141231' and sth.sth_tarih < '20150201')
and sth.sth_cari_kodu = ch.cari_kod
and sth.sth_normal_iade = 0
and sth.sth_tip = 0) as stoplam
from
dbo.CARI_HESAPLAR ch
where
ch.cari_kod like '320%'
try this query:
select Carikod, Unvani, mtoplam, stoplam, mtoplam - stoplam as Grand_total
from
(
-- your full query here
) T

Tough SQL problem for an amateur like me

My family has several stock accounts, and I keep a table of stock values for their contents and enter current values daily. Fields are...
ACCOUNT / TICKER / QUANTITY / CLOSINGDATE/ CLOSING (current price)
To get a report of the current stock contents and most recent price, I am using this code. I add this up row by row, and get an account total (for account # 2, for example ). The table is designed this way also so I can track individual stock performance.
SELECT distinct ticker, account, closingdate, quantity, closing, (quantity*closing) as "Net"
FROM "stock values" AS S
WHERE
(account=2) and
(quantity>0.000001) and
(closingdate =(SELECT MAX(closingdate) FROM "stock values" WHERE ( ticker = S.ticker) and (account=s.account)) )
But now what I would like to do is create a report that looks like this on a TdbGrid so that... i get a report, grouped by account with the most recent distinct tickers added up FOR THAT ACCOUNT. Four days later, I cannot crack this.
Account Value
1 35,000.00
2 122,132,32
3 43.23
I'm using Nexus 3 on Delphi 7.
Any help, greatly GREATLY appreciated.
Larry
To me, it looks like you just have to add a group by:
select account
, sum(quantity*closing)
from "stock_values" as S
where s.closingdate =
(
select max(closingdate)
from "stock values" as S2
where S2.ticker = S.ticker
and S2.account = S1.account
)
group by
account
Since you can have the same stock across multiple accounts, I would pre-query the last closing date per stock per account... This way, if you have stock in company "X" and entered its daily balance for Feb 4th to Account #1, but forgot to enter the "X" price for Account #5, you would have each account with different "Last Date Entered" for a particular stock. It would actually be better to have two tables... one with nothing but your stocks, closing date and closing price. Then join that to the accounts that use those stocks... However, this query should get you a DETAILED breakdown per account showing all the maximum respective stock dates... The next query will be simplified for totals per account... This one is primarily for a sanity check to ensure you're getting what you expect. This does ALL the accounts, but you can adjust your WHERE clause to get only ONE specific account if you so need to.
SELECT
ticker,
account,
closingdate,
quantity,
closing,
(quantity*closing) as "Net"
FROM
"stock values" AS S,
( select account,
ticker,
max(closingdate) LastDatePerStock
from
"stock values" sMax
group by
account, ticker ) TickerDate
WHERE
s.account = TickerDate.account
and s.ticker = TickerDate.ticker
and s.closingdate = TickerDate.LastDatePerStock
and (quantity>0.000001)
order by
account,
ticker
Now, the query simplified with just the account and the closing balance for all their stocks without seeing exactly what stocks are in the portfolio.
SELECT
account,
max( closingdate ) LatestClosingDate,
sum((quantity*closing)) as "Net"
FROM
"stock values" AS S,
( select account,
ticker,
max(closingdate) LastDatePerStock
from
"stock values" sMax
group by
account, ticker ) TickerDate
WHERE
s.account = TickerDate.account
and s.ticker = TickerDate.ticker
and s.closingdate = TickerDate.LastDatePerStock
and (quantity>0.000001)
group by
account