calculated measure and filter by two different dimensions - filtering

this works
WITH MEMBER [Measures].[Pre] AS
SUM
(
-{
[Dim Event].[Ticket].&[Open]
},
(
[Measures].[Event Incidents Count]
)
)
Select [Measures].[Pre] on 0
From Event
However, adding another dimension like this
WITH MEMBER [Measures].[Pre] AS
SUM
(
-{
[Dim Event].[Ticket].&[Open]
,[Dim Category].[Status].&[OPEN]
},
(
[Measures].[Event Incidents Count]
)
)
Select [Measures].[Pre] on 0
From Event
caused this error,
Executing the query ...
Members, tuples or sets must use the same hierarchies in the function.
Execution complete
how can I rewrite the second query to have two dimensions.
BTW, it's for a cube Calculation,so I'm only going to use something like this
SUM
(
-{
[Dim Event].[Ticket].&[Open]
,[Dim Category].[Status].&[OPEN]
},
(
[Measures].[Event Incidents Count]
)
)
Update: Solution
WITH MEMBER [Measures].[Pre] AS
SUM
(
CROSSJOIN(
- {
[Dim Event].[Ticket].&[Open] }
, - { [Dim Category].[Status].&[OPEN] }
)
,
[Measures].[Event Incidents Count]
)

Try this, might work..
SUM
(
-{
[Dim Event].[Ticket].&[Open]
},
{
[Dim Category].[Status].&[OPEN]
},
(
[Measures].[Event Incidents Count]
)
)

WITH MEMBER [Measures].[Pre] AS
SUM
([Dim Event].[Ticket].&[Open] * [Dim Category].[Status].&[OPEN],
([Measures].[Event Incidents Count])
)
Select [Measures].[Pre] on 0
From Event
Hop this help !

Related

Qgis: calculating distances for different types of lines for a report table

I have a Qgis project with lines classified in a field in 5 types. Composing a report, I need a table that shows the five types of line in the first column and in the second column the length for each type of line. No idea how to do this.
Any help, please?
Solved.
CASE
WHEN "Infra_class" = 0 THEN round ( sum ( "length","Infra_class" is '0') )
WHEN "Infra_class" = 1 THEN round ( sum ( "length","Infra_class" is '1') )
WHEN "Infra_class" = 2 THEN round ( sum ( "length","Infra_class" is '2') )
WHEN "Infra_class" = 3 THEN round ( sum ( "length","Infra_class" is '3') )
ELSE round ( sum ( "length","Infra_class" is '4') )
END

Convert Teradata-sql-query to tableau new custom query

