Not equal expression - reporting-services

I'm looking at using 4 expressions 3 being equals and 1 being not equals but is not counting the not equals
im using .
=sum(iif(Fields!NEW_SUPPLIER.Value = "Eon", 1,0)
And (Fields!WeekDay.Value = "Monday")
And (Fields!UTILITY.Value = "Elec")
And (Fields!OLD_SUPPLIER.Value <> "Eon"))
Please can some one point me in the right direction.

You need to make sure all the checks are in the first argument of the IIf expression:
=sum
(
iif
(
Fields!NEW_SUPPLIER.Value = "Eon"
And Fields!WeekDay.Value = "Monday"
And Fields!UTILITY.Value = "Elec"
And Fields!OLD_SUPPLIER.Value <> "Eon"
,1
,0
)
)

Related

Generating a table of subtraction of two colum count

Hello i want to make a summary table where the filter is the segment and date
The columns will be the subtraction of count of two column in same table so far i have come up to this
select
case
when SEGMENT='champion'THEN SUM(SEGMENTED_DATE = '2022-10-14')-SUM(SEGMENTED_DATE='2022-10-07')
END as total_change_champion,
case
when SEGMENT='Hibernating'THEN SUM(SEGMENTED_DATE = '2022-10-14')-SUM(SEGMENTED_DATE='2022-10-07')
END as total_change_hibernate
from weekly_customer_RFM_TABLE;
This query is giving me null value for columns
select
SUM(SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENTED_DATE='2022-10-07')as total_changes_in_14th
From weekly_customer_RFM_TABLE
where
SEGMENT ='Hibernating'
This query is actually giving me the result. but when I am going to make a consolidated table using different segment subtraction i am getting null value in my result table.Kindly help me out
my sample data
which is i want to substract totalcount of 2022/10/14 - 2022/10/07 for each segment
image is not loading unfortunately
You must include the CASE expressions inside the aggregate functions:
SELECT COUNT(CASE WHEN SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-14' THEN 1 END) -
COUNT(CASE WHEN SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-07' THEN 1 END) AS total_change_champion,
COUNT(CASE WHEN SEGMENT = 'Hibernating' AND SEGMENTED_DATE = '2022-10-14' THEN 1 END) -
COUNT(CASE WHEN SEGMENT = 'Hibernating' AND SEGMENTED_DATE='2022-10-07' THEN 1 END) AS total_change_hibernate
FROM weekly_customer_RFM_TABLE;
For MySql this can be simplified to:
SELECT SUM(SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-07') AS total_change_champion,
SUM(SEGMENT = 'Hibernating' AND SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENT = 'Hibernating' AND SEGMENTED_DATE='2022-10-07') AS total_change_hibernate
FROM weekly_customer_RFM_TABLE;
You may also use a WHERE clause to filter the table for only the SEGMENTs and SEGMENTED_DATEs that you want:
WHERE SEGMENT IN ('champion', 'Hibernating') AND SEGMENTED_DATE IN ('2022-10-07', '2022-10-14')

PowerBI Sum/IF/CASE WHEN

SUM(B.RefundedAmount) + SUM(CASE WHEN B.AccountTypeID = 2 AND B.TotalStake = B.TotalPayout THEN B.TotalPayout ELSE 0 END)
The following is the logic for SQL code that I need to convert into a new powerBI column/measure.
RefundedAmount, AccountTypeID, TotalStake and TotalPayout are all columns in the dataset. I'm struggling to wrap my head around the addition aspect along with the IF/CASE WHEN statement.
Does it only mean that IF (AccountTypeID = 2 AND TotalStake = TotalPayout) then we return RefundedAmount + TotalPayout, otherwise return 0?
The measure always returns at least RefundedAmount, but if AccountTypeID = 2 and TotalStake = TotalPayout then it returns the sum of RefundedAmount and TotalPayout.
The measure could be written in DAX as:
Measure :=
VAR _refund = SUM ( 'Table'[RefundedAmount] )
VAR _payout =
SUMX (
FILTER (
'Table' ,
'Table'[TotalPayout] = 'Table'[TotalStake]
&& 'Table'[AccountTypeID] = 2
),
'Table'[TotalPayout]
)
RETURN
_refund + _payout

Using case statement or IF inside a WHERE clause

I have several stored procedures that are almost identical but have some different AND parts inside a WHERE clause.
Based on a variable deptname, I want to add additional AND/OR conditions to my already existing WHERE clause. So kind of like IF/CASE WHEN on the part that is different.
Think about it as string concatenation
query_string = 'WHERE a= XYZ AND B= 123"
if deptname = a: query_string + "AND additional conditions for dept a"
else if deptname = b:query_string + "AND additional conditions for dept b"
What is the appropriate way to use a variable?
here is some pseudo code of what I am trying to do
SELECT
personID AS pid,
personcode,
persondeptcode,
more_fields AS fields
FROM
TABLE_XYZ
WHERE
--shared parts
personcode = 'C'
AND
persondeptcode = 'MAJ'
--- NOW the different part
IF #deptname = "deptA"
AND
(
PROGRAM_LDESCR IN
(
'prog1',
'prog2',
'prog3'
)
OR
aprogram IN ('aprogram1')
OR
(aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123'))
);
--- THIS IS A DIFFERENT DEPT SO WE HAVE DIFFERENT AND PART
ELSE IF #deptname = "deptB"
(
PROGRAM_LDESCR IN
(
'1234'
)
OR
aprogram IN ('a1234')
);
You can use a CASE expression in this case, the important thing is to make sure you have an ELSE clause to ensure the expression remains true if #deptname is not one of the two values with extra conditions:
WHERE personcode = 'C'
AND persondeptcode = 'MAJ'
AND (CASE #deptname
WHEN "deptA" THEN PROGRAM_LDESCR IN ('prog1', 'prog2', 'prog3')
OR aprogram IN ('aprogram1')
OR aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123')
WHEN "deptB" THEN PROGRAM_LDESCR IN ('1234')
OR aprogram IN ('a1234')
ELSE 1
END)
Here is a simple demo of a CASE expression used in this fashion.
You seem to want something like:
AND
(#deptname = 'dept123' AND (PROGRAM_LDESCR IN ('1234') OR aprogram IN ('a1234')) OR
#deptname <> 'dept123'
)
To combine the last part of the WHERE clause (if I'm understanding your commented-code correctly), you could do something like the following:
SELECT
personID AS pid,
personcode,
persondeptcode,
more_fields AS fields
FROM
TABLE_XYZ
WHERE
personcode = 'C'
AND persondeptcode = 'MAJ'
AND (
(#deptname="deptA" AND (PROGRAM_LDESCR IN ('prog1', 'prog2', 'prog3') OR aprogram IN ('aprogram1') OR (aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123'))))
OR
(#deptname="deptB" AND (PROGRAM_LDESCR IN ('1234') OR aprogram IN ('a1234'))
)
Normally you would use the WHERE clause to filter out unnecessary rows of data and a CASE statement if you wanted to actually change the value in the SELECT statement (I rarely see CASE statements outside a SELECT clause, unless it is doing something like a complex sort).

SSRS : How to aggregate on certain values of a column?

I have a column (Say PriceA) in SSRS which gets populated based of Expression.
PriceA.value
IIF((Fields!MOP.Value = "PRIV" OR Fields!MOP.Value = "FACI" OR Fields!MOP.Value = "GUAR" OR Fields!MOP.Value = "CASH"
OR Fields!MOP.Value = "CHRG" OR Fields!MOP.Value = "INV"),
IIF(Fields!UDRx.Value,"U/D",Fields!TtlPrice.Value),
IIF(Fields!elecadj.Value="0" AND Fields!elecadj2.Value="0", IIF(Fields!UDRx.Value,"U/D",IIF(Fields!MOP2.Value="0",Fields!Copay.Value,"")),
IIF(Fields!UDRx.Value,"U/D",Fields!PatientPayAmt.Value)
)
)
The Value in PriceA can be "U/D" or Zero or some value as "150.00". Now I want to display at the end of the report, an aggregate of the rows in that column, discarding the values $0.0 and "U/D".
How do I do that?
I also have another column where it shows values based off off 2 different expression, that I need to aggregate. That one is not working either.
2ndPriceA_expression
IIF((Fields!MOP.Value = "PRIV" OR Fields!MOP.Value = "FACI" OR Fields!MOP.Value = "GUAR" OR Fields!MOP.Value = "CASH"
OR Fields!MOP.Value = "CHRG" OR Fields!MOP.Value = "INV"), " ",
IIF(Fields!elecadj.Value="0" AND Fields!elecadj2.Value="0",
IIF(Fields!UDRx.Value="0",Format(Fields!TtlPrice.Value,"C"),"U/D") ,
IIF(Fields!UDRx.Value="0",Format(Fields!Payor1PaidAmt.Value,"C"),"U/D")
)
)
2ndPriceB_expression
IIF(Fields!MOP2.Value<>"",
IIF(Fields!elecadj.Value="0" and Fields!elecadj2.Value="0"
,IIF(Fields!UDRx.Value="1","U/D",Fields!Copay.Value)
,IIF(Fields!UDRx.Value="1"
,"U/D"
,IIF(Fields!elecadj2.Value="0"
,Fields!Copay.Value
,Fields!Payor2PaidAmt.Value
)
)
)
," ")
I need to show aggregate of 2ndPriceA & 2ndPriceB in a 3rd cell. Sorry I'm 2 weeks new to SSRS.
Thanks in advance. any help is appreciated.
Just do a SUM() of an IIF() expression that will filter out the values you don't want to SUM.

combining two case queries into one

I want to make a query where where i first check the current date with the date in a column and then base on that I am writing my case. this query is working fine individually but when
i am combining them its not working.
The queires are
SELECT MONTH(CURRENT_DATE)= SUBSTRING(yearmonth,6) FROM dp;
SELECT i,
CASE
WHEN DAY(CURRENT_DATE) =1 THEN `d1_v`
WHEN DAY(CURRENT_DATE) =2 THEN `d1_v`
END VALUE
FROM dp;
combined query..
SELECT i,
CASE
WHEN((MONTH(CURRENT_DATE ))= SUBSTRING(yearmonth,6) THEN
(CASE
WHEN DAY(CURRENT_DATE) = 1 THEN `d1_v`
WHEN DAY(CURRENT_DATE) = 2 THEN `d1_v`
END VALUE)END)Y
FROM dp
please guide me
You've to delete the '(' after the THEN:
SELECT i,
CASE
WHEN ((MONTH(CURRENT_DATE )) = SUBSTRING(yearmonth,6))THEN
CASE
WHEN DAY(CURRENT_DATE) = 1 THEN `day1_value`
WHEN DAY(CURRENT_DATE) = 2 THEN `day1_value`
END
END Y
FROM dp;
The output from the sqlfiddle is 5,null right now.
Hope this work for you.
The sqlfiddle is: http://sqlfiddle.com/#!2/e59c5/10