I need to create calculate table for the report in PowerBI Desktop.
I know how to do that in t-sql but I am unable to interpret it to DAX.
So should I use t-sql and add this query using "Get Data"?
Or should I create calculate table using DAX?
Which one is more efficient?
select distinct PolicyNumber,
ReserveStatus,
case when ReserveStatus = 'Open' then 1 else 0 end as OpenStatus
from RockhillClaimsDataFeed_PBI
group by PolicyNumber,ReserveStatus
Result looks like that:
can somebody help?
This is achievable by creating a calculated table in Power BI, with similar syntax using SELECTCOLUMNS and DISTINCT.
RockhillClaimsSummary =
DISTINCT(
SELECTCOLUMNS(
RockhillClaims,
"PolicyNumber", RockhillClaims[PolicyNumber],
"ReserveStatus", RockhillClaims[ReserveStatus],
"OpenStatus", IF(RockhillClaims[ReserveStatus] = "Open", 1, 0)
)
)
Results:
Related
I am very new to Snowflake. Till now I had used Teradata for writing complex SQL queries.
In snowflake, I need to create and call macros (similar to Teradata), where I have to pass date as parameters, and within the function I have to append records in a table. Something along these lines:
CREATE TABLE SFAAP.WS_DIRBNK_DPST.PV_HIGH_RISK_FI_LIST
(
APP_DT DATE
,FI_NAME VARCHAR(50)
);
CREATE OR REPLACE FUNCTION SFAAP.INSERT_FI (DT DATE, CRED CHAR(5))
--RETURNS NULL
--COMMENT='Create list of high risk FI by date'
AS
'
INSERT INTO SFAAP.WS_DIRBNK_DPST.PV_HIGH_RISK_FI_LIST
TO_DATE(DD) --------------Passed Parameter
,FI_NAME
FROM
(
SELECT
FINANCIAL_INSTITUTION AS FI_NAME
,COUNT(DISTINCT CASE WHEN IND_FPFA_FRAUD = 1 THEN APP_ID ELSE NULL END) AS TOT_FPFA_APPS
,COUNT(DISTINCT APP_ID) AS TOT_APPS
,CAST(TOT_FPFA_APPS AS DECIMAL(38,2))/TOT_APPS AS FRAUD_RATE
FROM
(
SELECT
A.*
,C.FINANCIAL_INSTITUTION
FROM BASE_05 A
LEFT JOIN
(
SELECT
BNK_ACCT_NBR_TOK
,BNK_TRAN_TYP_CDE
,ALT_DR_CR_CDE
,TRAN_1_DSC_TOK
,TRAN_DT
,TRAN_AMT
FROM "SFAAP"."V_SOT_DIRBNK_CLB_FRD_CRD"."BNK_DPS_TRAN_RLT_INFO"
WHERE TRAN_DT BETWEEN DATEADD(Day,-90,TO_DATE(DD)) AND TO_DATE(DD) --------------Passed Parameter, does calculation in the 90 days window from the passed date
AND ALT_DR_CR_CDE = TO_CHAR(CRED) --------------Passed Parameter
AND BNK_TRAN_TYP_CDE IN (22901,56003,56002,56302,56303,56102,70302)
AND TRAN_AMT>=5
QUALIFY ROW_NUMBER() OVER(PARTITION BY BNK_ACCT_NBR_TOK, TRAN_DT, TRAN_AMT, BNK_TRAN_TYP_CDE ORDER BY TRAN_DT ASC, TRAN_AMT DESC)=1
) B
ON A.BNK_ACCT_NBR = B.BNK_ACCT_NBR_TOK
LEFT JOIN SFAAP.WS_DIRBNK_DPST.PV_FRAUD_METRICS_03 C
ON B.TRAN_1_DSC_TOK = C.TOKEN_NAME
)SUB_A
GROUP BY 1
)SUB_B
WHERE FINANCIAL_INSTITUTION IS NOT NULL
AND TOT_APPS>=3
AND FRAUD_RATE>=0.20
'
;
I took some guidance from this answer here, but I am still not there yet. Here's the error which I am getting:
Due to lack of experience writing snowflake user-defined functions, I think I am messing up syntax somewhere (could be the way I am passing those two parameters). Comments/suggestions are most welcome.
Thanks in advance.
It looks like SFAAP is your database name, please include your schema name if you are going to use "Fully Qualified Names", or change your session context to use a database and schema and then create the function without the database and schema name.
example:
CREATE OR REPLACE FUNCTION SFAAP.WS_DIRBNK_DPST.INSERT_FI (
I am pretty new to the DAX world. I am trying to do get distinct records on multiple columns in DAX query similar to the way I do in SQL. I tried joining two tables based on the model in the Query Designer which gave me the following query.
EVALUATE SUMMARIZECOLUMNS(
'Dim_Products'[SaleCode],
'Dim_Products'[ProducttName],
'Dim_TimeZone'[StartDate],
'Dim_TimeZone'[StartTime],
'Dim_TimeZone'[EndDate],
'Dim_TimeZone'[EndTime],
'Dim_TimeZone'[Variation],
"Fact_Sales_Count", [Fact_Sales_Count]
)
Running the above is giving duplicate records. How do I just get distinct records as I am trying to call this from SSRS?
Thanks!
Look at: https://www.sqlbi.com/articles/introducing-summarizecolumns/
You switch from "group by" columns to "summary" columns by convention in the argument list to SUMMARIZECOLUMNS.
EG:
EVALUATE SUMMARIZECOLUMNS(
'Dim_Products'[SaleCode],
'Dim_Products'[ProducttName],
'Dim_TimeZone'[StartDate],
'Dim_TimeZone'[StartTime],
'Dim_TimeZone'[EndDate],
'Dim_TimeZone'[EndTime],
'Dim_TimeZone'[Variation],
"Fact_Sales_Count", sum([Fact_Sales_Count])
)
Just in case if this helps someone in future.
EVALUATE
DISTINCT(
SELECTCOLUMNS('Dim_Products',
'Dim_Products'[SaleCode],
'Dim_Products'[ProducttName],
'Dim_TimeZone'[StartDate],
'Dim_TimeZone'[StartTime],
'Dim_TimeZone'[EndDate],
'Dim_TimeZone'[EndTime],
'Dim_TimeZone'[Variation]))
And, if we need to add a filter:
EVALUATE
DISTINCT(
SELECTCOLUMNS(
FILTER('Dim_Products', 'Dim_Products'[SaleCode] = 123 && ('Dim_Products'[ProducttName] = "ABC" || 'Dim_Products'[ProducttName] = "XYZ" )),
'Dim_Products'[SaleCode],
'Dim_Products'[ProducttName],
'Dim_TimeZone'[StartDate],
'Dim_TimeZone'[StartTime],
'Dim_TimeZone'[EndDate],
'Dim_TimeZone'[EndTime],
'Dim_TimeZone'[Variation]))
We use a housing software based on ColdFusion and uses SQL to create database functions in our reports. I am working on a custom report to try to subtract one column from another: more specifically subtract COUNT_OF_INSPECTION_TYPE from OCCUPANCY. The OCCUPANCY column is based on the following code:
CONVERT(INT
, CASE
WHEN tblHalls.HALLNAME = 'Hall1' THEN 198
WHEN tblHalls.HALLNAME = 'Hall2' THEN 430
WHEN tblHalls.HALLNAME = 'Hall3' THEN 333
END
)
When I try a new function OCCUPANCY - COUNT_OF_INSPECTION_TYPE, I get an error:
Unable to invoke CFC - Error Executing Database Query. [Macromedia][SQLServer JDBC Driver][SQLServer]Invalid column name
'OCCUPANCY'.
I'm not sure if I am explaining this right. I'd appreciate any help you can offer.
You cannot create an alias and use it in another calculation, at the same level, because the alias is not defined yet. Either repeat the whole CASE ... END statement in the calculation (less desirable) OR use another option, such as a CTE, derived table, APPLY operator, etcetera.
Wrapping your existing SELECT in a CTE is probably one of the simplest options:
;WITH cte
AS
(
-- Your current SELECT statement goes here
SELECT
CONVERT(INT
, CASE
WHEN tblHalls.HALLNAME = 'Hall1' THEN 198
WHEN tblHalls.HALLNAME = 'Hall2' THEN 430
WHEN tblHalls.HALLNAME = 'Hall3' THEN 333
END
)
AS OCCUPANCY
, tblHalls.COUNT_OF_INSPECTION_TYPE
, ... (Other Columns)
FROM tblHalls
)
-- Now use the alias in a calculation
SELECT cte.*
, cte.OCCUPANCY - cte.COUNT_OF_INSPECTION_TYPE AS SomeAliasForThisCol
FROM cte
Side note, since the CASE statement does not define an ELSE condition, the result of the calculation will be NULL if none of HallName's match. If that is not desirable, consider setting a default.
I am facing problem converting bellow query in spark-sql in pyspark
SQL-server query is
coalesce((Select top 1 f2.ChargeAmt from Fact_CMCharges f2
where f2.BldgID = f.BldgID
and f2.LeaseID = f.LeaseID
and f2.IncomeCat = f.IncomeCat
and f2.Period < f.Period
and f2.ActualProjected = 'Lease'
order by f2.Period desc),0) as Charge
I did not find replacing key word of top in pyspark sql . Kindly Help me
how could i convert this query in py-spark sql
Since you said Spark-SQL and if you have `DF', then you can use something like this.
df.limit(1).show()
Good day,
I have a table as follows.
What I would love to do is add a new Column that will tabulate/summarize (anyway possible) called "New Net" by CovID/PolicyNo/CovYear/Positive(Negative) values.
In the example below the new column would look like this.
In short, what we are trying to do is SumUp all the Values in that group and only place that total in the first row of that group and zero out all the others. Any help/pointers would be appreciated with this. I have tried SQL Server Window Functions, standard SUM/GROUP.
This should meet ypur expectations:
SELECT PolicyNo ,
CovID ,
CovYear ,
p ,
net,
CASE WHEN ROW_NUMBER()OVER(PARTITION BY CovID, PolicyNo, CovYear, net ORDER BY PolicyNo) = 1 THEN net ELSE 0 END AS NewNet
FROM dbo.test1;