I have a Teradata-SQL-Query that auto-created through FinBI SAP tool. I am trying to use that query in Tableau as a New Custom SQL. Due to differences in the synax I am getting an error.
Below is the query that I pulled from FinBI SAP Tool.
SELECT
ABC.PRODUCT_ID,
sum(CASE WHEN DEF.SERVICE_FLG = 'N' THEN DEF.COMP_US_NET_PRICE_AMT ELSE 0 END),
Sum(CASE WHEN DEF.SERVICE_FLG = 'N' THEN DEF.COMP_US_LIST_PRICE_AMT ELSE 0 END),
Sum(CASE WHEN DEF.SERVICE_FLG = 'N' THEN DEF.COMP_US_COST_AMT ELSE 0 END),
Sum(CASE WHEN DEF.SERVICE_FLG = 'N' THEN DEF.EXTENDED_QTY ELSE 0 END)
,
GHI.FISCAL_YEAR_NUMBER_INT,
GHI.JKL,
MNO.GU_PRIMARY_NAME
FROM
ABC,
DEF,
GHI,
MNO
WHERE
( DEF.FISCAL_YEAR_QUARTER_NUMBER_INT=GHI.FISCAL_YEAR_QUARTER_NUMBER_INT )
AND ( ABC.ITEM_KEY=DEF.PRODUCT_KEY )
AND ( DEF.END_CUSTOMER_KEY=MNO.END_CUSTOMER_KEY )
AND ( DEF.PRODUCT_KEY IN ( SELECT ITEM_KEY FROM ABC H JOIN PQR S ON H.TECHNOLOGY_GROUP_ID = S.TECHNOLOGY_GROUP_ID WHERE user_id=#Variable('BOUSER') AND IAM_LEVEL_NUM=1 ) )
AND ( DEF.DV_ATTRIBUTION_CD IN ('ATTRIBUTED','STANDALONE') )
AND
(
ABC.BUSINESS_UNIT_ID IN ( 'xyz' )
AND
DEF.REVENUE_RECOGNITION_FLG IN ( 'Y' )
)
GROUP BY
1,
6,
7,
8
enter code here

DAX closest value match with no relationship

I'm trying to migrate a report from Excel into Power BI and I'm hoping someone can help me as I'm new to DAX.
I have two tables and one (let's call it table A) contains a column of planned start Date/Times for events while the other contains the actual start Date/Times of the same events. There is usually only a few minutes difference between the planned and actual start times.
I need to match the closest actual start Date/Time from Table B to the planned start Date/Times in table A.
There are no existing columns that I can use to create a relationship between the two tables.
If I can find the closest actual start time and pull it into Table A then I can create a relationship from that.
In Excel I would do this with an array formula such as this: (here I'm just assuming everything is in column A of each table)
{=Index(TableB!A:A,match(min(abs(TableB!A:A-TableA!A1)),abs(TableB!:A:A-TableA!A1),0),1)}
I have found the following DAX code online but it will only return the next lowest value even if there is a closer value which is higher.
If (
Hasonevalue ( TableA[A] ),
Calculate (
Max ( TableB[A] ),
Filter ( TableB, TableB[A] <= Values ( TableA[A] ) )
)
)
I've also tried to figure out a way to do this if I build a date/time table which contains every minute of the date range that my data covers (about 2 years) at but as I said I'm new to DAX and haven't been able to figure it out.
Is there any way to use something similar to the (min(abs( part of the excel formula in DAX (as it has these functions) to calculate this in a calculated column? Is this possible without an existing relationship or will I have to continue to do this part of the work in Excel every time I want to update this report?
Any help greatly appreciated.
Create a calculated column in the Planned table, call it ActualClosestDate and use this expression:
ActualClosestDate =
IF (
DATEDIFF (
CALCULATE (
MAX ( TableB[Actual] ),
FILTER ( TableB, [Planned] >= [Actual] && TableA[Event] = TableB[Event] )
),
[Planned],
SECOND
)
< DATEDIFF (
[Planned],
CALCULATE (
MIN ( TableB[Actual] ),
FILTER ( TableB, [Planned] <= [Actual] && TableA[Event] = TableB[Event] )
),
SECOND
),
CALCULATE (
MAX ( TableB[Actual] ),
FILTER ( TableB, [Planned] >= [Actual] && TableA[Event] = TableB[Event] )
),
CALCULATE (
MIN ( TableB[Actual] ),
FILTER ( TableB, [Planned] <= [Actual] && TableA[Event] = TableB[Event] )
)
)
Where:
[Planned] is the Planned Start Date/time column in TableA
[Actual] is the Actual Start Date/Time column in TableB
Replace according to your model.
If you don't have a Event column in each table supress that condition in the filters functions.
UPDATE: Calculating three different columns could improve performance instead of perform the calculation in one expression.
BeforePlanned =
DATEDIFF (
CALCULATE (
MAX ( TableB[Actual] ),
FILTER ( TableB, [Planned] >= [Actual] && TableA[Event] = TableB[Event] )
),
[Planned],
SECOND
)
AfterPlanned =
DATEDIFF (
[Planned],
CALCULATE (
MIN ( TableB[Actual] ),
FILTER ( TableB, [Planned] <= [Actual] && TableA[Event] = TableB[Event] )
),
SECOND
)
ActualClosestDate =
IF (
[BeforePlanned] < [AfterPlanned],
CALCULATE (
MAX ( TableB[Actual] ),
FILTER ( TableB, [Planned] >= [Actual] && TableA[Event] = TableB[Event] )
),
CALCULATE (
MIN ( TableB[Actual] ),
FILTER ( TableB, [Planned] <= [Actual] && TableA[Event] = TableB[Event] )
)
)
You could even split it in more columns, i.e. a column to get the previous actual date and a column to get the next actual date then you just need:
ActualClosestDate =
IF ( [BeforePlanned] < [AfterPlanned], [PreviousActualDate], [NextActualDate] )
Let me know if this helps.

Rank result set according to condition

I have a table which has 3 columns: Product, Date, Status
I want to rank in this manner:
for each product order by Date, and Rank if Status = FALSE then 0, if it's TRUE then start ranking by 1, continue ranking by the same value if previous Status is TRUE.
In this ordered set if FALSE comes assign to it 0, and for the next coming TRUE status for same product assign x+1 (x here is previous rank value for status TRUE).
I hope picture makes it more clear
This code uses SS2008R2 features which do not include LEAD/LAG. A better solution is certainly possible with more modern versions of SQL Server.
-- Sample data.
declare #Samples as Table ( Product VarChar(10), ProductDate Date,
ProductStatus Bit, DesiredRank Int );
insert into #Samples values
( 'a', '20160525', 0, 0 ), ( 'a', '20160526', 1, 1 ), ( 'a', '20160529', 1, 1 ),
( 'a', '20160601', 1, 1 ), ( 'a', '20160603', 0, 0 ), ( 'a', '20160604', 0, 0 ),
( 'a', '20160611', 1, 2 ), ( 'a', '20160612', 0, 0 ), ( 'a', '20160613', 1, 3 ),
( 'b', '20160521', 1, 1 ), ( 'b', '20160522', 0, 0 ), ( 'b', '20160525', 1, 2 );
select * from #Samples;
-- Query to rank data as requested.
with WithRN as (
select Product, ProductDate, ProductStatus, DesiredRank,
Row_Number() over ( partition by Product order by ProductDate ) as RN
from #Samples
),
RCTE as (
select *, Cast( ProductStatus as Int ) as C
from WithRN
where RN = 1
union all
select WRN.*, C + Cast( 1 - R.ProductStatus as Int ) * Cast( WRN.ProductStatus as Int )
from RCTE as R inner join
WithRN as WRN on WRN.Product = R.Product and WRN.RN = R.RN + 1 )
select Product, ProductDate, ProductStatus, DesiredRank,
C * ProductStatus as CalculatedRank
from RCTE
order by Product, ProductDate;
Note that the sample data was extracted from an image using a Mark I Eyeball. Had the OP taken heed of advice here it would have been somewhat easier.
Tip: Using column names that don't happen to match data types and keywords makes life somewhat simpler.
Try this query,
SELECT a.Product ,
a.Date ,
a.Status ,
CASE WHEN a.Status = 'FALSE' THEN 0
ELSE 1
END [Rank]
FROM ( SELECT Product ,
Date ,
Status ,
ROW_NUMBER() OVER ( PARTITION BY Product ORDER BY DATE, Status ) RNK
FROM TableProduct
) a
ORDER BY Product, a.RNK

DB2 SQL, cant define column as

I have some SQLthat is part of that is a section of a with statement
And I keep getting an error that "NEWID" is not valid in the context where it is used sqlstate 42703.
Update: The error has been comming from the group by clause using a having function I didnt put in the original code as I thought it wasnt the issue.So I updated the code to show the full version.
Does anyone know what the problem is with the statement?
HATSTABLE1 (HATId, NewID) as (
select HA.HATId as "ID",
round(
cast(
sum(
case when HA.ID = 4 or
HA.ID < 0
then 1 else 0 end
) AS FLOAT
) / count(*) * 100,
2
) AS NewID
from Hats T
join Heads HD on
T.ID=HD.HatID
group by T.ID
having NewID >1
try it
with tmp as (
select T.HATId as "ID",
sum(case when T.ID = 4 or HA.ID < 0 then 1 else 0 end) as sum1,
count(*) as nb
from Hats T
group by T.HATId
)
select HATId, round(cast(sum1 as decimal)/ nb * 100, 2) NewID
from tmp