PowerBI Sum/IF/CASE WHEN - mysql

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

Related

End of month value - take the last value in month

how can i take the last value in month per month?
enter image description here
i need to take in this month - 2461 but my measure returm me blank .
this is my measure =
LastConversion_Repayments =
VAR maxDate =
CALCULATE (
MAX ( 'DimDate'[Date] ),
FILTER ( ALL ( 'DimDate' ),[sum_currentrepayments2] <> 0 )
)
var endofmonth1 = ENDOFMONTH(DimDate[Date])
var notblank = if(NOT(ISBLANK([sum_currentrepayments2])),1,0)
RETURN
IF (
notblank = 0,
BLANK (),
CALCULATE (
[sum_currentrepayments2],
FILTER ( ALL('DimDate'), 'DimDate'[Date] = endofmonth1 )
)
)
what i do wrong ???
Thx a lot!
i need to take in this month - 2461 but my measure returm me blank .

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

How to Add and Compare Results from Subqueries

I have the following code written for a larger report that I have been working on. I have three columns resulting from subqueries: req_hrs, e_hrs, inprog_hrs. I need to add together the e_hrs and inprog_hrs, thus seeing whether that number is greater than or equal to req_hrs. If that is true, I need to return either an * or a null value.
Can someone please explain to me how I can add the two subquery results (e_hrs and inprog_hrs) together, and then compare that result to req_hrs thus returning said * or NVL? Code is below, Thank you:
SELECT
spriden_last_name lname,
spriden_first_name fname,
spriden_mi mi,
spriden_id id,
x.shrdgmr_majr_code_1 majr,
x.shrdgmr_grad_date grad_dt,
x.shrdgmr_degs_code degs,
DECODE(stvdegs_award_status_ind,'A','*',NULL) award_ind,
**DECODE(NVL(m.smbagen_req_credits_overall,0),0,
DECODE(NVL(sorcmjr_req_hours_ssdf,0),0,
DECODE(stvdegc_acat_code,'22',32,'23',64,'24',124,'42',42,999),
sorcmjr_req_hours_ssdf),m.smbagen_req_credits_overall) req_hrs,**
**TRUNC(shrlgpa_hours_earned,2) AS e_hrs,**
**(SELECT
NVL(SUM(sfrstcr_credit_hr),0)
FROM
sfrstcr
WHERE
sfrstcr_term_code = '&inprog_term'
AND sfrstcr_pidm = x.shrdgmr_pidm
AND sfrstcr_rsts_code IN ('RE','RW')
AND NOT EXISTS (
SELECT
'Y'
FROM
shrtckn,
shrtckg j
WHERE
shrtckn_pidm = sfrstcr_pidm
AND shrtckn_term_code = sfrstcr_term_code
AND shrtckn_crn = sfrstcr_crn
AND j.shrtckg_pidm = shrtckn_pidm
AND j.shrtckg_term_code = shrtckn_term_code
AND j.shrtckg_tckn_seq_no = shrtckn_seq_no
AND j.shrtckg_seq_no = (
SELECT
MAX(k.shrtckg_seq_no)
FROM
shrtckg k
WHERE
k.shrtckg_pidm = shrtckn_pidm
AND k.shrtckg_term_code = shrtckn_term_code
AND k.shrtckg_tckn_seq_no = shrtckn_seq_no))) AS inprog_hrs,**
ROUND(shrlgpa_gpa,2) gpa,
DECODE(SIGN(shrlgpa_gpa - 3.90),0,'S',1,'S',
DECODE(SIGN(shrlgpa_gpa - 3.75),0,'M',1,'M',
DECODE(SIGN(shrlgpa_gpa - 3.50),0,'C',1,'C',NULL))) latin,
(SELECT
m.shrasdl_astd_code_dl
FROM
shrasdl m
WHERE
m.shrasdl_term_code_effective = (
SELECT
MAX(n.shrasdl_term_code_effective)
FROM
shrasdl n)
AND m.shrasdl_min_gpa_term = (
SELECT
MAX(n.shrasdl_min_gpa_term)
FROM
shrasdl n
WHERE
n.shrasdl_term_code_effective = m.shrasdl_term_code_effective
AND shrlgpa_gpa >= n.shrasdl_min_gpa_term)) honors
FROM
shrdgmr x,
stvdegs,
stvdegc,
spriden,
sorcmjr,
smbagen m,
shrlgpa
WHERE
TO_CHAR(x.shrdgmr_grad_date,'MON-YY') IN ('&grad_dt1', NVL('&grad_dt2','XXX-
00'))
AND x.shrdgmr_seq_no = (
SELECT
MAX(z.shrdgmr_seq_no)
FROM
shrdgmr z
WHERE
z.shrdgmr_pidm = x.shrdgmr_pidm
AND z.shrdgmr_majr_code_1 = x.shrdgmr_majr_code_1
AND z.shrdgmr_grad_date IS NOT NULL)
AND stvdegs_code = x.shrdgmr_degs_code
AND stvdegc_code = x.shrdgmr_degc_code
AND spriden_pidm = x.shrdgmr_pidm
AND spriden_change_ind IS NULL
AND sorcmjr_cmjr_rule(+) = x.shrdgmr_cmjr_rule_1_1
AND REPLACE(m.smbagen_area(+),'-CORE','') = x.shrdgmr_majr_code_1
AND m.smbagen_active_ind(+) = 'Y'
AND m.smbagen_term_code_eff(+) <= x.shrdgmr_term_code_grad
AND ((m.smbagen_area IS NULL)
OR (m.smbagen_area IS NOT NULL
AND m.smbagen_term_code_eff = (
SELECT
MAX(n.smbagen_term_code_eff)
FROM
smbagen n
WHERE
REPLACE(n.smbagen_area,'-CORE','') = x.shrdgmr_majr_code_1
AND n.smbagen_active_ind = 'Y'
AND n.smbagen_term_code_eff <= x.shrdgmr_term_code_grad)))
AND shrlgpa_pidm(+) = x.shrdgmr_pidm
AND shrlgpa_levl_code(+) = x.shrdgmr_levl_code
AND shrlgpa_gpa_type_ind(+) = 'O'
ORDER BY
spriden_last_name,
spriden_first_name,
spriden_mi
;
Compare the sum of the union of the two sources with the required amount.
In simplified form:
select somekey, sum(hrs) worked_hrs, sum(req_hrs) required_hrs
from (
select
somekey,
e_hrs hrs
from e_hrs_table
where ...
union all -- the "all" is important to leave in!
select
somekey,
inprog_hrs
from inprog_hrs_table
where ...
) x
join req_hrs_table on req_hrs_table.somekey = x.somekey
where ... -- add req_hrs_table conditions here
group by somekey
You can add
having sum(hrs) < sum(req_hrs)
if you want only those rows that did not meet the quota.

GroovySql - retrieving list of values for named parameter to use with sql in operator

I'm trying to figure out the correct syntax to use to pass a list of values to the 'in' operator using GroovySql. When I pass in a comma separated list of values only the first value gets applied while the other values are ignored.
My code looks like:
final String query = '''\
SELECT
li_ch.event_id as event_id,
sum(CASE li_ch.line_item_type WHEN 0 THEN 1 WHEN 900 THEN (CASE li_p.line_item_type WHEN 0 THEN -1 ELSE 0 END) ELSE 0 END) sold,
sum(li_ch.amount) as gross
FROM
line_item li_ch LEFT JOIN line_item li_p on li_ch.parent_line_item_id = li_p.id,
sale s
WHERE
s.id = li_ch.sale_id AND
li_ch.event_id in (${Sql) AND
s.sale_status_type in (:eventIds) AND
li_ch.line_item_type in (0, 900)
GROUP BY li_ch.event_id
'''
// Create new Groovy SQL instance with injected DataSource
final Sql sql = new Sql(dataSource)
def ids = eventList.collect { "'$it'" }.join(",")
final List<GroovyRowResult> rows = sql.rows(query, eventIds: Sql.expand(ids))
rows
There's a pull request to make this easier, but for now you have to join your own attributes together:
def values = "'${list.join('\',\'')}'"
// or less confusingly:
// def values = list.collect { "'$it'" }.join(",")
sql.rows( "select * from table where value in (${Sql.expand(values)})" )

update query based on select query

exactly I want to write "UPDATE" and "SELECT" into one query.
I need check for setting a field.
for this action, I used SELECT query on TABLE1 and then if it dose not have result, another filed of TABLE2 is updated.
FOR EX:
$res = mysqli_query($con , "select sID FROM schedule where (dayID = '{$this->dID}' AND patientID = '')");
$rep = mysqli_fetch_array($res);
if(count($rep) == 0)
mysqli_query($con,"update days set schFilled = 1 where dID = '{$this->dID}'");
else
mysqli_query($con,"update days set schFilled = 0 where dID = '{$this->dID}'");
I would like run those with ONE query, in fact I want something like this:(whit CASE to write second update too)
update days set schFilled = 0 where( (select sID FROM schedule where (dayID = '{$this->dID}' AND patientID = '') IS NULL) AND (dID = '{$this->dID}'))
use the mysqli object like this:
$res = mysqli_query($con , "your select");
if($res->num_rows === 0) {
//no res
}
else {
//else
}
"UPDATE days SET schFilled =
CASE
WHEN (SELECT IF(EXISTS(select sID FROM schedule where (dayID = '{$this->dID}' AND patientID = '' AND hour != 0)), 1, 0))
THEN 0
ELSE 1
END
where dID = '{$this->dID}'"