qlik sense - KPI to table measure issue using rank - qlikview

I have the following in a kpi
Sum({$<RankNo =
{"=rank(Only({$<OrderWeekFlag = {1}, [DeliveryDate] =
{'>=$(=vMinDeliveryWeek)'}>
+
<OrderWeekFlag = {1}>}RankNo))
<=
$(=sum({$< [DeliveryDate]= {'>=$(=vMinDeliveryWeek)'} , OrderWeekFlag = {1} >}[HasOrder]))
"},OrderWeekFlag = {1} >}
[HasOrder])
I need to use this now in a table as a measure with the dimension 'Department', but the numbers are not working because of the rank within the expression. I have tried to use "aggr", "aggr with total" in different places within the expression with no joy.
 
I get different numbers when used in the table for each department, but when I click on an individual department (e.g 'Kitchen') it drills down to the correct number. please help / any ideas?

Related

Crystal Reports Summary field with sum of amounts from SQL command

I have a sub-report that returns the following....
SELECT
SUM (SorDetail.MPrice) OVER (PARTITION BY SorDetail.SalesOrder, [CusSorDetailMerch+].InvoiceGrouping) AS GroupTotal,
SorDetail.SalesOrder,
SorDetail.SalesOrderLine,
SorDetail.MStockCode,
SorDetail.MStockDes,
[CusSorDetailMerch+].InvoiceGrouping,
SorDetail.MOrderQty,
SorDetail.MPrice,
[CusSorDetailMerch+].SerialNumber
FROM
SorDetail
INNER JOIN
[CusSorDetailMerch+] ON SorDetail.SalesOrder = [CusSorDetailMerch+].SalesOrder
AND SorDetail.SalesOrderLine = [CusSorDetailMerch+].SalesOrderInitLine
WHERE
(dbo.SorDetail.SalesOrder = '000000000' + '{?SalesOrder}')
AND (dbo.SorDetail.SalesOrderInitLine = '{?Line}')
ORDER BY
[CusSorDetailMerch+].InvoiceGrouping
It returns this which is what I want:
Data Returned
What I just cant seem to do id to add a column that sums the different groups and separates the groups like this:
enter image description here
I have tried everything but cannot get the sums individually

how can i combine 2 tables and including if clause into one column

I am pretty new in using SQL and trying to combine 2 tables within APEX ( interactive grid), based on the salary and rate the information to be taken from table X ( which i managed), but i need within the same SQL statement based on the information from column Type, to bring within column percentage if clauses to create the information.
and also within the same SQL statement to have the total which would be (ratehourspercentage).
but it seems i just cant manager to combine so many codes into one correctly, i have tried different ways based on what i have found examples all over, but it seems it will just not work for some reason.
select
Overtime.ID,
OVERTIME.EMPLOYEE_NUMBER,
OVERTIME.EMPLOYEE_FULL_NAME,
OVERTIME."DATE",
OVERTIME.TYPE,
OVERTIME.HOURS,
EMPLOYEES.RATE,
EMPLOYEES.SALARY,
OVERTIME.PERCENTAGE (CASE
WHEN TYPE = "On Call " THEN "70%"
WHEN TYPE = "On Call PH" THEN "100%"
ELSE "150%"
END),
(Rate*hours*percentage) as total,
OVERTIME.CREATED,
OVERTIME.CREATED_BY
from OVERTIME
LEFT OUTER JOIN EMPLOYEES
ON OVERTIME.EMPLOYEE_NUMBER = EMPLOYEES.EMPLOYEE_NUMBER
group by Employee_Number
select
Overtime.ID,
OVERTIME.EMPLOYEE_NUMBER,
OVERTIME.EMPLOYEE_FULL_NAME,
OVERTIME."DATE",
OVERTIME.TYPE,
OVERTIME.HOURS,
EMPLOYEES.RATE,
EMPLOYEES.SALARY,
OVERTIME.PERCENTAGE,
(Rate*hours*percentage) as total,
OVERTIME.CREATED,
OVERTIME.CREATED_BY
from OVERTIME
LEFT OUTER JOIN EMPLOYEES
ON OVERTIME.EMPLOYEE_NUMBER = EMPLOYEES.EMPLOYEE_NUMBER
WHERE OVERTIME.TYPE LIKE
(CASE
WHEN TYPE = "On Call " THEN "70%"
WHEN TYPE = "On Call PH" THEN "100%"
ELSE "150%"
END);
group by Employee_number
The table for Overtime has the below:
ID Number primary Key
Employee_number vchar2
employee_full_name vchar2
date date
type vchar2
hours
salary number
rate number
percentage vachar2
total number
created timestamp
created_by vchar2
for the Employees table
ID number primary key
Employee_number vchar2
employee_full_name vchar2
salary number
rate number
i need to have the percentage information to reflect within the percentage column which i presume should be with if clause
You could calculate the total within the select statement using an SQL CASE statement.
Not sure that is the correct way to calculate the Total Pay - as you will want to Add the Basic Hourly rate to the % rate, but you can change that.
select
Overtime.ID,
OVERTIME.EMPLOYEE_NUMBER,
OVERTIME.EMPLOYEE_FULL_NAME,
OVERTIME."DATE",
OVERTIME.TYPE,
OVERTIME.HOURS,
EMPLOYEES.RATE,
EMPLOYEES.SALARY
,[Percentage] = CASE WHEN [Type] ='On Call ' THEN '70%'
WHEN [Type] = 'On Call PH' THEN '100%'
ELSE '150%'
END
,Total = CASE WHEN [Type] = 'On Call ' THEN (EMPLOYEES.RATE * OVERTIME.HOURS * 0.7)
WHEN [TYPE] = 'On Call PH THEN' THEN (EMPLOYEES.RATE * OVERTIME.HOURS)
ELSE (EMPLOYEES.RATE * OVERTIME.HOURS * 1.5)
END
,OVERTIME.CREATED
,OVERTIME.CREATED_BY
from OVERTIME LEFT OUTER JOIN EMPLOYEES ON OVERTIME.EMPLOYEE_NUMBER = EMPLOYEES.EMPLOYEE_NUMBER
group by Employee_Number

Query correctly returns calculations but returns null when multiplied together in a sum

I am trying to calculate the sum of two calculations multiplied together. Independently, the {Hours} and {Rate} calculations work just fine but when I multiply them together within a sum formula, they normally return a null. The exception is that they will return 0 if the rate is 0.
If anyone has an guidance, it would be appreciated. I would also love to know what I did wrong because I'm teaching myself this.
Select
SFLTX.BF2RDAT as {Date},
SFLTX.BF2DEPT as {Dept},
sum((SFLTX.BF2QTYG / HJOBDR.EDRUNS)) as {HOURS},
(RESRE.ABLABR * ((RESRE.ABBRDP + RESRE.ABBDVP)/100)) as {RATE},
sum((SFLTX.BF2QTYG / HJOBDR.EDRUNS) * (RESRE.ABLABR * ((RESRE.ABBRDP + RESRE.ABBDVP)/100))) as {ABS}
from SFLTX
join ERW.FCALPF on SFLTX.BF2RDAT = ERW.FCALPF.FDATE
join HJOBDR on SFLTX.BF2JOBN = HJOBDR.EDJOB# and SFLTX.BF2SEQN = HJOBDR.EDSEQ#
Join RESRE on SFLTX.BF2DEPT = RESRE.ABDEPT and SFLTX.BF2RESC = RESRE.ABRESC
where SFLTX.BF2RDAT > '1/1/2019' and (right(ERW.FCALPF.FYEAR,2) = ‘19’ and SFLTX.BF2CMODE = 'R')
group by SFLTX.BF2RDAT , SFLTX.BF2DEPT, (RESRE.ABLABR * ((RESRE.ABBRDP + RESRE.ABBDVP)/100))
I attached an image of the data output.
If any of the columns involved in any of the calculations contains NULL values, then the result of the calculation will likewise be NULL. Ensure that either all columns are defined as NOT NULL, or, use a CASE statement to convert any NULL value to 0 or 0.00 (unless you are going to be dividing by that column).

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' ;